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
The bundle with external dependencies can be used in various module contexts, such as [CommonJS, AMD, global and ES2015 modules](/concepts/modules). The external library may be available in any of these forms:
62
+
The property name `jquery` indicates that the module `jquery`in `import $ from 'jquery'` should be excluded. In order to replace this module, the value `jQuery` will be used to retrieve a global `jQuery` variable, as the default external library type is `var`, see [externalsType](#externalstype).
63
63
64
-
-**root**: The library should be available as a global variable (e.g. via a script tag).
65
-
-**commonjs**: The library should be available as a CommonJS module.
66
-
-**commonjs2**: Similar to the above but where the export is `module.exports.default`.
67
-
-**amd**: Similar to `commonjs` but using AMD module system.
64
+
The bundle with external dependencies can be used in various module contexts, such as [CommonJS, AMD, global and ES2015 modules](/concepts/modules).
65
+
66
+
The external library may be available in any of these forms: global variable, CommonJS, AMD, ES2015 Module, see more in [externalsType](#externalstype).
68
67
69
68
The following syntaxes are accepted...
70
69
71
70
### string
72
71
73
-
See the example above. The property name `jquery` indicates that the module `jquery` in `import $ from 'jquery'` should be excluded. In order to replace this module, the value `jQuery` will be used to retrieve a global `jQuery` variable. In other words, when a string is provided it will be treated as `root` (defined above and below).
72
+
Depending on the [externalsType](#externalstype), this could be the name of the global variable (see [`'global'`](#externalstypeglobal), [`'this'`](#externalstypethis), [`'var'`](#externalstypevar), [`'window'`](#externalstypewindow)) or the name of the module (see `amd`, [`commonjs`](#externalstypecommonjs), [`module`](#externalstypemodule), `umd`).
73
+
74
+
You can also use the shortcut syntax if you're defining only 1 external:
75
+
76
+
```javascript
77
+
module.exports= {
78
+
//...
79
+
externals:'jquery',
80
+
};
81
+
```
82
+
83
+
equals to
74
84
75
-
You can specify [external library type](#externalstype) to the external using the `[externalsType] [externals]` syntax. This will override the default external library type specified in the [externalsType](#externalstype) option.
85
+
```javascript
86
+
module.exports= {
87
+
//...
88
+
externals: {
89
+
jquery:'jquery',
90
+
},
91
+
};
92
+
```
76
93
77
-
For example, if you want to externalise a library as a [CommonJS module](#externalstypecommonjs), you can specify
94
+
You can specify the [external library type](#externalstype) to the external using the `${externalsType} ${path}` syntax. This will override the default external library type specified in the [externalsType](#externalstype) option.
95
+
96
+
For example, if the external library is a [CommonJS module](#externalstypecommonjs), you can specify
78
97
79
98
```javascript
80
99
module.exports= {
@@ -96,11 +115,11 @@ module.exports = {
96
115
};
97
116
```
98
117
99
-
`subtract: ['./math', 'subtract']` allows you select part of a commonjs module, where `./math` is the module and your bundle only requires the subset under the `subtract` variable. This example would translate to `require('./math').subtract;`
118
+
`subtract: ['./math', 'subtract']` allows you select part of a module, where `./math` is the module and your bundle only requires the subset under the `subtract` variable.
100
119
101
-
Similar to the [string syntax](#string), you can specify the external library type with the `[externalsType] [externals]` syntax.
120
+
When the `externalsType` is `commonjs`, this example would translate to `require('./math').subtract;`. When the `externalsType` is `window`, this example would translate to `window["./math"]["subtract"];`
102
121
103
-
For example
122
+
Similar to the [string syntax](#string), you can specify the external library type with the `${externalsType} ${path}` syntax, in the first item of the array, for example:
104
123
105
124
```javascript
106
125
module.exports= {
@@ -113,7 +132,7 @@ module.exports = {
113
132
114
133
### object
115
134
116
-
W> An object with `{ root, amd, commonjs, ... }` is only allowed for [`libraryTarget: 'umd'`](/configuration/output/#outputlibrarytarget). It's not allowed for other library targets.
135
+
W> An object with `{ root, amd, commonjs, ... }` is only allowed for [`libraryTarget: 'umd'`](/configuration/output/#outputlibrarytarget) and [`externalsType: 'umd'`](#externalstype). It's not allowed for other library targets.
117
136
118
137
```javascript
119
138
module.exports= {
@@ -163,8 +182,8 @@ Here're arguments the function can receive:
163
182
The callback function takes three arguments:
164
183
165
184
-`err` (`Error`): Used to indicate if there has been an error while externalizing the import. If there is an error, this should be the only parameter used.
166
-
-`result` (`string``[string]``object`): Describes the external module. Can accept a string in the format `${type} ${path}`, or one of the other standard external formats ([`string`](#string), [`[string]`](#string-1), or [`object`](#object))
167
-
-`type` (`string`): Optional parameter that indicates the module type (if it has not already been indicated in the `result` parameter).
185
+
-`result` (`string``[string]``object`): Describes the external module with the other external formats ([`string`](#string), [`[string]`](#string-1), or [`object`](#object))
186
+
-`type` (`string`): Optional parameter that indicates the module [external type](#externalstype) (if it has not already been indicated in the `result` parameter).
168
187
169
188
As an example, to externalize all imports where the import path matches a regular expression you could do the following:
170
189
@@ -330,23 +349,23 @@ Supported types:
330
349
331
350
-`'amd'`
332
351
-`'amd-require'`
333
-
-`'assign'`
352
+
-`'assign'` - same as `'var'`
334
353
-[`'commonjs'`](#externalstypecommonjs)
335
354
-`'commonjs-module'`
336
-
-`'global'`
355
+
-[`'global'`](#externalstypeglobal)
337
356
-`'import'` - uses `import()` to load a native EcmaScript module (async module)
338
357
-`'jsonp'`
339
358
-[`'module'`](#externalstypemodule)
340
359
-[`'node-commonjs'`](#externalstypenode-commonjs)
341
-
-`'promise'` - same as `'var'` but awaits the result (async module)
342
-
-`'self'`
360
+
-[`'promise'`](#externalstypepromise) - same as `'var'` but awaits the result (async module)
361
+
-[`'self'`](#externalstypeself)
343
362
-`'system'`
344
363
-[`'script'`](#externalstypescript)
345
-
-`'this'`
364
+
-[`'this'`](#externalstypethis)
346
365
-`'umd'`
347
366
-`'umd2'`
348
-
-`'var'`
349
-
-`'window'`
367
+
-[`'var'`](#externalstypevar)
368
+
-[`'window'`](#externalstypewindow)
350
369
351
370
**webpack.config.js**
352
371
@@ -385,6 +404,36 @@ into something like:
385
404
constfs=require('fs-extra');
386
405
```
387
406
407
+
### externalsType.global
408
+
409
+
Specify the default type of externals as `'global'`. Webpack will read the external as a global variable on the `global` object.
410
+
411
+
#### Example
412
+
413
+
```javascript
414
+
importjqfrom'jquery';
415
+
jq('.my-element').animate(/* ... */);
416
+
```
417
+
418
+
**webpack.config.js**
419
+
420
+
```javascript
421
+
module.exports= {
422
+
// ...
423
+
externalsType:'global',
424
+
externals: {
425
+
jquery:'$',
426
+
},
427
+
};
428
+
```
429
+
430
+
Will compile into something like
431
+
432
+
```javascript
433
+
constjq=global['$'];
434
+
jq('.my-element').animate(/* ... */);
435
+
```
436
+
388
437
### externalsType.module
389
438
390
439
Specify the default type of externals as `'module'`. Webpack will generate code like `import * as X from '...'` for externals used in a module.
@@ -415,9 +464,69 @@ module.export = {
415
464
};
416
465
```
417
466
467
+
### externalsType.promise
468
+
469
+
Specify the default type of externals as `'promise'`. Webpack will read the external as a global variable (similar to [`'var'`](#externalstypevar)) and `await` for it.
470
+
471
+
#### Example
472
+
473
+
```javascript
474
+
importjqfrom'jquery';
475
+
jq('.my-element').animate(/* ... */);
476
+
```
477
+
478
+
**webpack.config.js**
479
+
480
+
```javascript
481
+
module.exports= {
482
+
// ...
483
+
externalsType:'promise',
484
+
externals: {
485
+
jquery:'$',
486
+
},
487
+
};
488
+
```
489
+
490
+
Will compile into something like
491
+
492
+
```javascript
493
+
constjq=await $;
494
+
jq('.my-element').animate(/* ... */);
495
+
```
496
+
497
+
### externalsType.self
498
+
499
+
Specify the default type of externals as `'self'`. Webpack will read the external as a global variable on the `self` object.
500
+
501
+
#### Example
502
+
503
+
```javascript
504
+
importjqfrom'jquery';
505
+
jq('.my-element').animate(/* ... */);
506
+
```
507
+
508
+
**webpack.config.js**
509
+
510
+
```javascript
511
+
module.exports= {
512
+
// ...
513
+
externalsType:'self',
514
+
externals: {
515
+
jquery:'$',
516
+
},
517
+
};
518
+
```
519
+
520
+
Will compile into something like
521
+
522
+
```javascript
523
+
constjq=self['$'];
524
+
jq('.my-element').animate(/* ... */);
525
+
```
526
+
418
527
### externalsType.script
419
528
420
-
Specify the default type of externals as `'script'`. Webpack will Load the external as a script exposing predefined global variables with HTML `<script>` element. The `<script>` tag would be removed after the script has been loaded.
529
+
Specify the default type of externals as `'script'`. Webpack will load the external as a script exposing predefined global variables with HTML `<script>` element. The `<script>` tag would be removed after the script has been loaded.
421
530
422
531
#### Syntax
423
532
@@ -499,6 +608,96 @@ T> When loading code with HTML `<script>` tags, the webpack runtime will try to
499
608
500
609
T> Options like `output.chunkLoadTimeout`, `output.crossOriginLoading` and `output.scriptType` will also have effect on the external scripts loaded this way.
501
610
611
+
### externalsType.this
612
+
613
+
Specify the default type of externals as `'this'`. Webpack will read the external as a global variable on the `this` object.
614
+
615
+
#### Example
616
+
617
+
```javascript
618
+
importjqfrom'jquery';
619
+
jq('.my-element').animate(/* ... */);
620
+
```
621
+
622
+
**webpack.config.js**
623
+
624
+
```javascript
625
+
module.exports= {
626
+
// ...
627
+
externalsType:'this',
628
+
externals: {
629
+
jquery:'$',
630
+
},
631
+
};
632
+
```
633
+
634
+
Will compile into something like
635
+
636
+
```javascript
637
+
constjq=this['$'];
638
+
jq('.my-element').animate(/* ... */);
639
+
```
640
+
641
+
### externalsType.var
642
+
643
+
Specify the default type of externals as `'var'`. Webpack will read the external as a global variable.
644
+
645
+
#### Example
646
+
647
+
```javascript
648
+
importjqfrom'jquery';
649
+
jq('.my-element').animate(/* ... */);
650
+
```
651
+
652
+
**webpack.config.js**
653
+
654
+
```javascript
655
+
module.exports= {
656
+
// ...
657
+
externalsType:'var',
658
+
externals: {
659
+
jquery:'$',
660
+
},
661
+
};
662
+
```
663
+
664
+
Will compile into something like
665
+
666
+
```javascript
667
+
constjq= $;
668
+
jq('.my-element').animate(/* ... */);
669
+
```
670
+
671
+
### externalsType.window
672
+
673
+
Specify the default type of externals as `'window'`. Webpack will read the external as a global variable on the `window` object.
0 commit comments