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.mdx
+118Lines changed: 118 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,6 +19,7 @@ contributors:
19
19
- kinetifex
20
20
- anshumanv
21
21
- SaulSilver
22
+
- fi3ework
22
23
---
23
24
24
25
The `externals` configuration option provides a way of excluding dependencies from the output bundles. Instead, the created bundle relies on that dependency to be present in the consumer's (any end-user application) environment. This feature is typically most useful to **library developers**, however there are a variety of applications for it.
@@ -353,6 +354,8 @@ Supported types:
353
354
-`'import'` - uses `import()` to load a native EcmaScript module (async module)
354
355
-`'jsonp'`
355
356
-[`'module'`](#externalstypemodule)
357
+
-[`'import'`](#externalstypeimport)
358
+
-[`'module-import'`](#externalstypemodule-import)
356
359
-[`'node-commonjs'`](#externalstypenode-commonjs)
357
360
-[`'promise'`](#externalstypepromise) - same as `'var'` but awaits the result (async module)
Note that the output bundle will have an `import()` statement.
524
+
525
+
### externalsType.module-import
526
+
527
+
Specify the default type of externals as `'module-import'`. This combines [`'module'`](#externalstypemodule) and [`'import'`](#externalstypeimport). Webpack will automatically detect the type of import syntax, setting it to `'module'` for static imports and `'import'` for dynamic imports.
528
+
529
+
Ensure to enable [`experiments.outputModule`](/configuration/experiments/#experimentsoutputmodule) first if static imports exist, otherwise, webpack will throw errors.
Note that the output bundle will have an `import` or `import()` statement.
576
+
577
+
When a module is not imported via `import` or `import()`, webpack will use the `"module"` externals type as a fallback. If you want to use a different kind of externals as a fallback, you can specify it with a function in the `externals` option. For example:
578
+
579
+
```js
580
+
module.exports= {
581
+
externalsType:"module-import",
582
+
externals: [
583
+
function (
584
+
{ request, dependencyType },
585
+
callback
586
+
) {
587
+
if (dependencyType ==="commonjs") {
588
+
returncallback(null, `node-commonjs ${request}`);
589
+
}
590
+
callback();
591
+
},
592
+
]
593
+
```
594
+
477
595
### externalsType.node-commonjs
478
596
479
597
Specify the default type of externals as `'node-commonjs'`. Webpack will import [`createRequire`](https://nodejs.org/api/module.html#module_module_createrequire_filename) from `'module'` to construct a require function for loading externals used in a module.
0 commit comments