前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >5-13 多页面打包配置

5-13 多页面打包配置

作者头像
love丁酥酥
发布2020-06-02 15:37:24
4780
发布2020-06-02 15:37:24
举报
文章被收录于专栏:coding for love

1. 简介

前面的配置基本上是基于 spa,不过如果细心的同学可能会发现,之前在 5-5 webapck-dev-server 解决单页应用路由问题 一文中已经用到了多页面打包。其实,多页应用很简单,不过是指定多个入口,多个对应输出,以及将输出正确放到 html 中即可。

2. 配置多页面

代码语言:javascript
复制
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const path = require('path');

module.exports = {
  entry: {
    index: './src/index.jsx',
    list: './src/list.jsx',
  },
  output: {
    path: path.resolve(__dirname, '../dist'),
    filename: '[name].js',
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node-modules/,
        use: ['babel-loader'],
      },
      {
        test: /\.(jpg|jpeg|png|gif)$/,
        use: {
          loader: 'url-loader',
          options: {
            name: '[name].[ext]',
            limit: 2048,
          },
        },
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(eot|svg|ttf|woff)$/,
        use: 'file-loader',
      },
    ],
  },
  optimization: {
    runtimeChunk: {
      name: 'runtime',
    },
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10,
          name: 'vendors',
        },
      },
    },
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'index.html',
    }),
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'list.html',
    }),
    new CleanWebpackPlugin(),
  ],
};
代码语言:javascript
复制
<!--src/index.html-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>esmodule-oop</title>
</head>
<body>
    <div id="root"></div>
</body>
</html>
代码语言:javascript
复制
// src/index.jsx
import React, { Component } from 'react';
import ReactDom from 'react-dom';

class App extends Component {
  render() {
    return <div>index page</div>;
  }
}

ReactDom.render(<App />, document.getElementById('root'));
代码语言:javascript
复制
// src/index.jsx
import React, { Component } from 'react';
import ReactDom from 'react-dom';

class App extends Component {
  render() {
    return <div>index page</div>;
  }
}

ReactDom.render(<App />, document.getElementById('root'));

bpm run dev 后,打开 index.html 和 list.html 都展示

image.png

这是因为 HtmlWebpackPlugin 默认引入所有的 chunks,如果不想引入所有 chunks,我们需要使用 chunks 和 excludeChunks 手动指定。

3. chunks 和 excludeChunks

我们使用如下配置:

代码语言:javascript
复制
plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'index.html',
      chunks: ['runtime', 'vendors', 'index'],
    }),
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'list.html',
      excludeChunks: ['index'],
    }),
    new CleanWebpackPlugin(),
  ],

打包后得出的结果就正常了。入口较多时,我们可以简化一下写法如下:

代码语言:javascript
复制
// build/webpack.common.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const path = require('path');

const entry = {
  index: './src/index.jsx',
  list: './src/list.jsx',
};

const plugins = [
  new CleanWebpackPlugin(),
];

Object.keys(entry).forEach((name) => {
  plugins.push(new HtmlWebpackPlugin({
    template: './src/index.html',
    filename: `${name}.html`,
    chunks: ['runtime', 'vendors', name],
  }));
});


module.exports = {
  entry,
  output: {
    path: path.resolve(__dirname, '../dist'),
    filename: '[name].js',
  },
  resolve: {
    extensions: ['.js', '.jsx'],
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node-modules/,
        use: ['babel-loader'],
      },
      {
        test: /\.(jpg|jpeg|png|gif)$/,
        use: {
          loader: 'url-loader',
          options: {
            name: '[name].[ext]',
            limit: 2048,
          },
        },
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(eot|svg|ttf|woff)$/,
        use: 'file-loader',
      },
    ],
  },
  optimization: {
    runtimeChunk: {
      name: 'runtime',
    },
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10,
          name: 'vendors',
        },
      },
    },
  },
  plugins,
};

参考

webpack-docs/html-webpack-plugin/

npm/html-webpack-plugin

webpack4 之html-webpack-plugin

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 简介
  • 2. 配置多页面
  • 3. chunks 和 excludeChunks
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档