9、webpack多页应用

作者: 圆梦人生 | 来源:发表于2019-08-07 16:19 被阅读0次

webpack支持多页应用,需要配置入口和出口

  • 1、入口配置
entry: {
  home: './src/index.js',
  other: './src/other.js',
},
  • 2、出口配置
output: {   
  // 打包后的文件名 多入口根据入口名称生成
  filename: '[name].js',  
  // 输出后的路径(必须是一个绝对路径)
  path: path.resolve(__dirname, 'dist')
}
  • 3、 插件配置
// 插件配置
plugins: [
  new HtmlWebpackPlugins({
    // 模板位置
    template: 'index.html',
    // 文件名
    filename: 'home.html',
    chunks: ['home']
  }),
  new HtmlWebpackPlugins({
    // 模板位置
    template: 'index.html',
    // 文件名
    filename: 'other.html',
    chunks: ['other']
 })
]

webpack.config.js完整配置

//  webpack是node写出来的, 需要node的写法
let path = require('path');
// path.resolve 相对路径转成绝对路径
// console.log(path.resolve('dist'));
// 以当前目录
// console.log(__dirname);

// html-webpack-plugins 插件
let HtmlWebpackPlugins = require('html-webpack-plugin');


// webpack相关配置
module.exports = {
    // 开发服务的配置
    devServer: {
        // 端口,默认8080
        port: '8099',
        // 进度条
        progress: true,
        // 启动后访问目录,默认是项目根目录,这个设置到打包后目录
        contentBase: './build',
        // 启动压缩
        //compress: true
    },
    // 模式,2种:生产模式(production)和开发模式(development)
    // 开发模式不压缩打包后代码,生产模式压缩打包后代码
    mode: 'development',
    // 入口,表示从哪里开始打包
    entry: {
        home: './src/index.js',
        other: './src/other.js',
    },   
    // 表示出口(输出后文件相关配置)
    output: {   
        // 打包后的文件名 多入口根据入口名称生成
        filename: '[name].js',  
        // 输出后的路径(必须是一个绝对路径)
        path: path.resolve(__dirname, 'dist')
    },
    // 插件配置
    plugins: [
        new HtmlWebpackPlugins({
            // 模板位置
            template: 'index.html',
            // 文件名
            filename: 'home.html',
            chunks: ['home']
        }),
        new HtmlWebpackPlugins({
            // 模板位置
            template: 'index.html',
            // 文件名
            filename: 'other.html',
            chunks: ['other']
        })
    ]
}

相关文章

  • 9、webpack多页应用

    webpack支持多页应用,需要配置入口和出口 1、入口配置 2、出口配置 3、 插件配置 webpack.con...

  • 前端工程化

    场景 前后端完全分离前后端分离还看是单则应用还是多页应用单页应用用webpack,比较强势。 多页应用用webpa...

  • webpack4 构建vue多页工程

    webpack4 构建vue多页工程 多页应用开发环境配置 构建开发环境的配置,需要使用到webpack-dev-...

  • webpack 入门 2

    多页应用打包 例如 webpack.config.js 内容 7.resolve 配置(webpack 如何寻找模...

  • webpack多页应用配置

    说明: 网上很多教程,大各种各样的内容都有,自己研究的时候查了很多文档,出现了一些错误都一一记录下来.并且配置文件...

  • webpack多页应用打包

    场景:有两个页面 index.html, other.html主入口文件分别为:index.js 和 other...

  • webpack打包多页应用

    假设有两个入口文件home.js和other.js需要打包

  • webpack常用配置

    这里记录一下webpack常用配置打包多页应用 html-webpack-plugin loader 让 webp...

  • 【实战】webpack4 + ejs + express 带你撸

    最终解决方案 多页应用项目架构最终解决方案发布啦~ 【实战】webpack4 + ejs + egg 多页应用项目...

  • react学习笔记

    使用webpack打包react多页应用 demo地址: https://github.com/zhuweileo...

网友评论

    本文标题:9、webpack多页应用

    本文链接:https://www.haomeiwen.com/subject/xcfadctx.html