Skip to content

Commit 50d3410

Browse files
committed
update 多页面打包
1 parent 07312eb commit 50d3410

17 files changed

+693
-68
lines changed

build/utils.js

+55-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use strict'
22
const path = require('path')
33
const config = require('../config')
4+
const glob = require('glob')
45
const ExtractTextPlugin = require('extract-text-webpack-plugin')
56
const packageConfig = require('../package.json')
7+
const entriePaths = getEntries()
8+
const HtmlWebpackPlugin = require('html-webpack-plugin')
69

710
exports.assetsPath = function (_path) {
811
const assetsSubDirectory = process.env.NODE_ENV === 'production'
@@ -53,7 +56,6 @@ exports.cssLoaders = function (options) {
5356
return ['vue-style-loader'].concat(loaders)
5457
}
5558
}
56-
5759
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
5860
return {
5961
css: generateLoaders(),
@@ -99,3 +101,55 @@ exports.createNotifierCallback = () => {
99101
})
100102
}
101103
}
104+
105+
exports.entries = () => {
106+
return entriePaths.extries
107+
}
108+
109+
exports.htmlPlugins = function() {
110+
let templatePaths = entriePaths.templates
111+
let arr = []
112+
templatePaths.map(template => {
113+
let chunkName = template.split(path.sep).slice(-2)[0];
114+
arr.push(new HtmlWebpackPlugin({
115+
filename: chunkName + '.html',
116+
template: template,
117+
chunks: ['vendor', 'manifest', chunkName],
118+
inject: true,
119+
minify: {
120+
removeComments: true,
121+
collapseWhitespace: true,
122+
removeAttributeQuotes: true
123+
},
124+
chunksSortMode: 'dependency'
125+
}))
126+
})
127+
128+
return arr
129+
}
130+
131+
132+
function getPath(...args) {
133+
return path.join(path.resolve(__dirname, '../src'), ...args);
134+
}
135+
136+
function getEntries() {
137+
const files = glob.sync(`${config.base.pagesRoot}/*/*.js`)
138+
let extries = {}
139+
let templates = []
140+
141+
files.forEach(function(filePath) {
142+
let fileSplits = filePath.split('/');
143+
const length = fileSplits.length
144+
const folderName = fileSplits[length - 2]
145+
const fileName = fileSplits[length - 1]
146+
147+
extries[folderName] = getPath('pages', folderName, fileName)
148+
templates.push(getPath('pages', folderName, 'index.html'))
149+
})
150+
151+
return {
152+
extries: extries,
153+
templates: templates
154+
}
155+
}

build/webpack.base.conf.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path')
33
const utils = require('./utils')
44
const config = require('../config')
55
const vueLoaderConfig = require('./vue-loader.conf')
6+
const webpack = require('webpack')
67

