-
Notifications
You must be signed in to change notification settings - Fork 160
/
webpack.plugin.js
58 lines (53 loc) · 1.58 KB
/
webpack.plugin.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
const fs = require('fs');
const fse = require('fs-extra');
const path = require('path');
const folderMap = {
es: 'esm',
lib: 'cjs',
dist: 'lib',
build: 'preview',
};
module.exports = ({ onGetWebpackConfig, onHook }) => {
onGetWebpackConfig(config => {
// 启用静态文件支持
config.module.rules
.delete('woff2')
.delete('ttf')
.delete('eot')
.delete('svg');
config.module
.rule('url-loader')
.test(/\.(png|svg|jpg|gif|eot|woff|ttf)$/)
.use('url-loader')
.loader('url-loader')
.options({
limit: 20000,
});
// UMD 输出,将 output 改为 index
if (config.output.get('libraryTarget') === 'umd') {
const entries = config.entryPoints.entries();
for (const it in entries) {
config.entryPoints.set('index', entries[it]);
config.entryPoints.delete(it);
}
}
});
onHook('before.build.run', () => {
const folders = [...Object.keys(folderMap), ...Object.values(folderMap)];
for (const it of folders) {
fse.rmdirSync(path.join(__dirname, it), { recursive: true });
console.log('Remove directory ' + it);
}
});
onHook('after.build.compile', () => {
const toRename = Object.keys(folderMap);
for (const it of toRename) {
if (fs.existsSync(path.join(__dirname, it))) {
fs.renameSync(path.join(__dirname, it), path.join(__dirname, folderMap[it]));
console.log('Rename ' + it + ' to ' + folderMap[it]);
}
}
const dirs = fs.readdirSync(__dirname);
console.log('Current files: ', dirs.join(' '));
});
};