|
4 | 4 | contributors:
|
5 | 5 | - sokra
|
6 | 6 | - skipjack
|
| 7 | + - tomasAlabes |
7 | 8 | ---
|
8 | 9 |
|
9 | 10 | The top-level `output` key contains set of options instructing webpack on how and where it should output your bundles, assets and anything else you bundle or load with webpack.
|
@@ -216,23 +217,139 @@ Note that `output.libraryTarget` defaults to `var`. This means if only `output.l
|
216 | 217 |
|
217 | 218 | `string`
|
218 | 219 |
|
| 220 | +> Default: `"var"` |
| 221 | +
|
219 | 222 | Read the [library guide](/guides/author-libraries) for details.
|
220 | 223 |
|
221 |
| -Configure how the library will be exposed. Any one of the following options can be used: (the examples assume `library: "MyLibrary"`) |
| 224 | +Configure how the library will be exposed. Any one of the following options can be used. |
| 225 | + |
| 226 | +> To give your library a name, set the `output.library` config to it (the examples assume `library: "MyLibrary"`) |
| 227 | +
|
| 228 | +The following options are supported: |
| 229 | + |
| 230 | + |
| 231 | +`libraryTarget: "var"` - (default) When your library is loaded, the **return value of your entry point** will be assigned to a variable: |
| 232 | + |
| 233 | +```javascript |
| 234 | +var MyLibrary = _entry_return_; |
222 | 235 |
|
223 |
| -`libraryTarget: "amd"` - Expose it using [Asynchronous Module Definition](http://davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/) (AMD) |
| 236 | +// your users will use your library like: |
| 237 | +MyLibrary.doSomething(); |
| 238 | +``` |
| 239 | +(Not specifying a `output.library` will cancel this var configuration) |
| 240 | + |
| 241 | + |
| 242 | +`libraryTarget: "this"` - When your library is loaded, the **return value of your entry point** will be assigned to this, the meaning of `this` is up to you: |
| 243 | + |
| 244 | +```javascript |
| 245 | +this["MyLibrary"] = _entry_return_; |
| 246 | + |
| 247 | +// your users will use your library like: |
| 248 | +this.MyLibrary.doSomething(); |
| 249 | +MyLibrary.doSomething(); //if this is window |
| 250 | +``` |
224 | 251 |
|
225 |
| -`libraryTarget: "commonjs"` - Expose it using the `exports` object (i.e. `exports["MyLibrary"] = ...`) |
226 | 252 |
|
227 |
| -`libraryTarget: "commonjs2"` - Expose it using the `module.exports` object (`output.library` is ignored) |
| 253 | +`libraryTarget: "commonjs"` - When your library is loaded, the return value of your entry point will be part of the exports object. As the name implies, this is used in CommonJS environments: |
| 254 | + |
| 255 | +```javascript |
| 256 | +exports["MyLibrary"] = _entry_return_; |
| 257 | + |
| 258 | +//your users will use your library like: |
| 259 | +require("MyLibrary").doSomething(); |
| 260 | +``` |
| 261 | + |
| 262 | +`libraryTarget: "commonjs2"` - When your library is loaded, the return value of your entry point will be part of the exports object. As the name implies, this is used in CommonJS environments: |
| 263 | + |
| 264 | +```javascript |
| 265 | +module.exports = _entry_return_; |
| 266 | + |
| 267 | +//your users will use your library like: |
| 268 | +require("MyLibrary").doSomething(); |
| 269 | +``` |
| 270 | + |
| 271 | +_Wondering the difference between CommonJS and CommonJS2? Check [this](https://github.com/webpack/webpack/issues/1114) out (they are pretty much the same)._ |
| 272 | + |
228 | 273 |
|
229 | 274 | `libraryTarget: "commonjs-module"` - Expose it using the `module.exports` object (`output.library` is ignored), `__esModule` is defined (it's threaded as ES2015 Module in interop mode)
|
230 | 275 |
|
231 |
| -`libraryTarget: "this"` - Expose it as a property of `this` (i.e. `this["MyLibrary"] = ...`) |
232 | 276 |
|
233 |
| -`libraryTarget: "umd"` - Expose it using [Universal Module Definition](http://davidbcalhoun.com/2014/what-is-amd-commonjs-and-umd/) (UMD) |
| 277 | +`libraryTarget: "amd"` - In this case webpack will make your library an AMD module. |
| 278 | + |
| 279 | +But there is a very important pre-requisite, your entry chunk must be defined with the define property, if not, webpack wil create the AMD module, but without dependencies. |
| 280 | +The output will be something like this: |
| 281 | + |
| 282 | +```javascript |
| 283 | +define([], function() { |
| 284 | + //what this module returns is what your entry chunk returns |
| 285 | +}); |
| 286 | +``` |
| 287 | +But if you download this script, first you may get a error: `define is not defined`, it’s ok! |
| 288 | +If you are distributing your library with AMD, then your users need to use RequireJS to load it. |
| 289 | + |
| 290 | +Now that you have RequireJS loaded, you can load your library. |
| 291 | + |
| 292 | +But, `require([ _what?_ ])`? |
| 293 | + |
| 294 | +`output.library`! |
| 295 | + |
| 296 | +```javascript |
| 297 | +output: { |
| 298 | + name: "MyLibrary", |
| 299 | + libraryTarget: "amd" |
| 300 | +} |
| 301 | +``` |
| 302 | + |
| 303 | +So your module will be like: |
| 304 | + |
| 305 | +```javascript |
| 306 | +define("MyLibrary", [], function() { |
| 307 | + //what this module returns is what your entry chunk returns |
| 308 | +}); |
| 309 | +``` |
| 310 | + |
| 311 | +And you can use it like this: |
| 312 | + |
| 313 | +```javascript |
| 314 | +// And then your users will be able to do: |
| 315 | +require(["MyLibrary"], function(MyLibrary){ |
| 316 | + MyLibrary.doSomething(); |
| 317 | +}); |
| 318 | +``` |
| 319 | + |
| 320 | +`libraryTarget: "umd"` - This is a way for your library to work with all the module definitions (and where aren't modules at all). |
| 321 | +It will work with CommonJS, AMD and as global variable. You can check the [UMD Repository](https://github.com/umdjs/umd) to know more about it. |
| 322 | + |
| 323 | +In this case, you need the another property to name your module: |
| 324 | + |
| 325 | +```javascript |
| 326 | +output: { |
| 327 | + name: "MyLibrary", |
| 328 | + libraryTarget: "umd", |
| 329 | + umdNamedDefine: true |
| 330 | +} |
| 331 | +``` |
| 332 | + |
| 333 | +And finally the output is: |
| 334 | +```javascript |
| 335 | +(function webpackUniversalModuleDefinition(root, factory) { |
| 336 | + if(typeof exports === 'object' && typeof module === 'object') |
| 337 | + module.exports = factory(); |
| 338 | + else if(typeof define === 'function' && define.amd) |
| 339 | + define("MyLibrary", [], factory); |
| 340 | + else if(typeof exports === 'object') |
| 341 | + exports["MyLibrary"] = factory(); |
| 342 | + else |
| 343 | + root["MyLibrary"] = factory(); |
| 344 | +})(this, function() { |
| 345 | + //what this module returns is what your entry chunk returns |
| 346 | +}); |
| 347 | +``` |
| 348 | + |
| 349 | +Module proof library. |
| 350 | + |
| 351 | +The dependencies for your library will be defined by the [`externals`](/configuration/externals/) config. |
234 | 352 |
|
235 |
| -`libraryTarget: "var"` - Expose it as a variable (i.e. `var MyLibrary = ...`) |
236 | 353 |
|
237 | 354 | ## `output.path`
|
238 | 355 |
|
|
0 commit comments