vue多页面项目配置
全局配置
打开 ~\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多页面项目配置的更多相关文章
- Webpack + Vue 多页面项目升级 Webpack 4 以及打包优化
0. 前言 早在 2016 年我就发布过一篇关于在多页面下使用 Webpack + Vue 的配置的文章,当时也是我在做自己一个个人项目时遇到的配置问题,想到别人也可能遇到跟我同样的问题,就把配置的思 ...
- webpack4 + vue多页面项目精细构建思路
#构建思路 虽然当前前端项目多以单页面为主,但多页面也并非一无是处,在一些情况下也是有用武之地的,比如: 项目庞大,各个业务模块需要解耦 SEO更容易优化 没有复杂的状态管理问题 可以实现页面单独上线 ...
- vue多页面项目搭建(vue-cli 4.0)
1.创建vue项目 cmd命令执行 vue create app (app 自定义的项目名) 一般都会选择后者,自己配置一下自己需要的选项(空格为选中) 这是我个人需要的一些选项,路由Router.状 ...
- vue单页面项目架构方案
这里的架构方案是基于vue-cli2生成的项目应用程序产生的,是对项目应用程序或者项目模板的一些方便开发和维护的封装.针对单页面的解决方案. 主要有四个方面: 一,不同环境下的分别打包 主要是测试环境 ...
- 大型vue单页面项目优化总结
这是之前在公司oa项目优化时罗列的优化点,基本都已经完成,当时花了点心思整理的,保存在这里,方便以后其他项目用到查漏补缺. 1.打包文件中的app.js文件放入cdn,加快页面首次加载速度 2.提取公 ...
- vue单页面项目返回上一页无效,链接变化了,但是页面没有变化
在最近的项目中,返回上一页没有效果,经过好久的排查才发现问题,是路由守卫写法不规范导致. 在项目中用路由守卫做了登录拦截,没登录的跳转到登录页面.页面跳转和拦截都没问题,但是返回上一页就不行了,也没有 ...
- vue单页面项目中解决安卓4.4版本不兼容的问题
1.cnpm安装 cnpm i babel-polyfill --save cnpm i es6-promise --save 2.main.js引入 import ‘babel-polyfill‘ ...
- Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(上篇——纯前端多页面)
Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(上篇--纯前端多页面) @(HTML/JS) 一般来说,使用vue做成单页应用比较好,但特殊情况下,需要使用多页面也有另外 ...
- Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(下篇——多页面VueSSR+热更新Server)
Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(下篇--多页面VueSSR+热更新Server) @(HTML/JS) 这是Vue多页面框架系列文章的第二篇,上一篇(纯前 ...
随机推荐
- 【笔记篇】Ubuntu一日游
今天做数据的时候在Windows下出问题了(好像是爆栈了QAQ) 于是乎就打开了自己的Ubuntu虚拟机… 然而沉迷Windows的我已经忘记自己对这台虚拟机做过什么(比如装残了一个ycm自己都不知道 ...
- Lua程序设计之字符串精要
(摘自Lua程序设计) 基本: Lua语言的字符串是一串字节组成的序列. 在Lua语言中,字符使用8个比特位来存储. Lua语言中的字符串可以存储包括空字符在内的所有数值代码,这意味着我们可以在字符串 ...
- [转].NET4.0新特性集合贴
vs2010正式版4月12日发布了,前几天我也下了一个,但这几天都没有时间好好试用一下,今天针对C#语言的新特性使用了一下,感觉还不错,有几个新特性和大家分享一下,希望我没有太火星…… 一.新关键词— ...
- 常用DOM API总结
一. 获取节点 1. 获取元素节点 getElementsById getElementsByTagName getElementsByClassName 2. 获取属性节点 getAttribute ...
- Ubuntu下安装和配置Apache2,小编觉得挺不错的,现在就分享给大家
本篇文章主要介绍了详解Ubuntu下安装和配置Apache2,小编觉得挺不错的,现在就分享给大家,也给大家做个参考.有兴趣的朋友可以了解一下.(http://xz.8682222.com) 在Ubun ...
- 从微服务治理的角度看RSocket、. Envoy和. Istio
很多同学看到这个题目,一定会提这样的问题:RSocket是个协议,Envoy是一个 proxy,Istio是service mesh control plane + data plane. 这三种技术 ...
- 一个Js开发者学习Python的第一天
原文地址:小寒的博客 https://www.dodoblog.cn/blogs/5bf6b8fa0c09883d0f8aad13 作为一个有着足足两年半学习经验和一年半开发经验的js开发者,看着js ...
- 洛谷P1291 [SHOI2002]百事世界杯之旅
题目链接: kma 题目分析: 收集邮票的弱弱弱弱化版,因为是期望,考虑倒推 设\(f[i]\)表示现在已经买齐了\(i\)种,距离买完它的剩余期望次数 那么下一次抽有\(\frac{i}{n}\)的 ...
- Java-MyBatis-MyBatis3-XML映射文件:参数
ylbtech-Java-MyBatis-MyBatis3-XML映射文件:参数 1.返回顶部 1. 参数 你之前见到的所有语句中,使用的都是简单参数.实际上参数是 MyBatis 非常强大的元素.对 ...
- iOS之CATextLayer属性简介
1.CATextLayer简介 CATextLayer快速高效简单地来渲染纯文本.NSAttributedString /* The text layer provides simple text l ...