-
Notifications
You must be signed in to change notification settings - Fork 45
/
multiple.page.generate.js
63 lines (59 loc) · 1.96 KB
/
multiple.page.generate.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
const path = require('path');
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
let appConfig = require('./multiple.config.js').app;
let pageList = null;
let isBuild = ['production'].includes(process.env.NODE_ENV);
function readPages(dir) {
if (!pageList) {
const pagesPath = path.resolve('./' + dir);
pageList = [];
fs.readdirSync(pagesPath).forEach((pageFile) => {
let fullPath = pagesPath + '/' + pageFile;
let isDir = fs.statSync(fullPath).isDirectory();
// 如果pages下的文件是一个js文件,js文件会被重写成html文件
if (!isDir) {
if (pageFile.slice(-3) === '.ts') {
let baseName = pageFile.slice(0, pageFile.lastIndexOf('.'));
pageList.push({
entry: fullPath,
chunkName: baseName,
template: 'multiple.index.template.html',
});
}
}
});
}
return pageList;
}
exports.getEntryPages = function (dir) {
let pages = readPages(dir).reduce((r, page) => {
r[page.chunkName] = page.entry;
return r;
}, {});
console.log(' App • Getting enter files');
return pages;
};
exports.htmlPlugins = function (dir) {
let exChunks = isBuild ? ['manifest', 'vendor'] : [];
let list = readPages(dir).map((page) => {
let options = {
filename: page.chunkName + '.html',
template: page.template,
title: appConfig[page.chunkName] ? appConfig[page.chunkName].title : '',
chunks: [...exChunks, page.chunkName],
inject: true,
minify: {
removeComments: isBuild ? true : false,
collapseWhitespace: isBuild ? true : false,
removeAttributeQuotes: true,
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
appConfig: appConfig[page.chunkName] || { title: '', description: '' },
};
return new HtmlWebpackPlugin(options);
});
console.log(' App • Generating html file');
return list;
};