Skip to content

Commit b3bd506

Browse files
committed
docs(externals): add import and module-import external type
1 parent 6ca0e89 commit b3bd506

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

src/content/configuration/externals.mdx

Lines changed: 98 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.
@@ -474,6 +475,103 @@ jq('.my-element').animate(/* ... */);
474475

475476
Note that there will be an `import` statement in the output bundle.
476477

478+
### externalsType.import
479+
480+
Specify the default type of externals as `'import'`. Webpack will generate code like `import('...')` for externals used in a module.
481+
482+
#### Example
483+
484+
```javascript
485+
async function foo() {
486+
const jq = await import('jQuery');
487+
jq('.my-element').animate(/* ... */);
488+
}
489+
```
490+
491+
**webpack.config.js**
492+
493+
```js
494+
module.exports = {
495+
externalsType: 'import',
496+
externals: {
497+
jquery: 'jquery',
498+
},
499+
};
500+
```
501+
502+
Will generate into something like
503+
504+
```javascript
505+
var __webpack_modules__ = {
506+
jQuery: (module) => {
507+
module.exports = import('jQuery');
508+
},
509+
};
510+
511+
// webpack runtime...
512+
513+
async function foo() {
514+
const jq = await Promise.resolve(/* import() */).then(
515+
__webpack_require__.bind(__webpack_require__, 'jQuery')
516+
);
517+
jq('.my-element').animate(/* ... */);
518+
}
519+
```
520+
521+
Note that there will be an `import()` statement in the output bundle.
522+
523+
### externalsType['module-import']
524+
525+
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.
526+
527+
Make sure to enable [`experiments.outputModule`](/configuration/experiments/#experimentsoutputmodule) first if static imports exist, otherwise webpack will throw errors.
528+
529+
#### Example
530+
531+
```javascript
532+
import { attempt } from 'lodash';
533+
534+
async function foo() {
535+
const jq = await import('jQuery');
536+
attempt(() => jq('.my-element').animate(/* ... */));
537+
}
538+
```
539+
540+
**webpack.config.js**
541+
542+
```js
543+
module.exports = {
544+
externalsType: 'import',
545+
externals: {
546+
jquery: 'jquery',
547+
},
548+
};
549+
```
550+
551+
Will generate into something like
552+
553+
```javascript
554+
import * as __WEBPACK_EXTERNAL_MODULE_lodash__ from 'lodash';
555+
const lodash = __WEBPACK_EXTERNAL_MODULE_jquery__;
556+
557+
var __webpack_modules__ = {
558+
jQuery: (module) => {
559+
module.exports = import('jQuery');
560+
},
561+
};
562+
563+
// webpack runtime...
564+
565+
async function foo() {
566+
const jq = await Promise.resolve(/* import() */).then(
567+
__webpack_require__.bind(__webpack_require__, 'jQuery')
568+
);
569+
(0, lodash.attempt)(() => jq('.my-element').animate(/* ... */));
570+
}
571+
```
572+
573+
Note that there will be an `import` or `import()` statement in the output bundle.
574+
477575
### externalsType.node-commonjs
478576

479577
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)