-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.utils.js
79 lines (70 loc) · 2.03 KB
/
webpack.utils.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
75
76
77
78
79
'use strict';
const path = require('path');
const glob = require('glob');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const isDev = process.env.NODE_ENV === 'development';
const CONFIG = {
pageTitle: 'YuYan-Doc',
templatePath: './src/index.html',
yuyanDocConfig: {
GITHUB_USERNAME: 'ShenBao',
REPOSITORY_NAME: 'yuyan-doc-template',
pageTitle: 'YuYan-Doc',
templatePath: './src/index.html',
pageList: [],
pageMap: {},
contentType: 0,
isDev,
},
};
const findIsIgnoreFile = (match) => {
const filePath = match[0];
return filePath.endsWith('README.md') || filePath.includes('node_modules');
};
const buildPageList = () => {
const mdFiles = glob.sync(path.join(__dirname, './**/*.md'));
const pageList = [];
const pageMap = {};
Object.keys(mdFiles).map((index) => {
const entryFile = mdFiles[index];
const match = entryFile.match(/\/yuyan-doc-template\/(.*)\/*\.md/);
const isIgnoreFile = findIsIgnoreFile(match);
if (!(match && isIgnoreFile)) {
const arr = match[1].split('/');
!pageMap[arr[0]] ? (pageMap[arr[0]] = []) : '';
const item = {
title: arr[1],
url: isDev
? match[0].replace(`/${CONFIG.yuyanDocConfig.REPOSITORY_NAME}`, '')
: match[0],
};
pageList.push(item);
pageMap[arr[0]].push(item);
}
});
CONFIG.yuyanDocConfig.pageList = pageList;
CONFIG.yuyanDocConfig.pageMap = pageMap;
return CONFIG;
};
const IndexHtmlWebpackPlugin = (CONFIG, isTmp) => {
return new HtmlWebpackPlugin({
title: CONFIG.pageTitle,
template: CONFIG.templatePath,
staticUrl: isDev ? './' : '/yuyan-doc-template/dist/',
yuyanDocConfig: JSON.stringify(CONFIG.yuyanDocConfig),
filename: isTmp ? 'template.html' : 'index.html',
chunks: ['main'],
minify: {
html5: true,
collapseWhitespace: true,
preserveLineBreaks: false,
minifyCSS: true,
minifyJS: true,
removeComments: false,
},
});
};
module.exports = {
IndexHtmlWebpackPlugin,
buildPageList,
};