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/configuration/externals.md
+74Lines changed: 74 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -321,6 +321,7 @@ Supported types:
321
321
-`'system'`
322
322
-`'promise'` - same as `'var'` but awaits the result (async module)
323
323
-`'import'` - uses `import()` to load a native EcmaScript module (async module)
324
+
-`'script'` - load script exposing predefined global variables with HTML `<script>` element
324
325
325
326
__webpack.config.js__
326
327
@@ -330,3 +331,76 @@ module.exports = {
330
331
externalsType:'promise'
331
332
};
332
333
```
334
+
335
+
### `script`
336
+
337
+
External script can be loaded from any URL when [`externalsType`](#externalstype) is set to `'script'`. The `<script>` tag would be removed after the script has been loaded.
338
+
339
+
#### Syntax
340
+
341
+
```javascript
342
+
module.exports= {
343
+
externals: {
344
+
packageName: ['http://example.com/script.js', 'global', 'property', 'property'] // properties are optional
345
+
}
346
+
};
347
+
```
348
+
349
+
You can also use the shortcut syntax if you're not going to specify any properties:
350
+
351
+
```javascript
352
+
module.exports= {
353
+
externals: {
354
+
packageName:'global@http://example.com/script.js'// no properties here
355
+
}
356
+
};
357
+
```
358
+
359
+
T> [`output.publicPath`](/configuration/output/#outputpublicpath) won't be added to the provided URL.
Both local variable `head` and global `window._` will be exposed when you `import``lodash`:
397
+
398
+
```js
399
+
importheadfrom'lodash';
400
+
console.log(head([1, 2, 3])); // logs 1 here
401
+
console.log(window._.head(['a', 'b'])); // logs a here
402
+
```
403
+
404
+
T> When loading code with HTML `<script>` tags, the webpack runtime will try to find an existing `<script>` tag that matches the `src` attribute or has a specific `data-webpack` attribute. For chunk loading `data-webpack` attribute would have value of `'[output.uniqueName]:chunk-[chunkId]'` while external script has value of `'[output.uniqueName]:[global]'`.
405
+
406
+
T> Options like `output.chunkLoadTimeout`, `output.crossOriginLoading` and `output.scriptType` will also have effect on the external scripts loaded this way.
0 commit comments