night-cherry 3 долоо хоног өмнө
parent
commit
f53ca39d5e
1 өөрчлөгдсөн 42 нэмэгдсэн , 8 устгасан
  1. 42 8
      webpack.config.js

+ 42 - 8
webpack.config.js

@@ -6,11 +6,16 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
 const { VueLoaderPlugin } = require('vue-loader');
 
 module.exports={
-  entry:"./index.js",
+  entry:{
+    index:"./index.js",
+    sample:"./utils/sample.js",
+  },
   output:{
-    filename:"bundle.js",
+    filename:"[name].js",
     path:path.resolve(__dirname,"./dist"),
     assetModuleFilename:"media/[name].[ext]",
+    publicPath: '/',  // 静态资源公共路径
+    clean: true       // 构建前清空 dist 目录
   },
   resolve: {
     alias: {
@@ -32,8 +37,13 @@ module.exports={
       // 处理 .js 文件
     {
       test: /\.js$/,
-      loader: 'babel-loader',
-      exclude: /node_modules/
+      exclude: /node_modules/,
+      use: {
+        loader: 'babel-loader',
+        options: {
+          presets: ['@babel/preset-env']
+        }
+      }
     },
     {
      test:/\.(png|jpe?g|gif|svg)$/i,
@@ -71,8 +81,32 @@ module.exports={
     new CleanWebpackPlugin(),
     new HtmlWebpackPlugin({
       inject:'body',
-      title:"webpack sample ",
-      template:"./index.html"
-    })
-  ]
+      title:"index page ",
+      template:"./index.html",
+      filename:"index.html",
+      chunks:['index'],
+    }),
+    new HtmlWebpackPlugin({
+      inject:'body',
+      title:"sample page",
+      template:"./pages/sample.html",
+      filename:"sample.html",
+      chunks:['sample'],
+    }),
+  ],
+  // 优化配置(如代码拆分)
+  optimization: {
+    splitChunks: {
+      chunks: 'all',
+      minSize: 20000,
+      cacheGroups: {
+        vendors: {
+          test: /[\\/]node_modules[\\/]/,
+          priority: -10,
+          reuseExistingChunk: true,
+          name: 'vendors'
+        }
+      }
+    }
+  }
 }