- module.exports = {
- // 其他配置
- ...,
-
- // 热更新配置
- devServer: {
- host: "localhost",
- port: 3000,
- },
- }


- // module中配置,babel-loader中启动babel缓存
- module:{
- rules:{
- test:/\.js$/,
- include:path.resolve(__dirname,'../src'),
- use:{
- loader:"babel-loader",
- options:{
- cacheDirectory:true, // 是否启动缓存
- cacheCompression:false, // 是否开启压缩
- }
- }
- }
- }
-
- // plugins中的eslint开启缓存
- plugins:[
- new ESLintPlugin({
- context: resolve("../src"),
- exclude: "node_modules",
- cache:true, // 开启缓存
- //目录为缓存目录
- cacheLocation:path.resolve(__dirname,'../node_modules/.cache/eslintcache')
- }),
- ]
_extend。默认情况下会被添加到每一个需要它的文件中。 @babel/plugin-transform-runtime: 禁用了 Babel 自动对每个文件的 runtime 注入,而是引入 @babel/plugin-transform-runtime 并且使所有辅助代码从这里引用。
- module:{
- rules:[
- {
- loader: "babel-loader",
- options: {
- cacheDirectory: true,
- cacheCompression: false,
- plugins: ["@babel/plugin-transform-runtime"], // 减小代码体积
- },
- }
- ]
- }
- document.getElementById("btn").onClick = function () {
- // eslint会对动态导入语法报错,需要修改eslint配置文件
- // webpackChunkName: "math":这是webpack动态导入模块命名的方式
- // "math"将来就会作为[name]的值显示。
- import(/* webpackChunkName: "math" */ "./js/math.js").then(({ count }) => {
- console.log(count(2, 1));
- });
- };
- {
- ...,
- output:{
- // 入口文件打包输出文件名
- filename:"static/js/main.js"
- // 图片、字体等通过type:asset处理的资源命名方式
- assetModuleFilename: "static/images/[hash:10][ext][query]",
- // 动态导入输出资源命名方式
- chunkFilename:"static/js/[name].chunk.js",
- },
- {
- test: /\.(png|jpe?g|gif)$/,
- type: "asset",
- // generator: {
- // filename: "static/images/[hash:10][ext][query]",
- // },
- parser: {
- dataUrlCondition: {
- maxSize: 10 * 1024,
- },
- },
- },
- ...,
- optimization:{
- splitChunks:{
- chunks:"all"
- }
- }
- }
- {
- ...,
- output:{
- filename: "static/js/[name].[contenthash:10].js",//这里的名字一定要使用[name]方式
- },
- ...,
- optimization:{
- ...,
- // 提取runtime文件
- runtimeChunk: {
- name: (entrypoint) => `runtime~${entrypoint.name}.js`,
- },
- }
- }
- // babel-config.js
- module.exports = {
- //@babel/preset-env 是一个智能预设,允许你使用最新的 JavaScript,而无需微观管理目标环境需要哪些语法转换
- presets: [
- [
- "@babel/preset-env",
- {
- useBuiltIns: "usage",
- corejs: 3,
- },
- ],
- ],
- };

如果看不到输出的core-js相关语法文件,查看package.json中的browserslist配置是否设置的浏览器版本比较高
- // package.json
- {
- ...,
- "browserslist": [
- "last 2 version",
- "> 1%",
- "not dead",
- "IE 10"
- ],
- }
- plugins:{
- new EsLintWebpackPlugin({
- context: resolve("./src"),
- exclude: "node_modules",
- cache: true,
- cacheLocation: resolve("./node_modules/.cache/.eslintcache"),
- }),
- }

是context不是content
- module.exports = {
- presets: [
- [
- "react-app",
- {
- runtime: "automatic",
- },
- ],
- ],
- };
- {
- ...,
- optimization:{
- splitChunks: {
- chunks: "all",
- cacheGroups:{
- react:{
- test:/[\\/]node_modules[\\/]react(.*)?[\\/]/,
- name:'chunk-react',
- priority:40
- },
- antd:{
- test:/[\\/]node_modules[\\/]antd[\\/]/,
- name:"chunk-antd",
- priority:30
- },
- lib:{
- test:/[\\/]node_modules[\\/]/,
- name:"chunk-libs",
- priority:20
- }
- }
- },
- }
-
- }