4

I want create my component library based on Vuetify and publish on npm.

Vuetify has already vue-plugin standard installation and use vuetify-loader, I think was a more complex scenario than plain HTML component.

For example, I want create my Login Form, my Article Page, my default calendar picker with preset values.

There's a standard way or guide to do this or a sample to do that?

I use last version of vuetify (2.0.7)

7
  • vuejs.org/v2/guide/components-registration.html
    – Wils
    Commented Aug 21, 2019 at 4:53
  • vuetify is just a framework, you need to include it in main js and you can use vue component template to create your own.
    – Wils
    Commented Aug 21, 2019 at 4:53
  • @menteora Did you ever find a solution for this? I'm stuck at the project setup for the consuming package.
    – Johannes
    Commented Nov 7, 2019 at 8:45
  • 2
    Yes, @Johannes. All my problem was related to npm pack. See my repository: github.com/menteora/vuetify-extra I currenty use it
    – menteora
    Commented Nov 8, 2019 at 9:12
  • 2
    @menteora I am about to start on a similar project, and started thinking if I would run into issues since the components I am building will be using vuetify, which means they will need to include a v-app component for everything to work properly. If this library is then imported into another project that is using vuetify, won't that cause an issue since there will be 2 v-app components? Or if you develop a component library using vuetify can it only be imported into another project based on vuetify, which would mean you don't need to include v-app in the component library.
    – flyingL123
    Commented Jun 4, 2020 at 20:24

1 Answer 1

4

I just got it working for Vue3/Vuetify3. In a nutshell (using pnpm, vite, typescript, Vue plugin):

Create the component as a new project:

pnpm create vue@latest
-> your-plugin
-> Typescript
-> ESLint
cd <project>
echo auto-install-peers = true > .npmrc
pnpm add -D vuetify rollup-plugin-typescript2

Then remove all the components and make your own component instead. Create src\components\index.ts and src\YourPlugin.ts

src\components\index.ts

export {default as YourComponent} from "./YourComponent.vue"

src\YourPlugin.ts

import type { App } from "vue"
import { YourComponent } from "./components"
export default {
    install: (app: App) => {
        app.component("YourComponent", YourComponent)
    }
};

vite.config.ts

Add to the imports:

import vuetify from 'vite-plugin-vuetify'
import typeScript2 from "rollup-plugin-typescript2"

Add to the plugins:

vuetify({
  autoImport: true,
}),
typeScript2({
  check: false,
  include: ["src/components/*.vue"],
  tsconfigOverride: {
    compilerOptions: {
      sourceMap: true,
      declaration: true,
      declarationMap: true,
    }
  },
  exclude: [
    "vite.config.ts"
  ]
})

Add a new section build to the defineConfig:

  build: {
    cssCodeSplit: false,
    lib: {
      entry: "./src/YourPlugin.ts",
      formats: ["es", "cjs"],
      name: "CommonVtfyPlugin",
      fileName: format => (format == "es" ? "index.js" : "index.cjs"),
    },
    rollupOptions: {
      external: ["vue", "vuetify/lib"],
      output: {
        globals: {
          vue: "Vue",
          vuetify: "Vuetify",
            'vuetify/components': 'VuetifyComponents',
            'vuetify/directives': 'VuetifyDirectives'
        }
      }
    }
  },

dist\index.d.ts

I have not figured out how to generate this on yet. But this is a generic stand in:

declare const _default: any;
export default _default;

package.json

Add this:

  "type": "module",
  "exports": {
    ".": "./dist/index.js"
  },
  "types": "./dist/index.d.ts",
  "files": [
    "dist"
  ],

You can use it in any Vue project by importing it as a plugin:

import YourComponent from 'your-plugin'
app.use(YourComponent)

No guarantees on how optimized that is (feedback welcome).. but it works (tm).. A more detailed answer can be found pnpm monorepo: how to set up a simple reusable (vue) component for reuse? (any updates will primarily be updated in that answer as well, if any)

2
  • I've got rollup-plugin-typescript2 setup in my project like you do, and it generates a .d.ts file for my component, but the file exports and imports from my computer's directory...which doesn't seem right. So it looks something like dist/MyComponent.vue.d.ts export \* from "C:/projects/vue-common-components/src/components/MyComponent.vue?vue&type=script&lang.ts:; import "C:/projects/vue-common-components/src/components/MyComponent.vue?vue&type=style&index=0&scoped=d3028399&lang.css"; Commented Mar 17 at 17:27
  • As this is written you're only telling the typescript2 plugin to generate declarations files for files in the 'components' directory that end in .'vue'. If you want one generated for "src/index.ts" you need to add that to the "includes" option in the typescript2 config. So instead of typescript2({check: false, include: ['src/components/**/*.vue'], You would have typescript2({check: false, include: ['src/components/**/*.vue', 'src/index.ts'], Though if you have more than just that ts file that needs declarations you'd want src/**/*.ts instead Commented Apr 2 at 16:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.