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/content/guides/author-libraries.md
+62-18Lines changed: 62 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,7 @@ contributors:
6
6
- johnstew
7
7
- simon04
8
8
- 5angel
9
+
- marioacc
9
10
---
10
11
11
12
webpack is a tool which can be used to bundle application code and also to bundle library code. If you are the author of a JavaScript library and are looking to streamline your bundle strategy then this document will help you with the differents webpack configurations to expose your libraries as you think convenient.
@@ -14,7 +15,24 @@ webpack is a tool which can be used to bundle application code and also to bundl
14
15
## Authoring a Library
15
16
16
17
Let's assume that you are writing a small library ,`webpack-numbers`, allowing to convert numbers 1 to 5 from their numeric representation to the textual one and vice-versa, e.g.: 2 to 'two'.
17
-
The implementation makes use of ES2015 modules, and might look like this:
18
+
The basic project structure may look like this.
19
+
20
+
__project__
21
+
22
+
```diff
23
+
+ |webpack.config.js
24
+
+ |- package.json
25
+
+ |- /src
26
+
+ |- index.js
27
+
+ |- ref.json
28
+
```
29
+
30
+
Initialize npm, install webpack and lodash
31
+
```bash
32
+
33
+
npm init -y
34
+
npm install --save-dev webpack lodash
35
+
```
18
36
19
37
__src/ref.json__
20
38
```javascript
@@ -101,18 +119,23 @@ For full library configuration and code please refer to [webpack-library-example
101
119
102
120
## Configure webpack
103
121
104
-
Now the agenda is to bundle this library
122
+
Now the agenda is to bundle this library achieving the next goals:
123
+
124
+
- Without bundling `lodash`, but requiring it to be loaded by the consumer using `externals`.
125
+
- Setting the library name as `webpack-numbers`.
126
+
- Exposing the library as a variable called `webpackNumbers`.
127
+
- Being able to access the library inside Node.js.s
128
+
129
+
Also, the consumer will be able to access` the library the `next ways:
130
+
- ES2015 module. i.e. `import webpackNumbers from 'webpack-numbers'`.
131
+
- CommonJS module. i.e. `require('webpack-numbers')`.
132
+
- Global variable when included through `script` tag.
105
133
106
-
- Without bundling `lodash` but requiring it to be loaded by the consumer.
107
-
- Name of the library is `webpack-numbers` and the variable is `webpackNumbers`.
108
-
- Library can be imported as `import webpackNumbers from 'webpack-numbers'` or `require('webpack-numbers')`.
109
-
- Library can be accessed through global variable `webpackNumbers` when included through `script` tag.
110
-
- Library can be accessed inside Node.js.
111
134
112
135
113
136
### Add webpack
114
137
115
-
Add basic webpack configuration.
138
+
Add this basic webpack configuration to bundle the library.
116
139
117
140
__webpack.config.js__
118
141
@@ -129,15 +152,14 @@ module.exports = {
129
152
130
153
```
131
154
132
-
This adds basic configuration to bundle the library.
133
155
134
156
135
157
### Add `externals`
136
158
137
159
Now, if you run `webpack`, you will find that a largish bundle file is created. If you inspect the file, you will find that lodash has been bundled along with your code.
138
160
It would be unnecessary for your library to bundle a library like `lodash`. Hence you would want to give up control of this external library to the consumer of your library.
139
161
140
-
This can be done using the `externals` configuration as
162
+
This can be done using the `externals` configuration as:
141
163
142
164
__webpack.config.js__
143
165
@@ -158,7 +180,7 @@ module.exports = {
158
180
159
181
This means that your library expects a dependency named `lodash` to be available in the consumer's environment.
160
182
161
-
If you only plan on using your library as a dependency in another webpack bundle, you may specify externals as an array.
183
+
However, if you only plan on using your library as a dependency in another webpack bundle, you may specify externals as an array.
162
184
163
185
```javascript
164
186
module.exports= {
@@ -171,7 +193,7 @@ module.exports = {
171
193
};
172
194
```
173
195
174
-
Please note: for bundles that use several files from a package like this
196
+
Please note: for bundles that use several files from a package like this:
175
197
176
198
```javascript
177
199
importAfrom'library/A';
@@ -195,11 +217,27 @@ module.exports = {
195
217
};
196
218
```
197
219
220
+
W>At the moment of webpack 3.5.5, using the next configuration is not working properly as stated in the [issue 4824](https://github.com/webpack/webpack/issues/4824):
221
+
```javascript
222
+
module.exports= {
223
+
...
224
+
output: {
225
+
...
226
+
227
+
libraryTarget: {
228
+
root:'_'
229
+
}
230
+
}
231
+
...
232
+
};
233
+
```
234
+
235
+
198
236
### Add `libraryTarget`
199
237
200
-
For widespread use of the library, we would like it to be compatible in different environments, i.e. CommonJS, AMD, Node.js and as a global variable.
238
+
For widespread use of the library, we would like it to be compatible in different environments, i.e. CommonJS, AMD, Node.js and as a global variable.
201
239
202
-
To make your library available for reuse, add `library` property in webpack configuration.
240
+
To make your library available for reuse, add `library` property inside `output`in webpack configuration.
203
241
204
242
__webpack.config.js__
205
243
@@ -214,7 +252,7 @@ module.exports = {
214
252
};
215
253
```
216
254
217
-
This makes your library bundle to be available as a global variable when imported. To make the library compatible with other environments, add `libraryTarget` property to the config.
255
+
This makes your library bundle to be available as a global variable named `webpackNumbers`when imported. To make the library compatible with other environments, add `libraryTarget` property to the config. This will add the differents options about how the library can be exposed.
218
256
219
257
__webpack.config.js__
220
258
@@ -224,13 +262,19 @@ module.exports = {
224
262
output: {
225
263
...
226
264
library:'webpackNumbers',
227
-
libraryTarget:'umd'
265
+
libraryTarget:'umd',
228
266
}
229
267
...
230
268
};
231
269
```
232
-
233
-
If `library` is set and `libraryTarget` is not, `libraryTarget` defaults to `var` as specified in the [output configuration documentation](/configuration/output). See [`output.libraryTarget`](/configuration/output#output-librarytarget) there for a detailed list of all available options.
270
+
You can expose the library in the next ways:
271
+
- Variable: as a global variable. Available in the `script` tag. i.e. `libraryTarget:'var'`.
272
+
- This: available trough the this object. i.e. `libraryTarget:'this'`.
273
+
- Window: available trough the `window` object, in the browser. i.e. `libraryTarget:'window'`.
274
+
- UMD: available after AMD or CommonJS `require`. i.e. `libraryTarget:'umd'`
275
+
276
+
If `library` is set and `libraryTarget` is not, `libraryTarget` defaults to `var` as specified in the [output configuration documentation](/configuration/output).
277
+
See [`output.libraryTarget`](/configuration/output#output-librarytarget) there for a detailed list of all available options.
0 commit comments