全局配置

打开 ~\build\webpack.base.conf.js ,找到entry,添加多入口

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

run dev 开发环境

修改 webpack.dev.conf.js

打开 ~\build\webpack.dev.conf.js ,在plugins下找到new HtmlWebpackPlugin,在其后面添加对应的多页,并为每个页面添加Chunk配置

chunks: ['main']中的main对应的是webpack.base.conf.js中entry设置的入口文件

plugins:[
// <a rel="nofollow" href="https://github.com/ampedandwired/html-webpack-plugin" target="_blank">https://github.com/ampedandwired/html-webpack-plugin</a>
// 多页:index.html → main.js
new HtmlWebpackPlugin({
filename: 'index.html',//生成的html
template: 'index.html',//来源html
inject: true,//是否开启注入
chunks: ['main']//需要引入的Chunk,不配置就会引入所有页面的资源
}),
// 多页:index2.html → main2.js
new HtmlWebpackPlugin({
filename: 'index2.html',//生成的html
template: 'index2.html',//来源html
inject: true,//是否开启注入
chunks: ['main2']//需要引入的Chunk,不配置就会引入所有页面的资源
}),
// 多页:index3.html → main3.js
new HtmlWebpackPlugin({
filename: 'index3.html',//生成的html
template: 'index3.html',//来源html
inject: true,//是否开启注入
chunks: ['main3']//需要引入的Chunk,不配置就会引入所有页面的资源
})
]

run build 编译

修改 config/index.js

打开~\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'),
index3: path.resolve(__dirname, '../dist/index3.html'),
},

修改 webpack.prod.conf.js

打开~\build\webpack.prod.conf.js,在plugins下找到new HtmlWebpackPlugin,在其后面添加对应的多页,并为每个页面添加Chunk配置

HtmlWebpackPlugin 中的 filename 引用的是 config/index.js 中对应的 build

plugins: [
// 多页:index.html → main.js
new HtmlWebpackPlugin({
filename: config.build.index,
template: 'index.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
// more options:
// <a rel="nofollow" href="https://github.com/kangax/html-minifier#options-quick-reference" target="_blank">https://github.com/kangax/html-minifier#options-quick-reference</a>
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency',
chunks: ['manifest','vendor','main']//需要引入的Chunk,不配置就会引入所有页面的资源
}),
// 多页:index2.html → main2.js
new HtmlWebpackPlugin({
filename: config.build.index2,
template: 'index2.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
},
chunksSortMode: 'dependency',
chunks: ['manifest','vendor','main2']//需要引入的Chunk,不配置就会引入所有页面的资源
}),
// 多页:index3.html → main3.js
new HtmlWebpackPlugin({
filename: config.build.index3,
template: 'index3.html',
inject: true,
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
},
chunksSortMode: 'dependency',
chunks: ['manifest','vendor','main3']//需要引入的Chunk,不配置就会引入所有页面的资源
}),
]

页面较多时,可以用循环将 HtmlWebpackPlugin 添加到 plugins

