externals
é
ç½®é项æä¾äºãä»è¾åºç bundle ä¸æé¤ä¾èµãçæ¹æ³ãç¸åï¼æå建ç bundle ä¾èµäºé£äºåå¨äºç¨æ·ç¯å¢(consumer's environment)ä¸çä¾èµãæ¤åè½é常对 library å¼å人åæ¥è¯´æ¯ææç¨çï¼ç¶èä¹ä¼æåç§åæ ·çåºç¨ç¨åºç¨å°å®ã
string
object
function
RegExp
[string, object, function, RegExp]
é²æ¢å°æäº import
çå
(package)æå
å° bundle ä¸ï¼èæ¯å¨è¿è¡æ¶(runtime)åå»ä»å¤é¨è·åè¿äºæ©å±ä¾èµ(external dependencies)ã
ä¾å¦ï¼ä» CDN å¼å ¥ jQueryï¼èä¸æ¯æå®æå ï¼
index.html
<script
src="https://code.jquery.com/jquery-3.1.0.js"
integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk="
crossorigin="anonymous"
></script>
webpack.config.js
module.exports = {
//...
externals: {
jquery: 'jQuery',
},
};
è¿æ ·å°±å¥ç¦»äºé£äºä¸éè¦æ¹å¨çä¾èµæ¨¡åï¼æ¢å¥è¯ï¼ä¸é¢å±ç¤ºç代ç è¿å¯ä»¥æ£å¸¸è¿è¡ï¼
import $ from 'jquery';
$('.my-element').animate(/* ... */);
ä¸é¢ webpack.config.js
ä¸ externals
ä¸æå®çå±æ§å称 jquery
表示 import $ from 'jquery'
ä¸ç模å jquery
åºè¯¥ä»æå
产ç©ä¸æé¤ã 为äºæ¿æ¢è¿ä¸ªæ¨¡åï¼jQuery
å¼å°ç¨äºæ£ç´¢å
¨å± jQuery
åéï¼å 为é»è®¤çå¤é¨åºç±»åæ¯ var
ï¼è¯·åé
externalsTypeã
è½ç¶æ们å¨ä¸é¢å±ç¤ºäºä¸ä¸ªä½¿ç¨å¤é¨å ¨å±åéç示ä¾ï¼ä½å®é ä¸å¯ä»¥ä»¥ä»¥ä¸ä»»ä½å½¢å¼ä½¿ç¨å¤é¨åéï¼å ¨å±åéãCommonJSãAMDãES2015 模åï¼å¨ externalsType ä¸æ¥çæ´å¤ä¿¡æ¯ã
æ ¹æ® externalsTypeï¼è¿å¯è½æ¯å
¨å±åéçå称ï¼åè§ 'global'
ã'this'
ã'var '
, 'window'
) æ模åçå称ï¼åè§ amd
, commonjs
, module
, umd
)ã
å¦æä½ åªå®ä¹ 1 个 externalï¼æ¨ä¹å¯ä»¥ä½¿ç¨å¿«æ·è¯æ³ï¼
module.exports = {
//...
externals: 'jquery',
};
equals to
module.exports = {
//...
externals: {
jquery: 'jquery',
},
};
æ¨å¯ä»¥ä½¿ç¨ ${externalsType} ${libraryName}
è¯æ³ä¸ºå¤é¨æå® å¤é¨åºç±»åã å®å°è¦ç externalsType é项ä¸æå®çé»è®¤å¤é¨åºç±»åã
ä¾å¦ï¼å¦æå¤é¨åºæ¯ä¸ä¸ª CommonJS 模åï¼ä½ å¯ä»¥æå®
module.exports = {
//...
externals: {
jquery: 'commonjs jquery',
},
};
module.exports = {
//...
externals: {
subtract: ['./math', 'subtract'],
},
};
subtract: ['./math', 'subtract']
å
è®¸ä½ éæ©æ¨¡åçä¸é¨åï¼å
¶ä¸ ./math
æ¯æ¨¡åï¼å¹¶ä¸ä½ çå
åªéè¦ subtract
åéä¸çåéã
å½ externalsType
为 commonjs
æ¶ï¼æ¤ç¤ºä¾å°è½¬æ¢ä¸º require('./math').subtract;
ï¼èå½ externalsType
为 window
æ¶ï¼æ¤ç¤ºä¾å°è½¬æ¢ä¸º window["./math"]["subtract"];
ä¸ string è¯æ³ 类似ï¼ä½ å¯ä»¥ä½¿ç¨ ${externalsType} ${libraryName}
è¯æ³å¨æ°ç»ç第ä¸é¡¹ä¸æå®å¤é¨åºç±»åï¼ä¾å¦ï¼
module.exports = {
//...
externals: {
subtract: ['commonjs ./math', 'subtract'],
},
};
module.exports = {
//...
externals: {
react: 'react',
},
// æè
externals: {
lodash: {
commonjs: 'lodash',
amd: 'lodash',
root: '_', // æåå
¨å±åé
},
},
// æè
externals: {
subtract: {
root: ['math', 'subtract'],
},
},
};
æ¤è¯æ³ç¨äºæè¿°å¤é¨ library ææå¯ç¨ç访é®æ¹å¼ãè¿é lodash
è¿ä¸ªå¤é¨ library å¯ä»¥å¨ AMD å CommonJS 模åç³»ç»ä¸éè¿ lodash
访é®ï¼ä½å¨å
¨å±åéå½¢å¼ä¸ç¨ _
访é®ãsubtract
å¯ä»¥éè¿å
¨å± math
对象ä¸çå±æ§ subtract
访é®ï¼ä¾å¦ window['math']['subtract']
ï¼ã
function ({ context, request, contextInfo, getResolve }, callback)
function ({ context, request, contextInfo, getResolve }) => promise
5.15.0+å¯¹äº webpack å¤é¨åï¼éè¿å®ä¹å½æ°æ¥æ§å¶è¡ä¸ºï¼å¯è½ä¼å¾æ帮å©ãä¾å¦ï¼webpack-node-externals è½å¤æé¤ node_modules
ç®å½ä¸ææ模åï¼è¿æä¾ä¸äºé项ï¼æ¯å¦ç½åå package(whitelist package)ã
å½æ°æ¥æ¶ä¸¤ä¸ªå ¥åï¼
ctx
(object
)ï¼å
å«æ件详æ
ç对象ãctx.context
(string
): å
å«å¼ç¨çæ件ç®å½ãctc.request
(string
): 被请æ±å¼å
¥çè·¯å¾ãctx.contextInfo
(object
): å
å« issuer çä¿¡æ¯ï¼å¦ï¼layer å compilerï¼ctx.getResolve
5.15.0+: è·åå½å解æå¨é项ç解æå½æ°ãcallback
(function (err, result, type)
): ç¨äºææ模åå¦ä½è¢«å¤é¨åçåè°å½æ°åè°å½æ°æ¥æ¶ä¸ä¸ªå ¥åï¼
err
(Error
): 被ç¨äºè¡¨æå¨å¤é¨å¤å¼ç¨çæ¶åæ¯å¦ä¼äº§çé误ãå¦ææé误ï¼è¿å°ä¼æ¯å¯ä¸è¢«ç¨å°çåæ°ãresult
(string
[string]
object
): æè¿°å¤é¨åç模åãå¯ä»¥æ¥åå
¶å®æ ååå¤é¨å模åæ ¼å¼ï¼(string
, [string]
ï¼æ object
)ãtype
(string
): å¯éçåæ°ï¼ç¨äºææ模åç external typeï¼å¦æå®æ²¡å¨ result
åæ°ä¸è¢«ææï¼ãä½ä¸ºä¾åï¼è¦å¤é¨åææå¹é ä¸ä¸ªæ£å表达å¼çå¼å ¥ï¼ä½ å¯ä»¥åä¸é¢é£æ ·å¤çï¼
webpack.config.js
module.exports = {
//...
externals: [
function ({ context, request }, callback) {
if (/^yourregex$/.test(request)) {
// ä½¿ç¨ request è·¯å¾ï¼å°ä¸ä¸ª commonjs 模åå¤é¨å
return callback(null, 'commonjs ' + request);
}
// 继ç»ä¸ä¸æ¥ä¸ä¸å¤é¨åå¼ç¨
callback();
},
],
};
å ¶å®ä¾å使ç¨ä¸åç模åæ ¼å¼ï¼
webpack.config.js
module.exports = {
externals: [
function (ctx, callback) {
// 该å¤é¨åç模åï¼æ¯ä¸ä¸ª `commonjs2` ç模åï¼ä¸æ¾å¨ `@scope/library` ç®å½ä¸
callback(null, '@scope/library', 'commonjs2');
},
],
};
webpack.config.js
module.exports = {
externals: [
function (ctx, callback) {
// 该å¤é¨å模åæ¯ä¸ä¸ªå
¨å±åéå«ä½ `nameOfGlobal`.
callback(null, 'nameOfGlobal');
},
],
};
webpack.config.js
module.exports = {
externals: [
function (ctx, callback) {
// 该å¤é¨å模åæ¯ä¸ä¸ªå¨`@scope/library`模åéçå½å导åºï¼named exportï¼ã
callback(null, ['@scope/library', 'namedexport'], 'commonjs');
},
],
};
webpack.config.js
module.exports = {
externals: [
function (ctx, callback) {
// å¤é¨å模åæ¯ä¸ä¸ª UMD 模å
callback(null, {
root: 'componentsGlobal',
commonjs: '@scope/components',
commonjs2: '@scope/components',
amd: 'components',
});
},
],
};
å¹é ç»å®æ£å表达å¼çæ¯ä¸ªä¾èµï¼é½å°ä»è¾åº bundle ä¸æé¤ã
webpack.config.js
module.exports = {
//...
externals: /^(jquery|\$)$/i,
};
è¿ä¸ªç¤ºä¾ä¸ï¼ææå为 jQuery
çä¾èµï¼å¿½ç¥å¤§å°åï¼ï¼æè
$
ï¼é½ä¼è¢«å¤é¨åã
ææ¶ä½ éè¦æ··ç¨ä¸é¢ä»ç»çè¯æ³ãè¿å¯ä»¥å以ä¸è¿æ ·æä½ï¼
webpack.config.js
module.exports = {
//...
externals: [
{
// å符串
react: 'react',
// 对象
lodash: {
commonjs: 'lodash',
amd: 'lodash',
root: '_', // indicates global variable
},
// å符串æ°ç»
subtract: ['./math', 'subtract'],
},
// å½æ°
function ({ context, request }, callback) {
if (/^yourregex$/.test(request)) {
return callback(null, 'commonjs ' + request);
}
callback();
},
// æ£å表达å¼
/^(jquery|\$)$/i,
],
};
å ³äºå¦ä½ä½¿ç¨æ¤ externals é ç½®çæ´å¤ä¿¡æ¯ï¼è¯·åè å¦ä½ç¼å libraryã
function
object
æå±æå® externalsï¼
webpack.config.js
module.exports = {
externals: {
byLayer: {
layer: {
external1: 'var 43',
},
},
},
};
string = 'var'
æå® externals çé»è®¤ç±»åãå½ external 被设置为 amd
ï¼umd
ï¼system
以å jsonp
æ¶ï¼output.libraryTarget
çå¼ä¹åºç¸åãä¾å¦ï¼ä½ åªè½å¨ amd
åºä¸ä½¿ç¨ amd
ç externalsã
æ¯æçç±»åå¦ä¸ï¼
'amd'
'amd-require'
'assign'
- same as 'var'
'commonjs'
'commonjs-module'
'global'
'import'
- uses import()
to load a native EcmaScript module (async module)'jsonp'
'module'
'node-commonjs'
'promise'
- ä¸ 'var'
ç¸åï¼ä½æ¯ä¼ await ç»æï¼éç¨äº async 模åï¼'self'
'system'
'script'
'this'
'umd'
'umd2'
'var'
'window'
webpack.config.js
module.exports = {
//...
externalsType: 'promise',
};
å°å¤é¨çé»è®¤ç±»åæå®ä¸ºâcommonjsâãWebpack å°ä¸ºæ¨¡åä¸ä½¿ç¨çå¤é¨çæ类似 const X = require('...')
ç代ç ã
import fs from 'fs-extra';
webpack.config.js
module.exports = {
// ...
externalsType: 'commonjs',
externals: {
'fs-extra': 'fs-extra',
},
};
å°ä¼è½¬æ¢ä¸ºç±»ä¼¼ä¸é¢ç代ç ï¼
const fs = require('fs-extra');
请注æï¼è¾åºäº§ç©ä¸ä¼æä¸ä¸ª require()
ã
å°å¤é¨çé»è®¤ç±»åæå®ä¸º 'global'
ãWebpack ä¼å° external ä½ä¸ºå
¨å±åé读åå° globalObject
ã
import jq from 'jquery';
jq('.my-element').animate(/* ... */);
webpack.config.js
module.exports = {
// ...
externalsType: 'global',
externals: {
jquery: '$',
},
output: {
globalObject: 'global',
},
};
å°ä¼è½¬æ¢ä¸ºç±»ä¼¼ä¸é¢ç代ç ï¼
const jq = global['$'];
jq('.my-element').animate(/* ... */);
å° externals ç±»å设置为 'module'
ï¼webpack å°ä¼å¨ module ä¸ä¸ºå¤é¨å¼ç¨çæå½¢å¦ import * as X from '...'
ç代ç ã
ç¡®ä¿é¦å
æ experiments.outputModule
设置为 true
, å¦å webpack å°ä¼æ¥éã
import jq from 'jquery';
jq('.my-element').animate(/* ... */);
webpack.config.js
module.exports = {
experiments: {
outputModule: true,
},
externalsType: 'module',
externals: {
jquery: 'jquery',
},
};
å°ä¼è½¬æ¢ä¸ºç±»ä¼¼ä¸é¢ç代ç ï¼
import * as __WEBPACK_EXTERNAL_MODULE_jquery__ from 'jquery';
const jq = __WEBPACK_EXTERNAL_MODULE_jquery__['default'];
jq('.my-element').animate(/* ... */);
请注æï¼è¾åºäº§ç©ä¸ä¼æä¸ä¸ª import
è¯å¥ã
å° externals ç±»å设置为 'node-commonjs'
ï¼webpack å°ä» module
ä¸å¯¼å
¥ createRequire
æ¥æé ä¸ä¸ª require å½æ°ï¼ç¨äºå 载模åä¸ä½¿ç¨çå¤é¨å¯¹è±¡ã
import jq from 'jquery';
jq('.my-element').animate(/* ... */);
webpack.config.js
module.export = {
experiments: {
outputModule: true,
},
externalsType: 'node-commonjs',
externals: {
jquery: 'jquery',
},
};
å°ä¼è½¬æ¢ä¸ºç±»ä¼¼ä¸é¢ç代ç ï¼
import { createRequire } from 'module';
const jq = createRequire(import.meta.url)('jquery');
jq('.my-element').animate(/* ... */);
请注æï¼è¾åºå
ä¸ä¼æä¸ä¸ª import
è¯å¥ã
å°å¤é¨çé»è®¤ç±»åæå®ä¸º 'promise'
ãWebpack ä¼å° external 读å为å
¨å±åéï¼ç±»ä¼¼äº 'var'
ï¼å¹¶ä¸ºå®æ§è¡ await
ã
import jq from 'jquery';
jq('.my-element').animate(/* ... */);
webpack.config.js
module.exports = {
// ...
externalsType: 'promise',
externals: {
jquery: '$',
},
};
å°ä¼è½¬æ¢ä¸ºç±»ä¼¼ä¸é¢ç代ç ï¼
const jq = await $;
jq('.my-element').animate(/* ... */);
å°å¤é¨çé»è®¤ç±»åæå®ä¸º 'self'
ã Webpack ä¼å° external ä½ä¸º self
对象ä¸çå
¨å±åé读åã
import jq from 'jquery';
jq('.my-element').animate(/* ... */);
webpack.config.js
module.exports = {
// ...
externalsType: 'self',
externals: {
jquery: '$',
},
};
å°ä¼è½¬æ¢ä¸ºç±»ä¼¼ä¸é¢ç代ç ï¼
const jq = self['$'];
jq('.my-element').animate(/* ... */);
å° external çé»è®¤ç±»åæå®ä¸º 'script'
ãWebpack ä¼å° external ä½ä¸ºèæ¬å è½½ï¼å¹¶ä½¿ç¨ HTML <script>
å
ç´ æ´é²é¢å®ä¹çå
¨å±åéã<script>
æ ç¾å°å¨èæ¬å è½½å被移é¤ã
module.exports = {
externalsType: 'script',
externals: {
packageName: [
'http://example.com/script.js',
'global',
'property',
'property',
], // properties are optional
},
};
å¦æä½ ä¸æç®å®ä¹ä»»ä½å±æ§ï¼ä½ å¯ä»¥ä½¿ç¨ç®åå½¢å¼ï¼
module.exports = {
externalsType: 'script',
externals: {
packageName: 'global@http://example.com/script.js', // no properties here
},
};
请注æï¼output.publicPath
ä¸ä¼è¢«æ·»å å°æä¾ç URL ä¸ã
ä» CDN å è½½ lodash
ï¼
webpack.config.js
module.exports = {
// ...
externalsType: 'script',
externals: {
lodash: ['https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js', '_'],
},
};
ç¶åï¼ä»£ç ä¸ä½¿ç¨æ¹å¼å¦ä¸ï¼
import _ from 'lodash';
console.log(_.head([1, 2, 3]));
ä¸é¢ç¤ºä¾æ¯é对ä¸é¢ç¤ºä¾æ°å¢äºå±æ§é ç½®ï¼
module.exports = {
// ...
externalsType: 'script',
externals: {
lodash: [
'https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js',
'_',
'head',
],
},
};
å½ä½ import 'loadsh'
æ¶ï¼å±é¨åé head
åå
¨å±åé window._
é½ä¼è¢«æ´é²ï¼
import head from 'lodash';
console.log(head([1, 2, 3])); // logs 1 here
console.log(window._.head(['a', 'b'])); // logs a here
å° external çé»è®¤ç±»åæå®ä¸º 'this'
ãWebpack ä¼å° external ä½ä¸º this
对象ä¸çå
¨å±åé读åã
ç¤ºä¾ $#example$
import jq from 'jquery';
jq('.my-element').animate(/* ... */);
webpack.config.js
module.exports = {
// ...
externalsType: 'this',
externals: {
jquery: '$',
},
};
å°ä¼è½¬æ¢ä¸ºç±»ä¼¼ä¸é¢ç代ç ï¼
const jq = this['$'];
jq('.my-element').animate(/* ... */);
å° external çé»è®¤ç±»åæå®ä¸º 'var'
ãWebpack ä¼å° external ä½ä¸ºå
¨å±åé读åã
ç¤ºä¾ $#example$
import jq from 'jquery';
jq('.my-element').animate(/* ... */);
webpack.config.js
module.exports = {
// ...
externalsType: 'var',
externals: {
jquery: '$',
},
};
å°ä¼è½¬æ¢ä¸ºç±»ä¼¼ä¸é¢ç代ç ï¼
const jq = $;
jq('.my-element').animate(/* ... */);
å° external çé»è®¤ç±»åæå®ä¸º 'window'
ãWebpack ä¼å° external ä½ä¸º window
对象ä¸çå
¨å±åé读åã
ç¤ºä¾ $#example$
import jq from 'jquery';
jq('.my-element').animate(/* ... */);
webpack.config.js
module.exports = {
// ...
externalsType: 'window',
externals: {
jquery: '$',
},
};
å°ä¼è½¬æ¢ä¸ºç±»ä¼¼ä¸é¢ç代ç ï¼
const jq = window['$'];
jq('.my-element').animate(/* ... */);
object
为ç¹å®ç target å¯ç¨ externals ç presetã
é项 | æè¿° | è¾å ¥ç±»å |
---|---|---|
electron | å° main åé¢å è½½ä¸ä¸æä¸å¸¸è§ç electron å
置模åè§ä¸º external 模åï¼å¦ electron ï¼ipc æ shell ï¼ï¼ä½¿ç¨æ¶éè¿ require() å è½½ã | boolean |
electronMain | å° main ä¸ä¸æä¸ç electron å
置模åè§ä¸º external 模åï¼å¦ app ï¼ipc-main æ shell ï¼ï¼ä½¿ç¨æ¶éè¿ require() å è½½ã | boolean |
electronPreload | å°é¢å è½½ä¸ä¸æç electron å
置模åè§ä¸º external 模åï¼å¦ web-frame ï¼ipc-renderer æ shell ï¼ï¼ä½¿ç¨æ¶éè¿ require() å è½½ã | boolean |
electronRenderer | å° renderer ä¸ä¸æç electron å
置模åè§ä¸º external 模åï¼å¦ web-frame ãipc-renderer æ shell ï¼ï¼ä½¿ç¨æ¶éè¿ require() å è½½ã | boolean |
node | å° node.js çå
置模åè§ä¸º external 模åï¼å¦ fs ï¼path æ vm ï¼ï¼ä½¿ç¨æ¶éè¿ require() å è½½ã | boolean |
nwjs | å° NW.js éçç nw.gui 模åè§ä¸º external 模åï¼ä½¿ç¨æ¶éè¿ require() å è½½ã | boolean |
web | å° http(s)://... 以å std:... è§ä¸º external 模åï¼ä½¿ç¨æ¶éè¿ import å è½½ãï¼æ³¨æï¼è¿å°æ¹åæ§è¡é¡ºåºï¼å 为 external 代ç ä¼å¨è¯¥åä¸çå
¶ä»ä»£ç æ§è¡å被æ§è¡ï¼ã | boolean |
webAsync | å° 'http(s)://...' 以å 'std:...' çå¼ç¨è§ä¸º external 模åï¼ä½¿ç¨æ¶éè¿ async import() å è½½ãï¼æ³¨æï¼æ¤ external ç±»å为 async 模åï¼å®å¯¹æ§è¡ä¼äº§çåç§å¯ä½ç¨ï¼ã | boolean |
Note that if you're going to output ES Modules with those node.js-related presets, webpack will set the default externalsType
to node-commonjs
which would use createRequire
to construct a require function instead of using require()
.
Example
ä½¿ç¨ node
ç preset ä¸ä¼æ建å
置模åï¼èä¼å°å
¶è§ä¸º external 模åï¼ä½¿ç¨æ¶éè¿ require()
å è½½ã
webpack.config.js
module.exports = {
// ...
externalsPresets: {
node: true,
},
};