vue多页面打包配置

因业务需求中,有多页面打包的需求

经个人查阅,有两种处理方式

方式一

vue-cli版本3.x是支持多页打包的,可以直接在webpack.config.js 中添加pages,配置比较方便

  pages: {
    index: {
      entry: './src/main.js',
      template: 'index.html',
      filename: 'index.html'
    },
    index2: {
      entry: './src/main2.js',
      template: 'index2.html',
      filename: 'index2.html'
    }
  }

 方式二

vue-cli版本2.x实现多页面打包,需要配置一些文件,亲测可用

1.修改webpack.base.conf.js,配置entry添加入口,并与Chunk(后面解释)对应

entry: {
    app: './src/main.js',
    app2: './src/main2.js'
}

 2.修改webpack.dev.conf.js,在plugins下找到new HtmlWebpackPlugin,并为每个页面添加Chunk配置

例如:chunk [ 'app' ] 中的app对应的是上面web[ack.base.conf.js中entry设置的入口文件

plugins:[
    new HtmlWebpackPlugin({
      filename: 'index.html',//生成的html
      template: 'index.html',//来源html
      inject: true,//是否开启注入
      chunks: ['app']//需要引入的Chunk,不配置就会引入所有页面的资源
    }),
    new HtmlWebpackPlugin({
      filename: 'index2.html',//生成的html
      template: 'index2.html',//来源html
      inject: true,//是否开启注入
      chunks: ['app2']//需要引入的Chunk,不配置就会引入所有页面的资源
    }),
]

3.修改config下的index.js,找到build下的index:path.resolve(_dirname,'../dist/index.html') ,后添加多页配置

build: {
    index: path.resolve(__dirname, '../dist/index.html'),
    index2: path.resolve(__dirname, '../dist/index2.html')
}

4.修改webpack.prod.conf.js,在步骤2类似,在plugins下找到new HtmlWebpackPlugin,添加对应多页,filename中引用的是config/index.js中对应的build

plugins: [
    new HtmlWebpackPlugin({
        filename: config.build.index,
        template: 'index.html',
        inject: true,
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeAttributeQuotes: true
            // more options:
            // https://github.com/kangax/html-minifier#options-quick-reference
        },
        // necessary to consistently work with multiple chunks via CommonsChunkPlugin
        chunksSortMode: 'dependency',
        chunks: ['manifest','vendor','app']//需要引入的Chunk,不配置就会引入所有页面的资源
    }),
    new HtmlWebpackPlugin({
        filename: config.build.index2,
        template: 'index2.html',
        inject: true,
        minify: {
            removeComments: true,
            collapseWhitespace: true,
            removeAttributeQuotes: true
        },
        chunksSortMode: 'dependency',
        chunks: ['manifest','vendor','app2']//需要引入的Chunk,不配置就会引入所有页面的资源
    })
]

根据以上步骤配置完成后,重新打包即可,如需运行访问,可直接访问html页面

特此记录

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值