externals

防止将某些 import 的包 打包 到bundle中,而是在运行时从外部获取这些拓展依赖,
例如从 CDN 引入 JQ,而不是把他打包:
我们在 入口文件(index.html) 中用script标签引入JQ,然后在,配置文件中配置:

  1. <script
  2. src="https://code.jquery.com/jquery-3.1.0.js"
  3. integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk="
  4. crossorigin="anonymous"
  5. ></script>
  1. externals: {
  2. jquery: 'jQuery',
  3. },

但是这样我们每次都需要在 index.html 中手动的添加 script CDN链接,这样很不方便,于是我们可以这样配置:

  1. externalsType: 'script', // 在什么标签中引入
  2. externals: {
  3. jquery: [
  4. 'https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js',
  5. '$
  6. ]
  7. },

这样我们只需要在 webpack.config.js 中配置就可以成功引入 jq