使用 webpack 打包多页面应用(Multiple-Page Application)

使用 webpack 打包多页面应用(Multiple-Page Application)

多页面网站同样可以用 webpack 来打包,以便使用 npm 包,import(),code splitting 等好处。

MPA 意味着并没不是一个单一的 html 入口和 js 入口,而是每个页面对应一个 html 和多个 js。那么我们可以把项目结构设计为:

├── dist├── package.json├── node_modules├── src│   ├── components│   ├── shared|   ├── favicon.png│   └── pages                 页面放这里|       ├── foo               编译后生成 http://localhost:8080/foo.html|       |    ├── index.html|       |    ├── index.js|       |    ├── style.css|       |    └── pic.png|       └── bar                        http://localhost:8080/bar.html|           ├── index.html|           ├── index.js|           ├── style.css|           └── baz                    http://localhost:8080/bar/baz.html|               ├── index.html|               ├── index.js|               └── style.css└── webpack.config.js

这里每个页面的 index.html 是个完整的从 <​!DOCTYPE html> 开头到 <​/html> 结束的页面,这些文件都要用 html-webpack-plugin 处理。index.js 是每个页面的业务逻辑,作为每个页面的入口 js 配置到 entry 中。这里我们需要用 glob 库来把这些文件都筛选出来批量操作。为了使用 webpack 4 的 optimization.splitChunks 和 optimization.runtimeChunk 功能,我写了 html-webpack-include-sibling-chunks-plugin 插件来配合使用。还要装几个插件把 css 压缩并放到 <​head> 中。

npm install glob html-webpack-include-sibling-chunks-plugin uglifyjs-webpack-plugin mini-css-extract-plugin optimize-css-assets-webpack-plugin --save-dev

webpack.config.js 修改的地方:

// ...const UglifyJsPlugin = require('uglifyjs-webpack-plugin')const MiniCssExtractPlugin = require('mini-css-extract-plugin')const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')const HtmlWebpackIncludeSiblingChunksPlugin = require('html-webpack-include-sibling-chunks-plugin')const glob = require('glob')const dev = Boolean(process.env.WEBPACK_SERVE)const config = require('./config/' + (process.env.npm_config_config || 'default'))const entries = glob.sync('./src/**/index.js')const entry = {}const htmlPlugins = []for (const path of entries) {  const template = path.replace('index.js', 'index.html')  const chunkName = path.slice('./src/pages/'.length, -'/index.js'.length)  entry[chunkName] = dev ? [path, template] : path  htmlPlugins.push(new HtmlWebpackPlugin({    template,    filename: chunkName + '.html',    chunksSortMode: 'none',    chunks: [chunkName]  }))}module.exports = {  entry,  output: {    path: resolve(__dirname, 'dist'),    // 我们不定义 publicPath,否则访问 html 时需要带上 publicPath 前缀    filename: dev ? '[name].js' : '[chunkhash].js',    chunkFilename: '[chunkhash].js'  },  optimization: {    runtimeChunk: true,    splitChunks: {      chunks: 'all'    },    minimizer: dev ? [] : [      new UglifyJsPlugin({        cache: true,        parallel: true,        sourceMap: true      }),      new OptimizeCSSAssetsPlugin()    ]  },  module: {    rules: [      // ...      {        test: /\.css$/,        use: [dev ? 'style-loader' : MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader']      },      // ...    ]  },  plugins: [    // ...    /*    这里不使用 [chunkhash]    因为从同一个 chunk 抽离出来的 css 共享同一个 [chunkhash]    [contenthash] 你可以简单理解为 moduleId + content 生成的 hash    因此一个 chunk 中的多个 module 有自己的 [contenthash]    */    new MiniCssExtractPlugin({      filename: '[contenthash].css',      chunkFilename: '[contenthash].css'    }),    // 必须放在html-webpack-plugin前面    new HtmlWebpackIncludeSiblingChunksPlugin(),    ...htmlPlugins  ],  // ...}

entry 和 htmlPlugins 会通过遍历 pages 目录生成,比如:

entry:

{  'bar/baz': './src/pages/bar/baz/index.js',  bar: './src/pages/bar/index.js',  foo: './src/pages/foo/index.js'}

在开发环境中,为了能够修改 html 文件后网页能够自动刷新,我们还需要把 html 文件也加入 entry 中,比如:

{  foo: ['./src/pages/foo/index.js', './src/pages/foo/index.html']}

这样,当 foo 页面的 index.js 或 index.html 文件改动时,都会触发浏览器刷新该页面。虽然把 html 加入 entry 很奇怪,但放心,不会导致错误。记得不要在生产环境这么做,不然导致 chunk 文件包含了无用的 html 片段。

htmlPlugins:

[  new HtmlWebpackPlugin({    template: './src/pages/bar/baz/index.html',    filename: 'bar/baz.html',    chunksSortMode: 'none',    chunks: ['bar/baz']  },  new HtmlWebpackPlugin({    template: './src/pages/bar/index.html',    filename: 'bar.html',    chunksSortMode: 'none',    chunks: ['bar']  },  new HtmlWebpackPlugin({    template: './src/pages/foo/index.html',    filename: 'foo.html',    chunksSortMode: 'none',    chunks: ['foo']  }]

代码在 examples/mpa 目录。