有赞商店,js_脚本之家
年初的时候公司的老后台系统实在难以维护和继续在其上开发了,因为这个系统被很多人写过页面,有前端有后端,编写前端代码时都非常随意,加之没有模块化,复用性和可维护性都极低,便下定决定,重新搞一套。
var path = require('path')
var glob = require('glob')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var PAGE_PATH = path.resolve(__dirname, '../src/pages')
var merge = require('webpack-merge')
//多入口配置
exports.entries = function() {
var entryFiles = glob.sync(PAGE_PATH + '/*/*.js')
var map = {}
entryFiles.forEach((filePath) => {
var filename = filePath.substring(filePath.lastIndexOf('/') + 1, filePath.lastIndexOf('.'))
map[filename] = filePath
})
return map
}
//多页面输出配置
exports.htmlPlugin = function() {
let entryHtml = glob.sync(PAGE_PATH + '/*/*.html')
let arr = []
entryHtml.forEach((filePath) => {
let filename = filePath.substring(filePath.lastIndexOf('/') + 1, filePath.lastIndexOf('.'))
let conf = {
template: filePath,
filename: filename + '.html',
chunks: [filename],
inject: true
}
if (process.env.NODE_ENV === 'production') {
conf = merge(conf, {
chunks: ['manifest', 'vendor', filename],
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
},
chunksSortMode: 'dependency'
})
}
arr.push(new HtmlWebpackPlugin(conf))
})
return arr
}
这里还有个地方需要注意,就是抽取公用的js和css代码出来,这里做了一下改造,就是echarts指定提取出来,而不是按引用次数那种自动提取,
这里还踩了个坑,详细见注释。
- 项目目录结构的调整:
在开发路径src下增加modules和pages文件夹,分别存放模块和页面
有关页面的所有文件都放到同一文件夹下就近管理:index.html(页面模板)、main.js(页面入口文件)、App.vue(页面使用的组件,公用组件放到components文件夹下)、router(页面的路由配置)、assets(页面的静态资源)都移到index文件夹下,并把main.js改为index.js,保证页面的入口js文件和模板文件的名称一致 - 在build/utils.js中添加两个方法:webpack多入口文件和多页面输出
module.exports = { entry: utils.getEntries('./src/modules/*/*.js'),
4)修改build/webpack.dev.conf.js和build/webpack.prod.conf.js的多页面配置:把原有的页面模板配置注释或删除,并把多页面配置添加到plugins
webpack.dev.conf.js:
/build/weback.prod.conf
补充说明:在上面多页面输出配置中有这样一行代码:
/build/utils
电子游戏平台,4.安装所需要是模块
yarn
如何管理代码仓库 开发环境,测试环境搭建 如何接入公司的打包上线流程
如何目录划分 如何划分模块 登录和权限如何做
chunks: ['manifest', 'vendor', filename],
/build/webpack.dev.conf
经过一段时间的调研选择了vue全家桶+elementUI来开发后台系统,让交互体验更好,让开发体验更好,让生产效率提高。
module.exports = {
entry: utils.entries(),
var modules = utils.getEntries('./src/modules/*/*.html') Object.keys.forEach { var config = { filename: moduleName + '/index.html', template: modules[moduleName], inject: true, excludeChunks: Object.keys.filter { return name != moduleName }) } module.exports.plugins.push(new HtmlWebpackPlugin
3.在安装过程中会有一些提示:
/build/webpack.base.conf
至此,多页面应用已经搭建完毕,只需要在pages文件夹创建相应的页面文件即可。
总结
2、创建项目模板:
vue init webpack <filename>
从零搭建其实考虑的事情还挺多的,比如:
文章摘自基于vue-cli搭建一个多页面应用(一)–基础结构搭建
前言
plugins: [
......
// new HtmlWebpackPlugin({
// filename: config.build.index,
// template: 'index.html',
// inject: true,
// minify: {
// removeComments: true,
// collapseWhitespace: true,
// removeAttributeQuotes: true
// },
// chunksSortMode: 'dependency'
// }),
......
].concat(utils.htmlPlugin())
这样就完成了多页面的入口配置,其核心就是两点:1. 入口配置成数组。2.
plugins里面添加多个HtmlWebpackPlugin分别对应每一个页面,完成js打包后路径的自动注入功能。
这是html-webpack-plugin插件对页面入口文件(即js文件)的限定,如果不设置则会把整个项目下的所有入口文件全部引入
为什么要引入’manifest’和’vendor’,在build/webpack.prod.conf.js中有如下代码:
在webpack.prod.conf的plugins里面加入:
- 修改build/webpack.base.conf.js的入口配置
这篇文章来记录下和脚手架相关的改造,首先其实就是上了vue-cli来做,可是呢?由于预计项目会有很多页面,这些页面其实是分模块的,不同模块的页面之前其实关系不大。所以我觉得一个用户其实大部分时候只会用到其中一个模块的页面,如果把所有页面做成一个单页应用很多资源加载就不是很必要了,所以第一个改造就是:做成多入口打包,也就是做成多个单页应用,每个模块一个入口。
1、全局安装vue-cli:Vue.js官方提供的用于快速创建项目模板的脚手架工具
npm install vue-cli -g
exports.getEntries = function { var entries = {} glob.sync.forEach { var basename = path.basename(entry, path.extname entries[basename] = entry }) return entries}
5.现在创建的项目模板是单页面应用,与多页面应用还有些差别,需要做一些调整:
entry: { vendor: ['vue', 'vue-router', 'vuex', 'element-ui'], echarts: ['vue-echarts'] },// 这个地方天坑啊~~~死人了。。。:( // vendor是echarts的父模块,顺序不能反:https://github.com/webpack/webpack/issues/1943 // 包括声明CommonsChunkPlugin的顺序也是有关系的,不是随意的,后声明的是顶级模块,先声明的是依赖顶级模块的模块 // HtmlWebpackPlugin注入模块链接的时候的顺序也是由此保证的 new webpack.optimize.CommonsChunkPlugin({ names: ['echarts', 'vendor'], minChunks: function { // 抽取公用vendor.css // console.log return ( module.resource && /.css$/.test && module.resource.indexOf( path.join(__dirname, '../node_modules') ) === 0 ) } }),
- Vue build这个选项选择Runtime + Compiler
- 安装vue-router,ESLint、Karma+Mocha、Nightwatch根据需求选择安装
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
插件会按照模块的依赖关系依次加载,即:manifest,vendor,本页面入口,其他页面入口…
vendor模块是指提取涉及node_modules中的公共模块
manifest模块是对vendor模块做的缓存
关于CommonsChunkPlugin插件的详细说明请阅读官方文档
plugins: [
......
// new HtmlWebpackPlugin({
// filename: 'index.html',
// template: 'index.html',
// inject: true
// }),
......
].concat(utils.htmlPlugin())
chunksSortMode: 'dependency'
webpack.prod.conf.js:
// split vendor js into its own file
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks: function (module, count) {
// any required modules inside node_modules are extracted to vendor
return (
module.resource &&
/.js$/.test(module.resource) &&
module.resource.indexOf(
path.join(__dirname, '../node_modules')
) === 0
)
}
}),
// extract webpack runtime and module manifest to its own file in order to
// prevent vendor hash from being updated whenever app bundle is updated
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
chunks: ['vendor']
}),
关于html-webpack-plugin插件的配置还有一行代码:
标签:vue, 之家, 电子游戏平台, 脚手架, 脚本, 详解