hookehuyr

多页面不同环境下可配置

1 +var dev = [{
2 + title: '登录页',
3 + page: 'login'
4 +}];
5 +
6 +module.exports = dev;
1 +var prod = [{
2 + title: '登录页',
3 + page: 'login'
4 +}];
5 +
6 +module.exports = prod;
...@@ -5,7 +5,8 @@ ...@@ -5,7 +5,8 @@
5 "scripts": { 5 "scripts": {
6 "serve": "vue-cli-service serve", 6 "serve": "vue-cli-service serve",
7 "build": "vue-cli-service build", 7 "build": "vue-cli-service build",
8 - "lint": "vue-cli-service lint" 8 + "lint": "vue-cli-service lint",
9 + "rm": "rm -rf dist/"
9 }, 10 },
10 "dependencies": { 11 "dependencies": {
11 "axios": "^0.18.0", 12 "axios": "^0.18.0",
......
1 /* jshint esversion: 6 */ 1 /* jshint esversion: 6 */
2 -const path = require('path') 2 +const path = require('path');
3 -const webpack = require('webpack') 3 +const webpack = require('webpack');
4 -const CompressionWebpackPlugin = require('compression-webpack-plugin') 4 +const CompressionWebpackPlugin = require('compression-webpack-plugin');
5 5
6 // function resolve (dir) { 6 // function resolve (dir) {
7 // return path.join(__dirname, './', dir) 7 // return path.join(__dirname, './', dir)
...@@ -17,7 +17,7 @@ const externals = { ...@@ -17,7 +17,7 @@ const externals = {
17 '$': 'jquery', 17 '$': 'jquery',
18 'jquery': 'Jquery', 18 'jquery': 'Jquery',
19 'moment': 'moment' 19 'moment': 'moment'
20 -} 20 +};
21 21
22 const cdn = { 22 const cdn = {
23 // 开发环境 23 // 开发环境
...@@ -40,53 +40,63 @@ const cdn = { ...@@ -40,53 +40,63 @@ const cdn = {
40 'https://cdn.jsdelivr.net/npm/moment@2.23.0/moment.min.js' 40 'https://cdn.jsdelivr.net/npm/moment@2.23.0/moment.min.js'
41 ] 41 ]
42 } 42 }
43 -} 43 +};
44 44
45 // 是否使用gzip 45 // 是否使用gzip
46 -const productionGzip = true 46 +const productionGzip = true;
47 // 需要gzip压缩的文件后缀 47 // 需要gzip压缩的文件后缀
48 -const productionGzipExtensions = ['js', 'css'] 48 +const productionGzipExtensions = ['js', 'css'];
49 49
50 -module.exports = { 50 +// 多页模板页
51 - // 基本路径 51 +var pages = {
52 - publicPath: process.env.NODE_ENV === 'production' ? '/boh/' : '/', 52 + // 默认页
53 - // 输出文件目录
54 - outputDir: 'dist',
55 - // 放置生成的静态资源的目录
56 - assetsDir: '',
57 - // 指定生成的 index.html 的输出路径 (相对于 outputDir)。也可以是一个绝对路径。
58 - indexPath: 'index.html',
59 - filenameHashing: true,
60 - pages: {
61 index: { 53 index: {
62 - // page 的入口 54 + entry: 'src/index.js',
63 - entry: 'src/main.js',
64 - // 模板来源
65 template: 'public/index.html', 55 template: 'public/index.html',
66 - // 在 dist/index.html 的输出
67 filename: 'index.html', 56 filename: 'index.html',
68 - // 当使用 title 选项时,
69 - // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
70 title: 'Index Page', 57 title: 'Index Page',
71 - // 在这个页面中包含的块,默认情况下会包含
72 - // 提取出来的通用 chunk 和 vendor chunk。
73 chunks: ['chunk-vendors', 'chunk-common', 'index'] 58 chunks: ['chunk-vendors', 'chunk-common', 'index']
74 - }, 59 + }
75 - login: { 60 +};
61 +var prod_custom = require('./config/prod-custom');
62 +var dev_custom = require('./config/dev-custom');
63 +function getEntryPages (array) {
64 + for (let i = 0; i < array.length; i++) {
65 + pages[array[i].page] = {
76 // page 的入口 66 // page 的入口
77 - entry: 'src/multiPages/login/login.js', 67 + entry: `src/multiPages/${array[i].page}/${array[i].page}.js`,
78 // 模板来源 68 // 模板来源
79 - template: 'public/login.html', 69 + template: `public/${array[i].page}.html`,
80 // 在 dist/index.html 的输出 70 // 在 dist/index.html 的输出
81 - filename: 'login.html', 71 + filename: `${array[i].page}.html`,
82 // 当使用 title 选项时, 72 // 当使用 title 选项时,
83 // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title> 73 // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
84 - title: '登录页', 74 + title: `${array[i].title}`,
85 // 在这个页面中包含的块,默认情况下会包含 75 // 在这个页面中包含的块,默认情况下会包含
86 // 提取出来的通用 chunk 和 vendor chunk。 76 // 提取出来的通用 chunk 和 vendor chunk。
87 - chunks: ['chunk-vendors', 'chunk-common', 'login'] 77 + chunks: ['chunk-vendors', 'chunk-common', array[i].page]
78 + };
88 } 79 }
89 - }, 80 + return pages;
81 +}
82 +if (process.env.NODE_ENV === 'production') {
83 + pages = getEntryPages(prod_custom);
84 +}
85 +if (process.env.NODE_ENV === 'development') {
86 + pages = getEntryPages(dev_custom);
87 +}
88 +
89 +module.exports = {
90 + // 基本路径
91 + publicPath: process.env.NODE_ENV === 'production' ? '/boh/' : '/',
92 + // 输出文件目录
93 + outputDir: 'dist',
94 + // 放置生成的静态资源的目录
95 + assetsDir: '',
96 + // 指定生成的 index.html 的输出路径 (相对于 outputDir)。也可以是一个绝对路径。
97 + indexPath: 'index.html',
98 + filenameHashing: true,
99 + pages,
90 // eslint-loader 是否在保存的时候检查 100 // eslint-loader 是否在保存的时候检查
91 lintOnSave: 'error', 101 lintOnSave: 'error',
92 // 是否使用包含运行时编译器的 Vue 构建版本。设置为 true 后你就可以在 Vue 组件中使用 template 选项了。 102 // 是否使用包含运行时编译器的 Vue 构建版本。设置为 true 后你就可以在 Vue 组件中使用 template 选项了。
...@@ -105,25 +115,27 @@ module.exports = { ...@@ -105,25 +115,27 @@ module.exports = {
105 'axios': 'axios', 115 'axios': 'axios',
106 'moment': 'moment' 116 'moment': 'moment'
107 }) 117 })
108 - ) 118 + );
109 - config.resolve.extensions.push('.less') 119 + config.resolve.extensions.push('.less');
110 - config.resolve.extensions.push('.css') 120 + config.resolve.extensions.push('.css');
111 // 121 //
112 - config.resolve.alias.src = path.resolve(__dirname, './src/src') 122 + config.resolve.alias.src = path.resolve(__dirname, './src/src');
113 - config.resolve.alias.assets = path.resolve(__dirname, './src/assets') 123 + config.resolve.alias.assets = path.resolve(__dirname, './src/assets');
114 - config.resolve.alias.components = path.resolve(__dirname, './src/components') 124 + config.resolve.alias.components = path.resolve(__dirname, './src/components');
115 // 修改webpack config, 使其不打包externals下的资源 125 // 修改webpack config, 使其不打包externals下的资源
116 if (process.env.NODE_ENV === 'production') { 126 if (process.env.NODE_ENV === 'production') {
117 // 1. 生产环境npm包转CDN 127 // 1. 生产环境npm包转CDN
118 - config.externals = externals 128 + config.externals = externals;
119 // 2. 构建时开启gzip,降低服务器压缩对CPU资源的占用,服务器也要相应开启gzip 129 // 2. 构建时开启gzip,降低服务器压缩对CPU资源的占用,服务器也要相应开启gzip
120 - productionGzip && config.plugins.push( 130 + if (productionGzip) {
131 + config.plugins.push(
121 new CompressionWebpackPlugin({ 132 new CompressionWebpackPlugin({
122 test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'), 133 test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
123 threshold: 8192, 134 threshold: 8192,
124 minRatio: 0.8 135 minRatio: 0.8
125 }) 136 })
126 - ) 137 + );
138 + }
127 } 139 }
128 if (process.env.NODE_ENV === 'development') { 140 if (process.env.NODE_ENV === 'development') {
129 } 141 }
...@@ -138,25 +150,19 @@ module.exports = { ...@@ -138,25 +150,19 @@ module.exports = {
138 .plugin('html-index') 150 .plugin('html-index')
139 .tap(args => { 151 .tap(args => {
140 if (process.env.NODE_ENV === 'production') { 152 if (process.env.NODE_ENV === 'production') {
141 - args[0].cdn = cdn.build 153 + args[0].cdn = cdn.build;
142 } 154 }
143 - if (process.env.NODE_ENV === 'development') { 155 + return args;
144 - args[0].cdn = cdn.dev
145 - }
146 - return args
147 }) 156 })
148 .end() 157 .end()
149 .plugin('html-login') 158 .plugin('html-login')
150 .tap(args => { 159 .tap(args => {
151 if (process.env.NODE_ENV === 'production') { 160 if (process.env.NODE_ENV === 'production') {
152 - args[0].cdn = cdn.build 161 + args[0].cdn = cdn.build;
153 - }
154 - if (process.env.NODE_ENV === 'development') {
155 - args[0].cdn = cdn.dev
156 } 162 }
157 - return args 163 + return args;
158 }) 164 })
159 - .end() 165 + .end();
160 }, 166 },
161 // css相关配置 167 // css相关配置
162 css: { 168 css: {
...@@ -201,8 +207,8 @@ module.exports = { ...@@ -201,8 +207,8 @@ module.exports = {
201 '^/(.*)': '/$1' 207 '^/(.*)': '/$1'
202 }, 208 },
203 onProxyReq: function (proxyReq, req, res, options) { 209 onProxyReq: function (proxyReq, req, res, options) {
204 - proxyReq.setHeader('X-Proxy-Host', req.header('host')) 210 + proxyReq.setHeader('X-Proxy-Host', req.header('host'));
205 - proxyReq.setHeader('X-Proxy-Request-URI', req.url) 211 + proxyReq.setHeader('X-Proxy-Request-URI', req.url);
206 } 212 }
207 } 213 }
208 }, 214 },
...@@ -216,4 +222,4 @@ module.exports = { ...@@ -216,4 +222,4 @@ module.exports = {
216 pluginOptions: { 222 pluginOptions: {
217 // ... 223 // ...
218 } 224 }
219 -} 225 +};
......