externals
防止将某些 import 的包 打包 到bundle中,而是在运行时从外部获取这些拓展依赖,
例如从 CDN 引入 JQ,而不是把他打包:
我们在 入口文件(index.html) 中用script标签引入JQ,然后在,配置文件中配置:
<script
src="https://code.jquery.com/jquery-3.1.0.js"
integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk="
crossorigin="anonymous"
></script>
externals: {
jquery: 'jQuery',
},
但是这样我们每次都需要在 index.html 中手动的添加 script CDN链接,这样很不方便,于是我们可以这样配置:
externalsType: 'script', // 在什么标签中引入
externals: {
jquery: [
'https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js',
'$
]
},
这样我们只需要在 webpack.config.js 中配置就可以成功引入 jq