Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to publish a library of Vue.js components?

I am working on a project containing a Vuex module and an abstract components that users can extend from.

I would love to publish this on NPM to clean up my codebase and pull this away from my project as a solid well tested module. I have specified the main file in package.json to load an index which imports everything I want to expose:

https://github.com/stephan-v/vue-search-filters/

The index contains this at the moment:

import AbstractFilter from './src/components/filters/abstract/AbstractFilter.vue';
import Search from './src/store/modules/search';

module.exports = {
    AbstractFilter,
    Search
};

For this to work I need to transpile this since a babel compiler normally won't transpile files imported from node_modules(Correct me if I am wrong here). Besides that I would probably be a good idea to do this so it can be used by different systems.

How do I transpile only the files that I need though with Webpack? Do I have to create a separate config for this?

What does a config like that look like? I know the vue-cli has a build command for one single file component but this is a bit different.

Any tips or suggestions on how to transpile something like this are welcome.

Edit

This seems like a good start as well:

https://github.com/Akryum/vue-share-components

The most import thing for Webpack users to notice is that you need to transpile your files in UMD which can be set by:

libraryTarget: 'umd'

This will make sure your are transpiling for Universal Module Definition, meaning your code will work in different environments like AMD,CommonJS, as a simple script tag, etc.

Besides that it is import to provide the externals property in webpack:

externals: {}

Here you can define which libraries your project users but should not be built into your dist file. For example you don't want the Vue library to be compiled / transpiled into the source of your NPM package.

I will research a bit more, so far the best options looks like to create a custom project myself If I want flexibility and unit testing.

Webpack docs

This is also a useful page which goes in depth about how to publish something with Webpack:

https://webpack.js.org/guides/author-libraries/#add-librarytarget

like image 390
Stephan-v Avatar asked Jul 25 '17 09:07

Stephan-v


1 Answers

The best way probably will be to build the module and set main in your package.json to my_dist/my_index.js. Otherwise every project that will use your module will have to add it to include which is tedious.

You will also want your webpack build to follow UMD (Universal Module Definition). For that you must set libraryTarget to umd:

...
output: {
  filename: 'index.js',
  library:'my_lib_name',
  libraryTarget: 'umd'
},
...

Also a good thing will be to add Vue to externals so that you didn't pack extra 200kb of vue library.

externals: {
  vue: 'vue'
},
resolve: {
  alias: {
    'vue$': 'vue/dist/vue.esm.js'
  }
}

And add it to peerDependencies in package.json:

...
"peerDependencies": {
  "vue": "^2.0.0"
},
"devDependencies": {
  "vue": "^2.0.0"
}
...

If you need an existing example of how to pack a vue.js component, you can take a look in one of the modules I maintain:

https://github.com/euvl/vue-js-popover

Particularly webpack.config.js and package.json will be interesting for you.

like image 85
euvl Avatar answered Sep 23 '22 17:09

euvl