手动打包多页面入口
我们之前打包的都是单页面,那么如何多包多页面的项目呢(一个项目中存在多个html文件)
chapter├─babel.config.js├─package-lock.json├─package.json├─postcss.config.js├─src| ├─index.html| ├─index.js| └list.js├─dell| ├─vendors.dell.js| └vendors.dell.js.LICENSE.txt├─build| ├─webpack.common.js| ├─webpack.dell.js| ├─webpack.dev.js| └webpack.prod.js
src/index.js文件:
import React, { Component } from "react";import ReactDom from "react-dom";class App extends Component{Super(){}render(){return(<div><div>this is index</div></div>)}}ReactDom.render(<App />,document.getElementById("root"));
src/list.js文件:
import React, { Component } from "react";import ReactDom from "react-dom";class App extends Component{Super(){}render(){return(<div><div>this is List</div></div>)}}ReactDom.render(<App />,document.getElementById("root"));
如果我想要以上两个**JS**文件打包进两个**html**文件该如何进行配置呢?
1、增加打包入库
2、增加HtmlWebpackPlugin的配置,且进行配置filename和所需要用到的模块文件chunks:
// ...const commonConfig = {// 项目从哪个文件开始打包entry: {main: "./src/index.js",list: "./src/list.js"},// ...plugins: [// 自动引入打包好的js文件new HtmlWebpackPlugin({template: "./src/index.html",filename: "index.html",chunks: ["vendors", "main"]}),new HtmlWebpackPlugin({template: "./src/index.html",filename: "list.html",chunks: ["vendors", "list"]}),// ...],}module.exports = (env) => {if (env && env.production) {return merge(commonConfig, prodConfig)} else {return merge(commonConfig, devConfig)}}
然后运行npm run build就能看到dist文件夹下生成了两个html文件。
自动打包多页面入口
以上就是如何打包多页面配置?我们发现如何新建一个html文件就需要我们在entry里面增加入口,plugin里面增加配置,那么如何自动完成这些操作呢
将plugins单独抽离出来:
const path = require("path");const HtmlWebpackPlugin = require("html-webpack-plugin");const { CleanWebpackPlugin } = require('clean-webpack-plugin');const { merge } = require("webpack-merge");const addAssetsHtmlWebpackPlugin = require("add-asset-html-webpack-plugin")const devConfig = require("./webpack.dev")const prodConfig = require("./webpack.prod")const markPlugin = (configs) => {let pluginArr = [// 清除dist文件夹new CleanWebpackPlugin({cleanOnceBeforeBuildPatterns: [path.resolve(__dirname, '../dist')],}),// 将静态资源添加到html中new addAssetsHtmlWebpackPlugin({filepath: path.resolve(__dirname, "../dell/vendors.dell.js")})];Object.keys(configs.entry).forEach(item => {pluginArr.push(new HtmlWebpackPlugin({template: "./src/index.html",filename: `${item}.html`,chunks: ["vendors", item]}),)})return pluginArr;}const commonConfig = {// 项目从哪个文件开始打包entry: {main: "./src/index.js",list: "./src/list.js",detail: "./src/detail.js"},// 项目打包输出的配置output: {// 打包后输出的路径(不写也是可以的,因为这就是默认的路径,建议w写上更加直观),__dirname 表示当前目录下path: path.resolve(__dirname, "../dist"),},resolve: {// 自动帮我们把文件后缀补齐(从左到右)extensions: [".js", ".jsx"],// 自动帮我们查找文件(从左到右)mainFiles: ["index", "child"],// 别名,根据配置的别名查找路径alias: {"@": path.relative(__dirname, "src/coms")}},module: {rules: [{test: /\.jpg|.jpeg|.png$/,use: {loader: "url-loader",options: {limit: 54000,name: "[name].[ext]",outputPath: "/images",}}}, {test: /\.m?js$/,exclude: /node_modules/, // 排除node_modules下的代码use: {loader: "babel-loader"}}]},optimization: {// TreeShaking 的配置usedExports: true,// SplittingCode 的配置splitChunks: {chunks: 'all', // 分割引入代码库的方式,默认为 async 异步,可选 all:同步和异步cacheGroups: {vendors: {test: /[\\/]node_modules[\\/]/,priority: -10,name: "chunk"}}},},// plugins: [// // 自动引入打包好的js文件// new HtmlWebpackPlugin({// template: "./src/index.html",// filename: "index.html",// chunks: ["vendors", "main"]// }),// new HtmlWebpackPlugin({// template: "./src/index.html",// filename: "list.html",// chunks: ["vendors", "list"]// }),// // 清除dist文件夹// new CleanWebpackPlugin({// cleanOnceBeforeBuildPatterns: [// path.resolve(__dirname, '../dist')// ],// }),// // 将静态资源添加到html中// new addAssetsHtmlWebpackPlugin({// filepath: path.resolve(__dirname, "../dell/vendors.dell.js")// })// ],}commonConfig.plugins = markPlugin(commonConfig);module.exports = (env) => {if (env && env.production) {return merge(commonConfig, prodConfig)} else {return merge(commonConfig, devConfig)}}
以上代码把plugins单独抽离出来,根据entry的对象进行循环,这样每次新建一个html文件我们只需要到entry里面增加一个配置,就可以啦。
