|
65 | 65 | CLI はツールの設定の大部分の面倒を見てくれますが、[設定オプション](https://cli.vuejs.org/config/)を通してよりきめ細かいカスタマイズをすることもできます。
|
66 | 66 |
|
67 | 67 | あなたが独自のビルドセットアップをゼロから作ることを好む場合、webpack と [vue-loader](https://vue-loader.vuejs.org)を手動で設定する必要があるでしょう。webpack についてもっと学びたいときは、[公式ドキュメント](https://webpack.js.org/configuration/)や [webpack learning academy](https://webpack.academy/p/the-core-concepts)を参照してください。
|
| 68 | + |
| 69 | +### Building with rollup |
| 70 | + |
| 71 | +Most of the time when developing a third-party library we want to build it in a way that allows the consumers of the library to [tree shake](https://webpack.js.org/guides/tree-shaking/) it. To enable tree-shaking we need to build `esm` modules. Since webpack and, in turn, vue-cli do not support building `esm` modules we need to rely on [rollup](https://rollupjs.org/). |
| 72 | + |
| 73 | +#### Installing Rollup |
| 74 | + |
| 75 | +We will need to install Rollup and a few dependencies: |
| 76 | + |
| 77 | +```bash |
| 78 | +npm install --save-dev rollup @rollup/plugin-commonjs rollup-plugin-vue |
| 79 | +``` |
| 80 | + |
| 81 | +These are the minimal amount of rollup plugins that we need to use to compile the code in an `esm` module. We may want to also add [rollup-plugin-babel](https://github.com/rollup/plugins/tree/master/packages/babel) to transpile their code and [node-resolve](https://github.com/rollup/plugins/tree/master/packages/node-resolve) if we use dependencies that we want to bundle with our library. |
| 82 | + |
| 83 | +#### Configuring Rollup |
| 84 | + |
| 85 | +To configure our build with Rollup we will need to create a `rollup.config.js` file in the root of our project: |
| 86 | + |
| 87 | +```bash |
| 88 | +touch rollup.config.js |
| 89 | +``` |
| 90 | + |
| 91 | +Once the file is created we will need to open it with our editor of choice and add the following code. |
| 92 | + |
| 93 | +```javascript |
| 94 | +// import our third party plugins |
| 95 | +import commonjs from 'rollup-plugin-commonjs' |
| 96 | +import VuePlugin from 'rollup-plugin-vue' |
| 97 | +import pkg from './package.json' // import our package.json file to re-use the naming |
| 98 | + |
| 99 | +export default { |
| 100 | + // this is the file containing all our exported components/functions |
| 101 | + input: 'src/index.js', |
| 102 | + // this is an array of outputed formats |
| 103 | + output: [ |
| 104 | + { |
| 105 | + file: pkg.module, // the name of our esm librry |
| 106 | + format: 'esm', // the format of choice |
| 107 | + sourcemap: true, // ask rollup to include sourcemaps |
| 108 | + } |
| 109 | + ], |
| 110 | + // this is an array of the plugins that we are including |
| 111 | + plugins: [ |
| 112 | + commonjs(), |
| 113 | + VuePlugin() |
| 114 | + ], |
| 115 | + // ask rollup to not bundle Vue in the library |
| 116 | + external: ['vue'] |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +#### Configuring package.json |
| 121 | + |
| 122 | +To take advantage of our newly created `esm` module we need to add a few fields in our `package.json` file: |
| 123 | + |
| 124 | +```json |
| 125 | + "scripts": { |
| 126 | + ... |
| 127 | + "build": "rollup -c rollup.config.js", |
| 128 | + ... |
| 129 | + }, |
| 130 | + "module": "dist/my-library-name.esm.js", |
| 131 | + "files": [ |
| 132 | + "dist/", |
| 133 | + ], |
| 134 | + ``` |
| 135 | + |
| 136 | +Here we are specifying: |
| 137 | + |
| 138 | +- how to build our package |
| 139 | +- what files we want to bundle in our package |
| 140 | +- what file represents our `esm` module |
| 141 | + |
| 142 | +#### Bundling `umd` and `cjs` modules |
| 143 | + |
| 144 | +To also build `umd` and `cjs` modules we can simply add a few lines of configuration to our `rollup.config.js` and `package.json` |
| 145 | + |
| 146 | +##### rollup.config.js |
| 147 | +```javascript |
| 148 | +output: [ |
| 149 | + ... |
| 150 | + { |
| 151 | + file: pkg.main, |
| 152 | + format: 'cjs', |
| 153 | + sourcemap: true, |
| 154 | + }, |
| 155 | + { |
| 156 | + file: pkg.unpkg, |
| 157 | + format: 'umd', |
| 158 | + name: 'MyLibraryName', |
| 159 | + sourcemap: true, |
| 160 | + globals: { |
| 161 | + vue: 'Vue', |
| 162 | + }, |
| 163 | + }, |
| 164 | +] |
| 165 | +``` |
| 166 | +##### package.json |
| 167 | +```json |
| 168 | +"module": "dist/my-library-name.esm.js", |
| 169 | +"main": "dist/my-library-name.cjs.js", |
| 170 | +"unpkg": "dist/my-library-name.global.js", |
| 171 | +``` |
0 commit comments