Skip to content

Commit 25bf111

Browse files
authored
Merge pull request #818 from mattce/master
Update targetLibrary documentation
2 parents 1cb0598 + a4472c2 commit 25bf111

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

content/configuration/index.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ contributors:
88
- bondz
99
- sricc
1010
- terinjokes
11+
- mattce
1112
---
1213

1314
webpack is fed via a configuration object. It is passed in one of two ways depending on how you are using webpack: through the terminal or via Node.js. All the available configuration options are specified below.
@@ -57,12 +58,18 @@ var path = require('path');
5758
[library](/configuration/output#output-library): "MyLibrary", // string,
5859
// the name of the exported library
5960
60-
<details><summary>[libraryTarget](/configuration/output#output-librarytarget): "umd", // enum</summary>
61-
[libraryTarget](/configuration/output#output-librarytarget): "commonjs2", // exported with module.exports
62-
[libraryTarget](/configuration/output#output-librarytarget): "commonjs", // exported as properties to exports
63-
[libraryTarget](/configuration/output#output-librarytarget): "amd", // defined with AMD defined method
64-
[libraryTarget](/configuration/output#output-librarytarget): "this", // property set on this
65-
[libraryTarget](/configuration/output#output-librarytarget): "var", // variable defined in root scope
61+
<details><summary>[libraryTarget](/configuration/output#output-librarytarget): "umd", // universal module definition</summary>
62+
[libraryTarget](/configuration/output#output-librarytarget): "umd2", // universal module definition
63+
[libraryTarget](/configuration/output#output-librarytarget): "commonjs2", // exported with module.exports
64+
[libraryTarget](/configuration/output#output-librarytarget): "commonjs-module", // exports with module.exports
65+
[libraryTarget](/configuration/output#output-librarytarget): "commonjs", // exported as properties to exports
66+
[libraryTarget](/configuration/output#output-librarytarget): "amd", // defined with AMD defined method
67+
[libraryTarget](/configuration/output#output-librarytarget): "this", // property set on this
68+
[libraryTarget](/configuration/output#output-librarytarget): "var", // variable defined in root scope
69+
[libraryTarget](/configuration/output#output-librarytarget): "assign", // blind assignment
70+
[libraryTarget](/configuration/output#output-librarytarget): "window", // property set to window object
71+
[libraryTarget](/configuration/output#output-librarytarget): "global", // property set to global object
72+
[libraryTarget](/configuration/output#output-librarytarget): "jsonp", // jsonp wrapper
6673
</details>
6774
// the type of the exported library
6875

content/configuration/output.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ contributors:
55
- sokra
66
- skipjack
77
- tomasAlabes
8+
- mattce
89
---
910

1011
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.
@@ -250,7 +251,27 @@ MyLibrary.doSomething(); //if this is window
250251
```
251252

252253

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+
`libraryTarget: "window"` - When your library is loaded, **the return value of your entry point** will be part `window` object.
255+
256+
```javascript
257+
window["MyLibrary"] = _entry_return_;
258+
259+
//your users will use your library like:
260+
window.MyLibrary.doSomething();
261+
```
262+
263+
264+
`libraryTarget: "global"` - When your library is loaded, **the return value of your entry point** will be part `global` object.
265+
266+
```javascript
267+
global["MyLibrary"] = _entry_return_;
268+
269+
//your users will use your library like:
270+
global.MyLibrary.doSomething();
271+
```
272+
273+
274+
`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:
254275

255276
```javascript
256277
exports["MyLibrary"] = _entry_return_;
@@ -259,7 +280,8 @@ exports["MyLibrary"] = _entry_return_;
259280
require("MyLibrary").doSomething();
260281
```
261282

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:
283+
284+
`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:
263285

264286
```javascript
265287
module.exports = _entry_return_;
@@ -320,7 +342,7 @@ require(["MyLibrary"], function(MyLibrary){
320342
`libraryTarget: "umd"` - This is a way for your library to work with all the module definitions (and where aren't modules at all).
321343
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.
322344

323-
In this case, you need the another property to name your module:
345+
In this case, you need the `library` property to name your module:
324346

325347
```javascript
326348
output: {
@@ -347,6 +369,21 @@ And finally the output is:
347369

348370
Module proof library.
349371

372+
373+
`libraryTarget: "assign"` - Here webpack will blindly generate an implied global.
374+
375+
```javascript
376+
MyLibrary = _entry_return_;
377+
```
378+
Be aware that if `MyLibrary` isn't defined earlier your library will be set in global scope.
379+
380+
381+
`libraryTarget: "jsonp"` - This will wrap the return value of your entry point into a jsonp wrapper.
382+
383+
```javascript
384+
MyLibrary( _entry_return_ );
385+
```
386+
350387
The dependencies for your library will be defined by the [`externals`](/configuration/externals/) config.
351388

352389

0 commit comments

Comments
 (0)