Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(vue): add vue typescript support #726

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/using-baklava-in-vue.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ Also, you can add ignore rule as compiler options to your webpack or vite.
}
```
### TypeScript
Baklava offers TypeScript support for Vue versions 2.7 and higher. To enable this support, you should create a file named `components.d.ts` within the "src" directory and include the following line:
```ts
/// <reference types="@trendyol/baklava/dist/baklava-vue.d.ts" />
```
#### Eslint Configuration
Baklava components are developed with `kebab case`. Eslint uses `pascal case` by default. If you are using eslint in your project, it will automatically convert the baklava components to `pascal case`. To prevent this, you need to turn off the `pascal case` rule in your project.
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"start": "npm run storybook:dev",
"analyze": "cem analyze",
"commit": "commit",
"build": "del-cli dist/ && npm run analyze && npm run generate-react-exports && tsc --emitDeclarationOnly && node scripts/build.js",
"build": "del-cli dist/ && npm run analyze && npm run generate-react-exports && tsc --emitDeclarationOnly && node scripts/build.js && npm run generate-vue-types",
"build-storybook": "NODE_ENV=production storybook build -o storybook-static",
"build-storybook-docs": "storybook build --docs",
"serve": "node scripts/build.js --serve",
Expand All @@ -54,7 +54,8 @@
"test:watch": "web-test-runner --coverage --watch",
"test:debug": "web-test-runner --coverage --watch --debug",
"test:headless": "web-test-runner --coverage --watch --debug --headless",
"generate-react-exports": "node scripts/generate-react-exports.js"
"generate-react-exports": "node scripts/generate-react-exports.js",
"generate-vue-types": "node scripts/generate-vue-types.js"
},
"keywords": [
"web-components",
Expand Down
18 changes: 18 additions & 0 deletions scripts/generate-vue-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import fs from "fs";

const components = JSON.parse(fs.readFileSync("./dist/custom-elements.json", "utf-8"));
const declerations = components.modules.flatMap(module => module.declarations);
const customElements = declerations.filter(declaration => declaration.customElement === true);
const customElementNames = customElements.map(customElement => customElement.name);

const code = `
import type * as Baklava from '@trendyol/baklava/dist/baklava'
declare module 'vue' {
export interface GlobalComponents {
${customElementNames.map(component => `${component}: import("vue").Component<Baklava.${component}>`).join('\n ')}
}
}
`

fs.writeFileSync("./dist/baklava-vue.d.ts", code);
Loading