[
  {
    "path": ".gitignore",
    "content": "node_modules\n.DS_Store\ndist\ndist-ssr\n*.local\ndocs/.vuepress/.cache\ndocs/.vuepress/.temp\ntypes\n"
  },
  {
    "path": ".nvmrc",
    "content": "lts/*\n"
  },
  {
    "path": "README.md",
    "content": "[![Netlify Status](https://api.netlify.com/api/v1/badges/b1b84831-789e-4629-a9e3-55a36e136653/deploy-status)](https://app.netlify.com/sites/sharp-babbage-154f0a/deploys)\n\n# Vue Component Library Starter\n\n> Create your own [Vue 3](https://v3.vuejs.org/) component library with TypeScript, [Vite](https://vitejs.dev) and [VitePress](https://vitepress.vuejs.org/).\n\nSooner or later, you will find that creating a component library is much better than having all components inside your app project. A component library force to you remove app specific logic from your components, making it easier to test and reuse them in other apps.\n\nOnce the components are in a library, documentation becomes critical. This starter project includes a documentation app powered by VitePress. It not only documents the usage of the component, but also provides a testing bed during the development of components. See the generated documentation app [here](https://sharp-babbage-154f0a.netlify.com/).\n\n## Setup\n\n> When running `docs:dev` for the first time, you may encounter error like `vitepress data not properly injected in app` in your browser. Restart the server and reload the browser. Please refer to [issue #30](https://github.com/wuruoyun/vue-component-lib-starter/issues/30) for more details.\n\n```bash\n# install dependencies\nnpm install\n\n# start the doc app with hot reload, great for testing components\nnpm run docs:dev\n\n# build the library, available under dist\nnpm run build\n\n# build the doc app, available under docs/.vitepress/dist\nnpm run docs:build\n\n# preview the doc app locally from docs/.vitepress/dist\nnpm run docs:serve\n```\n\nYou may use [Netlify](https://www.netlify.com/) to auto build and deploy the doc app like this project does.\n\n## Develop and test locally\n\nThe best way to develop and test your component is by creating demos in `docs/components/demo` folder, as shown by the example components.\n\nIf you want to test the library in your Vue3 app locally:\n\n- In the root folder of this library, run `npm link`. This will create a symbolic link to the library.\n- In the root folder of your client app, run `npm link my-lib`. This will add the symbolic link to the `node_modules` folder in your client app.\n- You can now import `my-lib` in your client app.\n\nThere is no need to add `my-lib` to your client app's dependency in this case.\n\nIf you made changes to the library, you will need to rebuild the library. Your Vue3 app shall hot reload when the building of library is completed.\n\n## How it works\n\n### Components\n\nThe library is a [Vue plugin](https://v3.vuejs.org/guide/plugins.html). The `install` function in [index.ts](src/index.ts) registers all components under [components](src/components) to Vue globably.\n\nThe components are also exported by [index.ts](src/index.ts) so that the client app can import them individually and register them locally, instead of using the library as a plugin. This may be a better option if the client app only use a small set of components in your library.\n\nAs there are already many UI component libraries for Vue 3, you may just want to build on top of one of them and create components for your specific needs. The Component B in this starter shows the example of using [PrimeVue](https://www.primefaces.org/primevue/) as the fundation library. However, this means the client app shall also use the same fundation component library as your library does.\n\nThe doc app itself is a client app of the libary, therefore PrimeVue is imported in [docs/.vitepress/theme/index.js](docs/.vitepress/theme/index.js). The configuration in [docs/.vitepress/config.js](docs/.vitepress/config.js) below forces VitePress to resolve these modules with no duplication, avoiding error at runtime, as PrimeVue also has Vue in its dependency.\n\n```js\nmodule.exports = {\n  vite: {\n    resolve: {\n      dedupe: ['vue', /primevue\\/.+/],\n    },\n  },\n}\n```\n\n> In [vite.config.ts](vite.config.ts), format 'umd' is not present in `build.lib.formats` option. This is because the PrimeVue components used by this library are externalized, and therefore requiring corresponding options in `rollupOptions.output.globals`. To avoid adding global varaibles for PrimeVue components, 'umd' is removed for simplicity.\n\n### Utilities and constants\n\nThe library includes example utilities and constants. They are also exported in [index.ts](src/index.ts). The client app may use them as below:\n\n```js\n<script lang=\"ts\">\nimport { MyConstants, MyUtil } from 'my-lib'\n\nexport default {\n  data () {\n    return {\n      magicNum: MyConstants.MAGIC_NUM\n    }\n  },\n  methods: {\n    add (a:number, b:number) {\n      return MyUtil.add(a, b)\n    }\n  }\n}\n</script>\n```\n\n### Styling\n\nIndividual components may have styles defined in its `.vue` file. They will be processed, combined and minified into `dist/style.css`, which is included in the `exports` list in [package.json](package.json).\n\nIf you have library level styles shared by all components in the library, you may add them to [src/assets/main.scss](src/assets/main.scss). This file is imported in [index.ts](src/index.ts), therefore the processed styles are also included into `dist/style.css`. To avoid conflicting with other global styles, consider pre-fixing the class names or wrapping them into a namespace class.\n\nIf you have your own special set of SVG icons, you may create a font file (`.woff` format) using tools like [Icomoon](https://icomoon.io/) or [Fontello](https://fontello.com/). This starter includes an example font file [src/assets/fonts/myfont.woff](src/assets/fonts/myfont.woff) and references it in [src/assets/main.scss](src/assets/main.scss), with utility icon CSS classes. An icon from the font file is used in Component A. Vite will include the font file into the build, see [https://vitejs.dev/guide/assets.html](https://vitejs.dev/guide/assets.html).\n\nThe client app shall import `style.css`, usually in the entry file:\n\n```js\nimport 'my-lib/dist/style.css'\n```\n\n### Third-party dependencies\n\nThird-party libraries used by you library may bloat up the size of your library, if you simply add them to the `dependencies` in [package.json](package.json).\n\nThe following are some strategies to reduce the size of your library:\n\n#### Externalization\n\nIf you expect the client app of your library may also need the same dependency, you may externalize the dependency. For example, to exclude PrimeVue from your library build artifact, in [vite.config.ts](vite.config.ts), you may have\n\n```js\nmodule.exports = defineConfig({\n    rollupOptions: {\n      external: ['vue', /primevue\\/.+/]\n    }\n  }\n})\n```\n\nThe dependency to be externalized may be declared as peer dependency in your library.\n\n#### Cherry picking\n\nIf you don't expect the client app of your library also needing the same dependency, you may embed cherry-picked functions. For example, to embed the `fill` function of popular library [lodash](https://lodash.com), import the `fill` function like the following:\n\n```js\nimport fill from 'lodash/fill'\n```\n\nEven with tree-shaking, the codes being brought into your library may still be large, as the function may have its own dependencies.\n\nNote that `import { fill } from 'lodash'` or `import _ from 'lodash'` will not work and will embed the whole `lodash` library.\n\nFinally, if your client app also use `lodash` and you don't want `lodash` to be in both the client app and your libraries, even after cherry-picking, you may consider cherry-picking in component library and re-export them as utils for client to consume, so that the client does not need to depend on `lodash`, therefore avoiding duplication.\n\n### Type generation\n\nIn [tsconfig.json](tsconfig.json), the following options instructs `tsc` to emit declaration (`.d.ts` files) only, as `vite build` handles the `.js` file generation. The generated `.d.ts` files are sent to `dist/types` folder.\n\n```json\n\"compilerOptions\": {\n  \"declaration\": true,\n  \"emitDeclarationOnly\": true,\n  \"declarationDir\": \"./dist/types\"\n}\n```\n\nIn [package.json](package.json), the line below locates the generated types for library client.\n\n```json\n\"types\": \"./dist/types/index.d.ts\",\n```\n\n> In [vite.config.ts](vite.config.ts), `build.emptyOutDir` is set to `false` and `rimraf` is used instead to remove the `dist` folder before the build. This is to avoid the `dist/types` folder generated by `tsc` being deleted when running `vite build`.\n\n### Configuration\n\n#### TypeScript\n\nIn [tsconfig.json](tsconfig.js), set the following as recommended by Vite (since esbuild is used). However, enableing this option leads to https://github.com/vitejs/vite/issues/5814. The workaround is to also enable `compilerOptions.skipLibCheck`.\n\n```json\n\"compilerOptions\": {\n  \"isolatedModules\": true\n}\n```\n\nIn [tsconfig.json](tsconfig.js), set the following to address [Issue #32](https://github.com/wuruoyun/vue-component-lib-starter/issues/32). The solution is from https://github.com/johnsoncodehk/volar/discussions/592.\n\n```json\n\"compilerOptions\": {\n  \"types\": [\n    \"vite/client\"\n  ]\n}\n```\n"
  },
  {
    "path": "docs/.vitepress/components/DemoContainer.vue",
    "content": "<template>\n  <div class=\"demo-container\">\n    <slot></slot>\n  </div>\n</template>"
  },
  {
    "path": "docs/.vitepress/config.js",
    "content": "const path = require('path')\n\nmodule.exports = {\n  title: 'My Lib',\n  description: 'Just playing around.',\n  themeConfig: {\n    repo: 'https://github.com/wuruoyun/vue-component-lib-starter',\n    sidebar: [\n      {\n        text: 'Introduction',\n        children: [\n          { text: 'What is My Lib?', link: '/' },\n          { text: 'Getting Started', link: '/guide/' },\n        ],\n      }, {\n        text: 'Components',\n        children: [\n          { text: 'Component A', link: '/components/component-a' },\n          { text: 'Component B', link: '/components/component-b' },\n        ],\n      }\n    ],\n  },\n  vite: {\n    resolve: {\n      alias: {\n        'my-lib': path.resolve(__dirname, '../../src'),\n      },\n      dedupe: ['vue', /primevue\\/.+/], // avoid error when using dependencies that also use Vue\n    }\n  }\n}\n"
  },
  {
    "path": "docs/.vitepress/theme/custom.css",
    "content": ".demo-container {\n  border: 1px solid lightgrey;\n  border-radius: 5px;\n  padding: 10px 20px;\n}"
  },
  {
    "path": "docs/.vitepress/theme/index.js",
    "content": "import DefaultTheme from 'vitepress/theme'\nimport PrimeVue from 'primevue/config'\nimport DemoContainer from '../components/DemoContainer.vue'\nimport MyLib from 'my-lib'\n\nimport 'primevue/resources/themes/saga-blue/theme.css'       //theme\nimport 'primevue/resources/primevue.min.css'                 //core css\nimport 'primeicons/primeicons.css'\nimport 'primeflex/primeflex.css'\n\nimport './custom.css'\n\nexport default {\n  ...DefaultTheme,\n  enhanceApp({ app }) {\n    app.use(PrimeVue)\n    app.use(MyLib)\n    app.component('DemoContainer', DemoContainer)\n  }\n}\n"
  },
  {
    "path": "docs/components/component-a.md",
    "content": "<script setup>\nimport Basic from './demo/ComponentA/Basic.vue'\n</script>\n\n# Component A\n\nThis is a simple Hello Word component with a prop and use icon <span class=\"icon-heart\" style=\"color:tomato\"></span> from a font file.\n\n## Example Usage\n\nYou may show demo below with the code snippet.\n\n<DemoContainer>\n  <Basic/>\n</DemoContainer>\n\n<<< @/components/demo/ComponentA/Basic.vue\n\n## Reference\n\nYou may show props, slots, events, methods, etc. using Markdown.\n\n### Properties\n\n| Name | Type   | Default | Description    |\n| ---- | ------ | ------- | -------------- |\n| msg  | string | null    | Messge to show |\n\n### Events\n\n| Name | Parameters | Description |\n| ---- | ---------- | ----------- |\n|      |            |             |\n\n### Slots\n\n| Name | Parameters | Description |\n| ---- | ---------- | ----------- |\n|      |            |             |\n"
  },
  {
    "path": "docs/components/component-b.md",
    "content": "<script setup>\nimport Basic from './demo/ComponentB/Basic.vue'\n</script>\n# Component B\n\nThis component depends on third-party component library for Vue 3:\n\n* Component (button) from [PrimeVue](https://www.primefaces.org/primevue/)\n* CSS from [PrimeIcons](https://www.primefaces.org/showcase/icons.xhtml) and [PrimeFlex](https://www.primefaces.org/primeflex/)\n\n## Example Usage\n\nClick the buttons to change the count.\n\n<DemoContainer>\n  <Basic/>\n</DemoContainer>\n\n<<< @/components/demo/ComponentB/Basic.vue\n\n## Reference\nYou may show props, slots, events, methods, etc. using Markdown.\n\n### Properties\n\n| Name        | Type     | Default  | Description     |\n| ----------- | -------- | -------- | --------------- |\n|             |          |          |                 |\n\n### Events\n\n| Name        | Parameters   | Description     |\n| ----------- | ----------   | --------------- |\n|             |              |                 |\n\n### Slots\n\n| Name        | Parameters   | Description     |\n| ----------- | ----------   | --------------- |\n|             |              |                 |\n"
  },
  {
    "path": "docs/components/demo/ComponentA/Basic.vue",
    "content": "<template>\n  <ComponentA msg=\"World\"/>\n</template>"
  },
  {
    "path": "docs/components/demo/ComponentB/Basic.vue",
    "content": "<template>\n  <ComponentB/>\n</template>\n"
  },
  {
    "path": "docs/components/index.md",
    "content": "# Components\n\nYou may add a summary of the components here."
  },
  {
    "path": "docs/guide/index.md",
    "content": "# Getting Started\n\nYou may add the usage of the library here.\n\n> If you remove Component B and the [PrimeFaces](https://www.primefaces.org/) (PrimeVue, PrimeIcons and PrimeFlex) dependencies from your library, the setup related to PrimeFaces won't be needed from the guide below.\n\n## Setup\n\nThis setup assumes your client app is created with Vite and vue-ts template, and you use 'npm link' to link to `my-lib` locally.\n\nIn your `package.json`, you shall have the dependencies compatible with the following:\n\n```json\n\"dependencies\": {\n  \"primeflex\": \"^3.1.2\",\n  \"primeicons\": \"^5.0.0\",\n  \"primevue\": \"^3.11.1\",\n  \"vue\": \"^3.2.25\"\n}\n```\n\nIn your `vite.config.ts`, you shall configure to dedupe `vue`:\n\n```ts\nexport default defineConfig({\n  resolve: {\n    dedupe: ['vue'],\n  },\n});\n```\n\nIn your `main.ts`, you shall import the libraries and CSS:\n\n```ts\nimport 'primevue/resources/themes/saga-blue/theme.css';\nimport 'primevue/resources/primevue.min.css';\nimport 'primeicons/primeicons.css';\nimport 'primeflex/primeflex.css';\n\nimport 'my-lib/dist/style.css';\n```\n\nImport components from this library in your own component:\n\n```html\n<script setup lang=\"ts\">\n  import { ComponentA, ComponentB } from 'my-lib';\n</script>\n```\n"
  },
  {
    "path": "docs/index.md",
    "content": "# What is My Lib?\n\nThis library is a starter to create your own Vue 3 component library in TypeScript.\n\nThere are two example components included for you to get started with creating your own components:\n\n- [ComponentA](/components/component-a) is a simple Hello World component with icon from bundled font file.\n- [ComponentB](/components/component-b) is a counter component using button from [PrimeVue](https://www.primefaces.org/primevue/), icons from [PrimeIcons](https://www.primefaces.org/showcase/icons.xhtml) and styling from [PrimeFlex](https://www.primefaces.org/primeflex/).\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"my-lib\",\n  \"version\": \"0.0.0\",\n  \"scripts\": {\n    \"build\": \"rimraf dist && vue-tsc && vite build\",\n    \"docs:dev\": \"vitepress dev docs\",\n    \"docs:build\": \"vitepress build docs\",\n    \"docs:serve\": \"vitepress serve docs\"\n  },\n  \"peerDependencies\": {\n    \"primeflex\": \"^3.3.1\",\n    \"primeicons\": \"^6.0.1\",\n    \"primevue\": \"^3.29.2\",\n    \"vue\": \"^3.3.4\"\n  },\n  \"devDependencies\": {\n    \"@types/node\": \"^20.3.3\",\n    \"@vitejs/plugin-vue\": \"^4.2.3\",\n    \"rimraf\": \"^5.0.1\",\n    \"sass\": \"^1.63.6\",\n    \"typescript\": \"^5.1.6\",\n    \"vite\": \"^4.3.9\",\n    \"vitepress\": \"^0.22.4\",\n    \"vue-tsc\": \"^1.8.3\"\n  },\n  \"files\": [\n    \"src\",\n    \"dist\"\n  ],\n  \"module\": \"./dist/my-lib.es.js\",\n  \"exports\": {\n    \".\": {\n      \"types\": \"./dist/types/index.d.ts\",\n      \"import\": \"./dist/my-lib.es.js\"\n    },\n    \"./dist/style.css\": \"./dist/style.css\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/wuruoyun/vue-component-lib-starter.git\"\n  }\n}\n"
  },
  {
    "path": "src/assets/main.scss",
    "content": "@font-face {\n  font-family: 'myfont';\n  src: url('fonts/myfont.woff');\n}\n\n[class^='icon-'],\n[class*=' icon-'] {\n  font-family: 'myfont' !important;\n}\n\n.icon-heart:before {\n  content: '\\e9da';\n}\n\n.global-example {\n  color: red;\n}\n"
  },
  {
    "path": "src/components/ComponentA.vue",
    "content": "<script setup lang=\"ts\">\ninterface Props {\n  msg: string\n}\n\nconst props = defineProps<Props>()\n</script>\n\n<template>\n  <div>\n    Hello {{ msg }}! <span class=\"icon-heart\" style=\"color:tomato\"></span>\n  </div>\n</template>\n"
  },
  {
    "path": "src/components/ComponentB.vue",
    "content": "<script setup lang=\"ts\">\nimport { ref } from 'vue'\nimport Button from 'primevue/button'\nconst count = ref(0)\n</script>\n\n<template>\n  <div class=\"flex align-content-center flex-wrap counter\">\n    <label class=\"flex align-items-center justify-content-center\">Counter:</label>\n    <Button icon=\"pi pi-plus\" class=\"p-button-sm flex align-items-center justify-content-center\" @click=\"count++\"></Button>\n    <span class=\"flex align-items-center justify-content-center count\">{{ count }}</span>\n    <Button icon=\"pi pi-minus\" class=\"p-button-sm flex align-items-center justify-content-center\" @click=\"count--\"></Button>\n  </div>\n</template>\n\n<style lang=\"scss\" scoped>\n.counter {\n  label {\n    padding-right: 10px;\n    font-weight: bold;\n  }\n  .count {\n  padding: 0 10px;\n}\n}\n</style>\n"
  },
  {
    "path": "src/components/index.ts",
    "content": "import ComponentA from './ComponentA.vue'\nimport ComponentB from './ComponentB.vue'\n\nexport {\n  ComponentA,\n  ComponentB\n}\n"
  },
  {
    "path": "src/constants/MyConstants.ts",
    "content": "export const MAGIC_NUM = 100\n"
  },
  {
    "path": "src/constants/index.ts",
    "content": "import * as MyConstants from './MyConstants'\n\nexport {\n  MyConstants\n}\n"
  },
  {
    "path": "src/env.d.ts",
    "content": "/// <reference types=\"vite/client\" />\n\ndeclare module '*.vue' {\n  import { DefineComponent } from 'vue'\n  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types\n  const component: DefineComponent<{}, {}, any>\n  export default component\n}\n"
  },
  {
    "path": "src/index.ts",
    "content": "import { App } from 'vue'\nimport * as components from './components'\n\nfunction install (app: App) {\n  for (const key in components) {\n    // @ts-expect-error\n    app.component(key, components[key])\n  }\n}\n\nimport './assets/main.scss'\n\nexport default { install }\n\nexport * from './components'\nexport * from './constants'\nexport * from './utils'\n"
  },
  {
    "path": "src/utils/MyUtil.ts",
    "content": "function add(a: number, b: number): number {\n  return a + b;\n}\n\nexport default {\n  add,\n};\n"
  },
  {
    "path": "src/utils/index.ts",
    "content": "import MyUtil from './MyUtil'\n\nexport {\n  MyUtil\n}\n"
  },
  {
    "path": "tsconfig.json",
    "content": "{\n  \"compilerOptions\": {\n    \"target\": \"esnext\",\n    \"useDefineForClassFields\": true,\n    \"module\": \"esnext\",\n    \"moduleResolution\": \"node\",\n    \"strict\": true,\n    \"jsx\": \"preserve\",\n    \"sourceMap\": true,\n    \"resolveJsonModule\": true,\n    \"esModuleInterop\": true,\n    \"lib\": [\"esnext\", \"dom\"],\n    \"declaration\": true,\n    \"emitDeclarationOnly\": true,\n    \"declarationDir\": \"./dist/types\",\n    \"isolatedModules\": true,\n    \"skipLibCheck\": true,\n    \"types\": [\n      \"vite/client\"\n    ]\n  },\n  \"include\": [\"src/**/*.ts\", \"src/**/*.d.ts\", \"src/**/*.tsx\", \"src/**/*.vue\", \"src/index.js\"]\n}\n"
  },
  {
    "path": "vite.config.ts",
    "content": "const path = require('path');\nconst { defineConfig } = require('vite');\nimport vue from '@vitejs/plugin-vue';\n\nmodule.exports = defineConfig({\n  plugins: [vue()], // to process SFC\n  build: {\n    lib: {\n      entry: path.resolve(__dirname, 'src/index.ts'),\n      name: 'my-lib',\n      formats: ['es'], // adding 'umd' requires globals set to every external module\n      fileName: (format) => `my-lib.${format}.js`,\n    },\n    rollupOptions: {\n      // external modules won't be bundled into your library\n      external: ['vue', /primevue\\/.+/], // not every external has a global\n      output: {\n        // disable warning on src/index.ts using both default and named export\n        exports: 'named',\n        // Provide global variables to use in the UMD build\n        // for externalized deps (not useful if 'umd' is not in lib.formats)\n        globals: {\n          vue: 'Vue',\n        },\n      },\n    },\n    emptyOutDir: false, // to retain the types folder generated by tsc\n  },\n});\n"
  }
]