// utils.js
exports.getEntry = function(globPath, pathDir) {
var files = glob.sync(globPath);
var entries = {},
entry, dirname, basename, pathname, extname; for (var i = 0; i < files.length; i++) {
entry = files[i];
dirname = path.dirname(entry);
extname = path.extname(entry);
basename = path.basename(entry, extname);
pathname = path.join(dirname, basename);
pathname = pathDir ? pathname.replace(new RegExp(^_^ _^` + pathDir), '') : pathname;
entries[pathname] = ['./' + entry];
}
return entries;
}
// webpack.base.conf.js
var pages = Object.keys(utils.getEntry('../src/views/**/*.html', '../src/views/'));
pages.forEach(function (pathname) {
// <a rel="nofollow" href="https://github.com/ampedandwired/html-webpack-plugin" target="_blank">https://github.com/ampedandwired/html-webpack-plugin</a>
var conf = {
filename: '../views/' + pathname + '.html', //生成的html存放路径,相对于path
template: '../src/views/' + pathname + '.html', //html模板路径
inject: false, //js插入的位置,true/'head'/'body'/false
/*
* 压缩这块,调用了html-minify,会导致压缩时候的很多html语法检查问题,
* 如在html标签属性上使用{{...}}表达式,所以很多情况下并不需要在此配置压缩项,
* 另外,UglifyJsPlugin会在压缩代码的时候连同html一起压缩。
* 为避免压缩html,需要在html-loader上配置'html?-minimize',见loaders中html-loader的配置。
*/
// minify: { //压缩HTML文件
// removeComments: true, //移除HTML中的注释
// collapseWhitespace: false //删除空白符与换行符
// }
};
if (pathname in config.entry) {
conf.favicon = 'src/images/favicon.ico';
conf.inject = 'body';
conf.chunks = ['vendors', pathname];
conf.hash = true;
}
config.plugins.push(new HtmlWebpackPlugin(conf));
});

同样入口 entry 也可以使用

// webpack.base.conf.js
entry: {
app: utils.getEntry('../src/scripts/**/*.js', '../src/scripts/')
},

引用自如何使用 vue-cli 开发多页应用方法

vue多页面项目配置的更多相关文章

  1. Webpack + Vue 多页面项目升级 Webpack 4 以及打包优化

    0. 前言 早在 2016 年我就发布过一篇关于在多页面下使用 Webpack + Vue 的配置的文章,当时也是我在做自己一个个人项目时遇到的配置问题,想到别人也可能遇到跟我同样的问题,就把配置的思 ...

  2. webpack4 + vue多页面项目精细构建思路

    #构建思路 虽然当前前端项目多以单页面为主,但多页面也并非一无是处,在一些情况下也是有用武之地的,比如: 项目庞大,各个业务模块需要解耦 SEO更容易优化 没有复杂的状态管理问题 可以实现页面单独上线 ...

  3. vue多页面项目搭建(vue-cli 4.0)

    1.创建vue项目 cmd命令执行 vue create app (app 自定义的项目名) 一般都会选择后者,自己配置一下自己需要的选项(空格为选中) 这是我个人需要的一些选项,路由Router.状 ...

  4. vue单页面项目架构方案

    这里的架构方案是基于vue-cli2生成的项目应用程序产生的,是对项目应用程序或者项目模板的一些方便开发和维护的封装.针对单页面的解决方案. 主要有四个方面: 一,不同环境下的分别打包 主要是测试环境 ...

  5. 大型vue单页面项目优化总结

    这是之前在公司oa项目优化时罗列的优化点,基本都已经完成,当时花了点心思整理的,保存在这里,方便以后其他项目用到查漏补缺. 1.打包文件中的app.js文件放入cdn,加快页面首次加载速度 2.提取公 ...

  6. vue单页面项目返回上一页无效,链接变化了,但是页面没有变化

    在最近的项目中,返回上一页没有效果,经过好久的排查才发现问题,是路由守卫写法不规范导致. 在项目中用路由守卫做了登录拦截,没登录的跳转到登录页面.页面跳转和拦截都没问题,但是返回上一页就不行了,也没有 ...

  7. vue单页面项目中解决安卓4.4版本不兼容的问题

    1.cnpm安装 cnpm i babel-polyfill --save cnpm i es6-promise --save 2.main.js引入 import ‘babel-polyfill‘ ...

  8. Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(上篇——纯前端多页面)

    Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(上篇--纯前端多页面) @(HTML/JS) 一般来说,使用vue做成单页应用比较好,但特殊情况下,需要使用多页面也有另外 ...

  9. Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(下篇——多页面VueSSR+热更新Server)

    Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(下篇--多页面VueSSR+热更新Server) @(HTML/JS) 这是Vue多页面框架系列文章的第二篇,上一篇(纯前 ...

随机推荐

  1. 【笔记篇】C#笔记1

    返回目录:目录请戳这里~ 以后的C#笔记如果不出意外的话都是Win10 Professional + VS2015 Professional出的,(当然还有直接在编译框敲的所以能不能过编译我也不知道┑ ...

  2. Luogu P4180 【模板】严格次小生成树[BJWC2010]

    P4180 [模板]严格次小生成树[BJWC2010] 题意 题目描述 小\(C\)最近学了很多最小生成树的算法,\(Prim\)算法.\(Kurskal\)算法.消圈算法等等.正当小\(C\)洋洋得 ...

  3. postgresql数据库安装后的pgadmin4中无法加载本地连接解决办法

    postgresql 在安装最后一步提示the database cluster initialisation failed, 而后点开pgadmin4发现如下图所示 经过百度搜索找出问题原因, 由于 ...

  4. 【Uva 12128】Perfect Service

    [Link]: [Description] 给你n个机器组成的一棵树,然后,让你在某些机器上安装服务器. 要求,每个机器如果没有安装服务器,都要恰好和一个安装了服务器的机器连接. 问你,最少要安装多少 ...

  5. 洛谷P4027 [NOI2007]货币兑换

    P4027 [NOI2007]货币兑换 算法:dp+斜率优化 题面十分冗长,题意大概是有一种金券每天价值会有变化,你可以在某些时间点买入或卖出所有的金券,问最大收益 根据题意,很容易列出朴素的状态转移 ...

  6. Zuul的过滤器

    过滤器类型与请求生命周期:         Zuul中定义了4种标准过滤器类型,这些过滤器类型对应于请求的典型生命周期         PRE: 这种过滤器在请求被路由之前调用.可利用这种过滤器实现身 ...

  7. JS Math.sin() 与 Math.cos() 用法 (含圆上每个点的坐标)

    Math.sin(x)      x 的正玄值.返回值在 -1.0 到 1.0 之间: Math.cos(x)    x 的余弦值.返回的是 -1.0 到 1.0 之间的数: 这两个函数中的X 都是指 ...

  8. 关于H5裁剪图片后,直传阿里云的一些问题

    这段时间在工作中碰到一个需要在h5裁剪图像,然后直传阿里云的需求.图中遇到了一些小问题,分享出来大家都看看. h5裁剪图像:cropper.js是一个神器啊关于用法,网上可以收罗出大量的帖子,这里我就 ...

  9. iOS之CGAffineTransform属性详解和方法使用

    1.CGAffineTransform简介 UIView有个属性transform,是CGAffineTransform类型.可以使其在二维界面做旋转.平移.缩放单独或者组合动画! CGAffineT ...

  10. iOS之CGPath的应用(二)

    1.矩形路径 CG_EXTERN CGPathRef CGPathCreateWithRect(CGRect rect, const CGAffineTransform * __nullable tr ...