78
function resolve (dir) {
89
return path.join(__dirname, '..', dir)
@@ -21,9 +22,7 @@ const createLintingRule = () => ({
2122

2223
module.exports = {
2324
context: path.resolve(__dirname, '../'),
24-
entry: {
25-
app: './src/main.js'
26-
},
25+
entry: utils.entries(),
2726
output: {
2827
path: config.build.assetsRoot,
2928
filename: '[name].js',

build/webpack.dev.conf.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const merge = require('webpack-merge')
66
const path = require('path')
77
const baseWebpackConfig = require('./webpack.base.conf')
88
const CopyWebpackPlugin = require('copy-webpack-plugin')
9-
const HtmlWebpackPlugin = require('html-webpack-plugin')
109
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
1110
const portfinder = require('portfinder')
1211

@@ -51,21 +50,15 @@ const devWebpackConfig = merge(baseWebpackConfig, {
5150
new webpack.HotModuleReplacementPlugin(),
5251
new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
5352
new webpack.NoEmitOnErrorsPlugin(),
54-
// https://github.com/ampedandwired/html-webpack-plugin
55-
new HtmlWebpackPlugin({
56-
filename: 'index.html',
57-
template: 'index.html',
58-
inject: true
59-
}),
60-
// copy custom static assets
53+
6154
new CopyWebpackPlugin([
6255
{
6356
from: path.resolve(__dirname, '../static'),
6457
to: config.dev.assetsSubDirectory,
6558
ignore: ['.*']
6659
}
6760
])
68-
]
61+
].concat(utils.htmlPlugins())
6962
})
7063

7164
module.exports = new Promise((resolve, reject) => {

build/webpack.dll.config.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Created by [email protected] on 2018/3/13.
3+
*/
4+
5+
const webpack = require('webpack');
6+
const ExtractTextPlugin = require('extract-text-webpack-plugin');
7+
const config = require('../config');
8+
9+
module.exports = {
10+
output: {
11+
path: config.dll.path,
12+
filename: '[name].js',
13+
library: '[name]_libs', // 当前Dll的所有内容都会存放在这个参数指定变量名的一个全局变量下,注意与DllPlugin的name参数保持一致
14+
},
15+
entry: {
16+
dlls: config.dll.libs
17+
},
18+
plugins: [
19+
new webpack.optimize.ModuleConcatenationPlugin(),
20+
new webpack.DllPlugin({
21+
path: 'manifest.json', // 本Dll文件中各模块的索引,供DllReferencePlugin读取使用
22+
name: '[name]_libs', // 当前Dll的所有内容都会存放在这个参数指定变量名的一个全局变量下,注意与参数output.library保持一致
23+
context: __dirname, // 指定一个路径作为上下文环境,需要与DllReferencePlugin的context参数保持一致,建议统一设置为项目根目录
24+
}),
25+
new webpack.optimize.UglifyJsPlugin({
26+
compress: {
27+
warnings: true
28+
}
29+
}),
30+
new ExtractTextPlugin('[name].css'), // 打包css/less的时候会用到ExtractTextPlugin
31+
]
32+
};

build/webpack.prod.conf.js

+19-40
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const config = require('../config')
66
const merge = require('webpack-merge')
77
const baseWebpackConfig = require('./webpack.base.conf')
88
const CopyWebpackPlugin = require('copy-webpack-plugin')
9-
const HtmlWebpackPlugin = require('html-webpack-plugin')
109
const ExtractTextPlugin = require('extract-text-webpack-plugin')
1110
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
1211
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
@@ -24,8 +23,8 @@ const webpackConfig = merge(baseWebpackConfig, {
2423
devtool: config.build.productionSourceMap ? config.build.devtool : false,
2524
output: {
2625
path: config.build.assetsRoot,
27-
filename: utils.assetsPath('js/[name].[chunkhash].js'),
28-
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
26+
filename: utils.assetsPath('js/[name].[chunkhash:8].js'),
27+
chunkFilename: utils.assetsPath('js/[id].[chunkhash:8].js')
2928
},
3029
plugins: [
3130
// http://vuejs.github.io/vue-loader/en/workflow/production.html
@@ -43,10 +42,10 @@ const webpackConfig = merge(baseWebpackConfig, {
4342
}),
4443
// extract css into its own file
4544
new ExtractTextPlugin({
46-
filename: utils.assetsPath('css/[name].[contenthash].css'),
45+
filename: utils.assetsPath('css/[name].[contenthash:8].css'),
4746
// Setting the following option to `false` will not extract CSS from codesplit chunks.
4847
// Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
49-
// It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
48+
// It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
5049
// increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
5150
allChunks: true,
5251
}),
@@ -57,32 +56,24 @@ const webpackConfig = merge(baseWebpackConfig, {
5756
? { safe: true, map: { inline: false } }
5857
: { safe: true }
5958
}),
60-
// generate dist index.html with correct asset hash for caching.
61-
// you can customize output by editing /index.html
62-
// see https://github.com/ampedandwired/html-webpack-plugin
63-
new HtmlWebpackPlugin({
64-
filename: config.build.index,
65-
template: 'index.html',
66-
inject: true,
67-
minify: {
68-
removeComments: true,
69-
collapseWhitespace: true,
70-
removeAttributeQuotes: true
71-
// more options:
72-
// https://github.com/kangax/html-minifier#options-quick-reference
73-
},
74-
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
75-
chunksSortMode: 'dependency'
76-
}),
59+
7760
// keep module.id stable when vendor modules does not change
7861
new webpack.HashedModuleIdsPlugin(),
7962
// enable scope hoisting
8063
new webpack.optimize.ModuleConcatenationPlugin(),
81-
// split vendor js into its own file
64+
65+
// copy custom static assets
66+
new CopyWebpackPlugin([
67+
{
68+
from: path.resolve(__dirname, '../static'),
69+
to: config.build.assetsSubDirectory,
70+
ignore: ['.*']
71+
}
72+
]),
73+
8274
new webpack.optimize.CommonsChunkPlugin({
8375
name: 'vendor',
8476
minChunks (module) {
85-
// any required modules inside node_modules are extracted to vendor
8677
return (
8778
module.resource &&
8879
/\.js$/.test(module.resource) &&
@@ -92,31 +83,19 @@ const webpackConfig = merge(baseWebpackConfig, {
9283
)
9384
}
9485
}),
95-
// extract webpack runtime and module manifest to its own file in order to
96-
// prevent vendor hash from being updated whenever app bundle is updated
86+
9787
new webpack.optimize.CommonsChunkPlugin({
9888
name: 'manifest',
9989
minChunks: Infinity
10090
}),
101-
// This instance extracts shared chunks from code splitted chunks and bundles them
102-
// in a separate chunk, similar to the vendor chunk
103-
// see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
91+
10492
new webpack.optimize.CommonsChunkPlugin({
10593
name: 'app',
10694
async: 'vendor-async',
10795
children: true,
10896
minChunks: 3
109-
}),
110-
111-
// copy custom static assets
112-
new CopyWebpackPlugin([
113-
{
114-
from: path.resolve(__dirname, '../static'),
115-
to: config.build.assetsSubDirectory,
116-
ignore: ['.*']
117-
}
118-
])
119-
]
97+
})
98+
].concat(utils.htmlPlugins())
12099
})
121100

122101
if (config.build.productionGzip) {

config/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ module.exports = {
4949

5050
// Paths
5151
assetsRoot: path.resolve(__dirname, '../dist'),
52-
assetsSubDirectory: 'static',
52+
assetsSubDirectory: '',
53+
// assetsSubDirectory: 'multi-vue-h5/dist/',
5354
assetsPublicPath: '/',
5455

5556
/**
@@ -72,5 +73,18 @@ module.exports = {
7273
// `npm run build --report`
7374
// Set to `true` or `false` to always turn it on or off
7475
bundleAnalyzerReport: process.env.npm_config_report
76+
},
77+
78+
base: {
79+
"isMultiPage": true,
80+
"pagesRoot": path.resolve(__dirname, '../src/pages')
81+
},
82+
dll: {
83+
isOk: false,
84+
path: path.resolve(__dirname, '../src/units/libs'),
85+
libs: [
86+
'vue',
87+
'vue-router'
88+
]
7589
}
7690
}

manifest.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"dlls_libs","content":{"../node_modules/vue/dist/vue.runtime.esm.js":{"id":3,"meta":{"harmonyModule":true},"exports":["default"]},"../node_modules/vue-router/dist/vue-router.esm.js":{"id":6,"meta":{"harmonyModule":true},"exports":["default"]},"../node_modules/process/browser.js":{"id":0,"meta":{}},"../node_modules/webpack/buildin/global.js":{"id":1,"meta":{}},"../node_modules/timers-browserify/main.js":{"id":4,"meta":{}},"../node_modules/setimmediate/setImmediate.js":{"id":5,"meta":{}}}}

package.json

+13-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
99
"start": "npm run dev",
1010
"lint": "eslint --ext .js,.vue src",
11-
"build": "node build/build.js"
11+
"build": "node build/build.js",
12+
"build:dll": "webpack --config build/webpack.dll.config.js"
1213
},
1314
"dependencies": {
1415
"vue": "^2.5.2",
1516
"vue-router": "^3.0.1"
1617
},
1718
"devDependencies": {
19+
"add-asset-html-webpack-plugin": "^2.1.3",
1820
"autoprefixer": "^7.1.2",
1921
"babel-core": "^6.22.1",
2022
"babel-eslint": "^8.2.1",
@@ -28,6 +30,7 @@
2830
"chalk": "^2.0.1",
2931
"copy-webpack-plugin": "^4.0.1",
3032
"css-loader": "^0.28.0",
33+
"cssnano": "^3.10.0",
3134
"eslint": "^4.15.0",
3235
"eslint-config-standard": "^10.2.1",
3336
"eslint-friendly-formatter": "^3.0.0",
@@ -40,15 +43,24 @@
4043
"extract-text-webpack-plugin": "^3.0.0",
4144
"file-loader": "^1.1.4",
4245
"friendly-errors-webpack-plugin": "^1.6.1",
46+
"glob": "^7.1.2",
4347
"html-webpack-plugin": "^2.30.1",
4448
"node-notifier": "^5.1.2",
4549
"optimize-css-assets-webpack-plugin": "^3.2.0",
4650
"ora": "^1.2.0",
4751
"portfinder": "^1.0.13",
52+
"postcss-aspect-ratio-mini": "^0.0.2",
53+
"postcss-attribute-case-insensitive": "^2.0.0",
54+
"postcss-calc": "^6.0.1",
55+
"postcss-cssnext": "^3.1.0",
4856
"postcss-import": "^11.0.0",
4957
"postcss-loader": "^2.0.8",
58+
"postcss-px-to-viewport": "^0.0.3",
5059
"postcss-url": "^7.2.1",
60+
"postcss-viewport-units": "^0.1.3",
61+
"postcss-write-svg": "^3.0.1",
5162
"rimraf": "^2.6.0",
63+
"sass-loader": "^6.0.7",
5264
"semver": "^5.3.0",
5365
"shelljs": "^0.7.6",
5466
"uglifyjs-webpack-plugin": "^1.1.1",

src/App.vue src/pages/index/App.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div id="app">
3-
<img src="./assets/logo.png">
3+
<img src="../../assets/logo.png">
44
<router-view/>
55
</div>
66
</template>
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/pages/user/App.vue

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<template>
2+
<div id="app">
3+
<img src="../../assets/logo.png">
4+
<router-view/>
5+
</div>
6+
</template>
7+
8+
<script>
9+
export default {
10+
name: 'App'
11+
}
12+
</script>
13+
14+
<style>
15+
#app {
16+
font-family: 'Avenir', Helvetica, Arial, sans-serif;
17+
-webkit-font-smoothing: antialiased;
18+
-moz-osx-font-smoothing: grayscale;
19+
text-align: center;
20+
color: #2c3e50;
21+
margin-top: 60px;
22+
}
23+
</style>

src/pages/user/index.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
6+
<title>multi-vue-h5</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
<!-- built files will be auto injected -->
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)