-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.helpers.ts
131 lines (104 loc) · 4.28 KB
/
webpack.helpers.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* eslint-disable no-console */
import * as fs from 'fs';
import * as Path from 'path';
import HtmlWebpackPlugin, { MinifyOptions } from 'html-webpack-plugin';
import * as webpack from 'webpack';
/* global __dirname, process */
export function pathResolve(p: string) {
return Path.resolve(__dirname, p);
}
export class HtmlBuilder {
readonly htmlMinifyOptions: MinifyOptions | false;
constructor(readonly chunksPriority: Record<string, number>, minify = false) {
this.htmlMinifyOptions = minify
? {
removeAttributeQuotes: true,
collapseWhitespace: true,
conservativeCollapse: true,
preserveLineBreaks: true,
html5: true,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeEmptyAttributes: true,
} : false;
}
createHtmlPlugin(output: string, templatePath: string, id: string, options = {}) {
const chunks: [string, number][] = Object.entries(this.chunksPriority);
chunks.push([ id, 0 ]);
chunks.sort((c1, c2) => c1[1] - c2[1]);
const htmlOptions: HtmlWebpackPlugin.Options = {
filename: output,
cache: false,
template: templatePath,
minify: this.htmlMinifyOptions,
inject: false,
chunks: chunks.map(c => c[0]),
chunksSortMode: 'manual',
templateParameters: options,
};
// console.log('Creating HtmlWebpackPlugin with options', htmlOptions);
return new HtmlWebpackPlugin(htmlOptions);
}
generateHtmlPlugins(templateDir: string = './app/html/', extensions: string[] = ['html', 'ejs']) {
const templateFiles = fs.readdirSync(pathResolve(templateDir));
return templateFiles
.map(item => {
const parts = item.split('.');
const ext = parts.pop();
if (!extensions.includes(ext)) {
return null;
}
const name = parts.join().toLowerCase();
const outputName = name === 'index' ? 'index.html' : `${name}/index.html`;
const templatePath = pathResolve(`${templateDir}/${item}`);
return this.createHtmlPlugin(outputName, templatePath, item);
})
.filter(p => p);
}
}
export type PluginOption = {
name: string,
plugin: webpack.WebpackPluginInstance,
allowedEnv?: string,
enabled?: boolean,
};
export function wrapPlugins(plugins: (PluginOption | webpack.WebpackPluginInstance)[]): webpack.WebpackPluginInstance[] {
return plugins.map(p => {
const pp = p as webpack.WebpackPluginInstance;
const po = p as PluginOption;
if (!po.name || !po.plugin) {
return pp;
}
if (process.argv.indexOf(`--disable-${po.name}-plugin`) >= 0) {
console.log(`[Webpack Config] Skipping plugin '${po.name}' because it was explicitly disabled by cl args.`);
return null;
}
if (po.allowedEnv && process.env.NODE_ENV !== po.allowedEnv) {
console.log(`[Webpack Config] Skipping plugin '${po.name}' because it was disabled by NODE_ENV. Allowed = ${po.allowedEnv}, current = ${process.env.NODE_ENV}`);
return null;
}
if (po.enabled !== undefined && typeof po.enabled === 'boolean') {
if (!po.enabled) {
console.log(`[Webpack Config] Skipping plugin '${po.name}' because it was explicitly disabled by option.`);
return null;
}
return po.plugin;
}
return po.plugin;
}).filter(pp => pp);
}
export function flattenEnvObject(obj: any, prefix = '', maxLevel: number | null = null): Record<string, string> {
const res: Record<string, string> = { };
Object.keys(obj).forEach((key: string) => {
const v = obj[key];
const nextPrefix = prefix ? `${prefix}.${key}` : key;
if ((maxLevel == null || maxLevel > 0) && typeof v === 'object') {
const next = flattenEnvObject(v, nextPrefix, maxLevel == null ? null : maxLevel - 1);
Object.assign(res, next);
} else {
res[nextPrefix] = JSON.stringify(v);
}
});
return res;
}