官网:https://webpack.docschina.org/configuration/externals/
作用:防止将某些 import 的包(package)打包到 bundle 中,而是在运行时(runtime)再去从外部获取这些扩展依赖(external dependencies)。
(通俗地说,就是)指示webpack忽略哪些包,不参与打包
eg:将vue、element-ui、jquery不进行打包,采用BootCDN的方式引入
const { resolve } = require('path');const HtmlWebpackPlugin = require('html-webpack-plugin');module.exports = {entry: './src/js/index.js',output: {filename: 'js/built.js',path: resolve(__dirname, 'build')},plugins: [new HtmlWebpackPlugin({template: './src/index.html'})],mode: 'production',externals: {// 拒绝jQuery被打包进来jquery: 'jQuery'}};
语法:
externals ——-> JSON Obeject(对象内的键值对是指示被忽略的包)
- 键名:忽略的库名(npm install xxx,即install的是啥)
- 键值:全局对象(import $ from ‘jquery’中的$, 也可以是jQuery)
module.exports = {externals: {jquery: 'jQuery' // 表示应该排除 import $ from 'jquery' 中的 jquery 模块}}
