How to build a es6 vue-component library using vue-class-component
Asked Answered
P

0

9

I am trying to create a vuejs component library in typescript with vue-class-component and vue-property-decorator with the following requirements:

  • The library components should be written in class-style typescript.
  • The library components should be exported as es6 modules to allow treeshaking in the project.

For The library I used the latest version of @vue/cli to scaffold the project. Within that library-project the components are working just fine.

The Application on the other hand is using a webpack-setup and in here I want to use the vue-components.

Usually when I want to build a typescript based library in es6, I use tsc to transpile the ts-files to es6 js-files. But following that approach the components do not know the templates. Is there a way to combine tsc with vue-template-compiler?

I found a another way to make this work, but I'm not satisfied at all by that approach:

I used "vue-cli-service build --target lib --name vue-lib ./src/index.ts" This way the vue-cli-service will create a umd-bundle and commonjs-bundle as stated in the docs here. In the project I can use the component with an //@ts-ignore statement above. So I lose the declarations and the components are not in es6 syntax :/

To get a better understanding of the Issue I have, I've created a minimal example on github that you can find here:

Library: https://github.com/Sh4bbY/vue-lib

Application: https://github.com/Sh4bbY/vue-project

Pelag answered 4/7, 2018 at 11:53 Comment(5)
Hi have you figured out how to build a component library with typescript?Console
This is still not supported out of the box. See here for possible workarounds: github.com/vuejs/vue-cli/issues/1081.Fumed
Hi I'm just wondering does this persist in Q3 2020? I am wanting to do a similar thingCoercive
For me the way to go was to not compile the components within the library. Instead i've just placed my .vue files in the library. import them from the project. and let the project deal with the compilation as is does with project-own components. This approach works pretty well for me far.Pelag
I've made a library of vue component in TS with Vue Class Component, and I use Rollup (rollupjs.org) to build an ESM version of the library. Then I can import any component in my project (also in TS Vue Class Component)Resignation

© 2022 - 2024 — McMap. All rights reserved.