You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/single-file-component.md
+104Lines changed: 104 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -65,3 +65,107 @@ After you've taken a day to dive into these resources, we recommend checking out
65
65
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/).
66
66
67
67
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:
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
+
importcommonjsfrom'rollup-plugin-commonjs'
96
+
importVuePluginfrom'rollup-plugin-vue'
97
+
importpkgfrom'./package.json'// import our package.json file to re-use the naming
98
+
99
+
exportdefault {
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`
0 commit comments