Skip to content

Commit d451d21

Browse files
Add bundling with rollup to docs (#683)
* docs: add rollup to single-file-component.md * docs: integrate reviewer feedback * docs: styling and fixes * docs: reword introduction paragraph Co-authored-by: skirtle <65301168+skirtles-code@users.noreply.github.com> * docs: update to only use `we/our` Co-authored-by: skirtle <65301168+skirtles-code@users.noreply.github.com>
1 parent 367358b commit d451d21

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

src/guide/single-file-component.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,107 @@ After you've taken a day to dive into these resources, we recommend checking out
6565
The CLI takes care of most of the tooling configurations for you, but also allows fine-grained customization through its own [config options](https://cli.vuejs.org/config/).
6666

6767
In case you prefer setting up your own build setup from scratch, you will need to manually configure webpack with [vue-loader](https://vue-loader.vuejs.org). To learn more about webpack itself, check out [their official docs](https://webpack.js.org/configuration/) and [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

Comments
 (0)