Skip to content

Commit 7729b52

Browse files
fi3eworksnitin315
andauthored
docs(externals): add import and module-import external type (#7345)
Co-authored-by: Nitin Kumar <[email protected]>
1 parent 468ac75 commit 7729b52

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

src/content/configuration/externals.mdx

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ contributors:
1919
- kinetifex
2020
- anshumanv
2121
- SaulSilver
22+
- fi3ework
2223
---
2324

2425
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:
353354
- `'import'` - uses `import()` to load a native EcmaScript module (async module)
354355
- `'jsonp'`
355356
- [`'module'`](#externalstypemodule)
357+
- [`'import'`](#externalstypeimport)
358+
- [`'module-import'`](#externalstypemodule-import)
356359
- [`'node-commonjs'`](#externalstypenode-commonjs)
357360
- [`'promise'`](#externalstypepromise) - same as `'var'` but awaits the result (async module)
358361
- [`'self'`](#externalstypeself)
@@ -474,6 +477,121 @@ jq('.my-element').animate(/* ... */);
474477

475478
Note that there will be an `import` statement in the output bundle.
476479

480+
### externalsType.import
481+
482+
Specify the default type of externals as `'import'`. Webpack will generate code like `import('...')` for externals used in a module.
483+
484+
#### Example
485+
486+
```javascript
487+
async function foo() {
488+
const jq = await import('jQuery');
489+
jq('.my-element').animate(/* ... */);
490+
}
491+
```
492+
493+
**webpack.config.js**
494+
495+
```js
496+
module.exports = {
497+
externalsType: 'import',
498+
externals: {
499+
jquery: 'jquery',
500+
},
501+
};
502+
```
503+
504+
Will generate something like below:
505+
506+
```javascript
507+
var __webpack_modules__ = {
508+
jQuery: (module) => {
509+
module.exports = import('jQuery');
510+
},
511+
};
512+
513+
// webpack runtime...
514+
515+
async function foo() {
516+
const jq = await Promise.resolve(/* import() */).then(
517+
__webpack_require__.bind(__webpack_require__, 'jQuery')
518+
);
519+
jq('.my-element').animate(/* ... */);
520+
}
521+
```
522+
523+
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.
530+
531+
#### Example
532+
533+
```javascript
534+
import { attempt } from 'lodash';
535+
536+
async function foo() {
537+
const jq = await import('jQuery');
538+
attempt(() => jq('.my-element').animate(/* ... */));
539+
}
540+
```
541+
542+
**webpack.config.js**
543+
544+
```js
545+
module.exports = {
546+
externalsType: 'import',
547+
externals: {
548+
jquery: 'jquery',
549+
},
550+
};
551+
```
552+
553+
Will generate something like below:
554+
555+
```javascript
556+
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from 'lodash';
557+
const lodash = __WEBPACK_EXTERNAL_MODULE_jquery__;
558+
559+
var __webpack_modules__ = {
560+
jQuery: (module) => {
561+
module.exports = import('jQuery');
562+
},
563+
};
564+
565+
// webpack runtime...
566+
567+
async function foo() {
568+
const jq = await Promise.resolve(/* import() */).then(
569+
__webpack_require__.bind(__webpack_require__, 'jQuery')
570+
);
571+
(0, lodash.attempt)(() => jq('.my-element').animate(/* ... */));
572+
}
573+
```
574+
575+
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+
return callback(null, `node-commonjs ${request}`);
589+
}
590+
callback();
591+
},
592+
]
593+
```
594+
477595
### externalsType.node-commonjs
478596
479597
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

Comments
 (0)