Skip to content

Commit a5f17b0

Browse files
marioaccskipjack
authored andcommitted
Project Structure added
Explanations rewritten to be more understandable. Warning for bug added.
1 parent 257df4b commit a5f17b0

File tree

1 file changed

+62
-18
lines changed

1 file changed

+62
-18
lines changed

src/content/guides/author-libraries.md

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ contributors:
66
- johnstew
77
- simon04
88
- 5angel
9+
- marioacc
910
---
1011

1112
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
1415
## Authoring a Library
1516

1617
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+
```
1836

1937
__src/ref.json__
2038
```javascript
@@ -101,18 +119,23 @@ For full library configuration and code please refer to [webpack-library-example
101119

102120
## Configure webpack
103121

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.
105133

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.
111134

112135

113136
### Add webpack
114137

115-
Add basic webpack configuration.
138+
Add this basic webpack configuration to bundle the library.
116139

117140
__webpack.config.js__
118141

@@ -129,15 +152,14 @@ module.exports = {
129152

130153
```
131154

132-
This adds basic configuration to bundle the library.
133155

134156

135157
### Add `externals`
136158

137159
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.
138160
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.
139161

140-
This can be done using the `externals` configuration as
162+
This can be done using the `externals` configuration as:
141163

142164
__webpack.config.js__
143165

@@ -158,7 +180,7 @@ module.exports = {
158180

159181
This means that your library expects a dependency named `lodash` to be available in the consumer's environment.
160182

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.
162184

163185
```javascript
164186
module.exports = {
@@ -171,7 +193,7 @@ module.exports = {
171193
};
172194
```
173195

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:
175197

176198
```javascript
177199
import A from 'library/A';
@@ -195,11 +217,27 @@ module.exports = {
195217
};
196218
```
197219

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+
198236
### Add `libraryTarget`
199237

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.
201239

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.
203241

204242
__webpack.config.js__
205243

@@ -214,7 +252,7 @@ module.exports = {
214252
};
215253
```
216254

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.
218256

219257
__webpack.config.js__
220258

@@ -224,13 +262,19 @@ module.exports = {
224262
output: {
225263
...
226264
library: 'webpackNumbers',
227-
libraryTarget: 'umd'
265+
libraryTarget: 'umd',
228266
}
229267
...
230268
};
231269
```
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.
234278

235279

236280
### Final Steps

0 commit comments

Comments
 (0)