-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmulti-page.js
76 lines (70 loc) · 2.52 KB
/
multi-page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//遍历pages文件夹生成入口
const path = require('path')
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const config = require('../config')
const appConfig = require('../config/app-config')
let pageList = null;
function readPages() {
if (!pageList) {
const pagesPath = path.resolve('./src/pages');
pageList = []
fs.readdirSync(pagesPath).forEach(pageFile => {
const fullPath = pagesPath + '/' + pageFile
const isDir = fs.statSync(fullPath).isDirectory()
if (!isDir) {
if (pageFile.slice(-3) == '.js') {
const baseName = pageFile.slice(0, pageFile.lastIndexOf('.'));
pageList.push({
entry: fullPath,
chunkName: baseName,
template: 'src/index.html',
})
}
} else { //文件夹
if (fs.existsSync(fullPath + '/entry.js')) {
try {
pageList.push({
entry: fullPath + '/entry.js',
chunkName: path.basename(pageFile),
template: fullPath + '/template.html',
})
} catch (e) {
console.error(fullPath + '/index.js not found.\n', e)
}
}
}
})
}
return pageList;
}
exports.getEntryPages = function () {
return readPages().reduce((r, page) => {
r[page.chunkName] = page.entry;
return r;
}, {});
}
exports.htmlPlugins = function (webackConfig) {
const exChunks = config.isBuild ? ['manifest', 'vendor'] : [];
const list = readPages().map(page => {
// see https://github.com/ampedandwired/html-webpack-plugin
const options = {
filename: page.chunkName + '.html',
template: page.template,
title: appConfig.title,
chunks: [...exChunks, page.chunkName],
inject: true,
favicon: 'static/favicon.ico',
// minify: {
// removeComments: true,
// collapseWhitespace: true,
// removeAttributeQuotes: false
// // more options:
// // https://github.com/kangax/html-minifier#options-quick-reference
// },
appConfig: appConfig,
}
return new HtmlWebpackPlugin(options);
});
return list
}