webpack配置【六】配置多页面

1.修改webpack.config.js【注释掉生成html的插件】

const path=require('path');
//生成html
// const HtmlWebpackPlugin=require('html-webpack-plugin');
//css打包分离
const ExtractTextPlugin = require('extract-text-webpack-plugin'); 
//压缩
const BabiliPlugin = require('babili-webpack-plugin');
//分析包的大小
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

const PATHS={
  app: path.join(__dirname, 'app'),
  build: path.join(__dirname, 'build'),
};
//css打包分离
const plugin = new ExtractTextPlugin({
  filename: '[name].css',
  ignoreOrder: true,
}); 

module.exports={
  devServer:{ 
    host: process.env.HOST, //Defaults to 'localhost
    port: 80,  //Defalut to 8080
    // overlay: true captures only errors
    overlay: {
      errors: true,
      warnings: true,
    },
  },
  performance:{
    hints:'warning',//'error'
    maxEntrypointSize: 500000,// bytes
    maxAssetSize: 450000,// bytes
  },
  entry:{
    app:PATHS.app,
    vendor: ['react']
  },
  output: {
    path: PATHS.build,
    filename: '[name].js',
  },
  //解决开发环境下压缩插件报错问题
  devtool: 'source-map',
  module:{
    rules:[
      {
        test: /\.js$/,
        enforce: 'pre',
        
        loader:'eslint-loader',
        options:{
          emitWarning: true,
        },
      },
      //css打包分离
      {
        test: /\.css$/,
        exclude: /node_modules/,
        use: plugin.extract({
          use: {
            loader: 'css-loader',
            options: {
              modules: true,
            },
          },
          fallback : 'style-loader',
        }),
      },
    ],
  },
  
  plugins:[
    //生成html
    // new HtmlWebpackPlugin({
    //   title: 'Webpack demo',
    // }),
    //css打包分离
    plugin,
    //压缩
    new BabiliPlugin(),
    //分析工具
    new BundleAnalyzerPlugin()

  ],
  //分离组件
  optimization: {
    splitChunks: {
        cacheGroups: {
            commons: {
                name: "commons",
                chunks: "initial",
                minChunks: 2
            }
        }
    }
  },
};

2.再根目录下创建一个index.html和about.html

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./build/index.css">
</head>
<body>
    <p>this is index page.</p>
    <!-- 先使用vendor.js,再使用app.js -->
    <script src="./build/vendor.js"></script>
    <script src="./build/index.js"></script>
</body>
</html>

about.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./build/index.css">
</head>
<body>
    <p>this is about page.</p>
    <!-- 先使用vendor.js,再使用app.js -->
    <script src="./build/vendor.js"></script>
    <script src="./build/about.js"></script>
</body>
</html>

3.再app的文件夹下创建一个about.js

console.log('this is about js output...');

4.修改webpack.config.js

  entry:{
    // app:PATHS.app,
    index: './app/index.js',
    about: './app/about.js',
    vendor: ['react']
  },

5.运行命令

webpack
npm run start

6.路径查看

http://localhost/index.html
http://localhost/about.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值