0.5.0 • Published 3 years ago

@angelioputra/midnight v0.5.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Netlify Status

Vue Component Library Starter

Create your own component library with Vue CLI 3 and VuePress.

Sooner 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 you remove app specific logic from your components, makes it possible to reuse them in other apps.

Once the components are in a libraray, documentation becomes critical. This starter project includes a documentation app powered by VuePress. 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.

Dev dependency "@vue/babel-preset-app": "^4.1.1" is introduced as Vue CLI v4 is using core-js v3.x while vuepress 1.2.0 is still using core-js v2.x.

Setup

# install dependencies
npm install

# start the doc app with hot reload, great for testing components
npm run docs:dev

# build the library, available under dist
npm run build

# build the doc app, available under docs/.vuepress/dist
npm run docs:build

How it works

Components

The library is a Vue plugin. Its install function in install.js imports all components from components folder, registers them to Vue and automatically call itself.

Mixins, Utils and Constants

Besides the install function, index.js may also exports mixins, utils and constants. The client may use them as below:

<script>
import { MyMixin, MyConstants, MyUtil } from 'my-lib'

export default {
  mixins: [MyMixin],
  data () {
    return {
      magicNum: MyConstants.MAGIC_NUM
    }
  },
  methods: {
    add (a, b) {
      return MyUtil.add(a, b)
    }
  }
}
</script>

Third-party libraries

Third-party libraries you library depends on bloats the size of your library, if not handled well.

Externalize

One strategy is to make it external. As an example, the popular library moment is used by ComponentA. Since it is very likely the client of your library may also use this library, we configure CLI not to include it to the bundle by adding the following in vue.config.js.

module.exports = {
  //...
  chainWebpack: config => {
    config.externals({
      moment: 'moment'
    })
  }
}

In your client app, you don't need to explicitly add dependency to moment in package.json as it is a dependency of my-lib. However, if you want to reduce the size of the bundle size of client app, add the following in the vue.config.js of client app (details), assuming it is also built with Vue CLI .

const webpack = require('webpack')
module.exports = {
  //...
  plugins: [
    // Ignore all locale files of moment.js
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  ],
}

Cherry picking

Another strategy is to embed cherry-picked functions with tree shaking. As an example, the fill function of popular library lodash is used by ComponentA.

To get the tree shaking working, import the fill function like the following. Note that import { fill } from 'lodash' or import _ from 'lodash' will not work and will embed the whole lodash library.

import fill from 'lodash/fill'

If your client app also use lodash and you don't want lodash to be in both the client app and the component 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.

Use your component library

You may publish your component library to NPM repository. If you prefer to use/test your component library locally in a client app, you may use npm link or install-local.

If your app is not using a bundler, the following is the example of how to use the library in vanilla HTML page.

<!DOCTYPE html>
<html>
  <head><title>Demo app</title></head>
  <body>
    <div id="app">
      <p>Component A: <component-a/></p>
      <p>Component B: <component-b @click="onClick"/></p>
    </div>
  </body>

  <script src="https://unpkg.com/vue"></script>
  <script src="dist/my-lib.umd.js"></script>
  <script>
      console.log(window['my-lib'])
      var app = new Vue({
        el: '#app',
        methods: {
          onClick (message) {
            alert(message)
          }
        }
      })
    </script>
</html>
0.5.0

3 years ago

0.4.0

3 years ago

0.0.0

3 years ago

0.3.16

3 years ago

0.3.15

3 years ago

0.3.14

3 years ago

0.3.13

3 years ago

0.3.11

3 years ago

0.3.10

3 years ago

0.3.9

3 years ago

0.3.8

3 years ago

0.3.6

3 years ago

0.3.7

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.0

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.1

3 years ago