forked from monokh/make-dir-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (35 loc) · 1.03 KB
/
index.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
var fs = require('fs');
var path = require('path');
class MakeDirWebpackPlugin {
constructor(options) {
if (!options || !options.dirs)
throw new Error('options and options.dirs must be specified');
this.dirs = options.dirs;
}
makeDir(dir) {
const sep = path.sep;
const initDir = path.isAbsolute(dir) ? sep : '';
dir.split(sep).reduce((parentDir, childDir) => {
const currentDir = path.resolve(parentDir, childDir);
if (!fs.existsSync(currentDir)) {
fs.mkdirSync(currentDir);
}
return currentDir;
}, initDir);
}
apply(compiler) {
const doneHook = () => {
this.dirs.forEach((dirSpec) => {
this.makeDir(dirSpec.path);
});
};
// https://github.com/gdborton/webpack-parallel-uglify-plugin/issues/58#issuecomment-499537639
// avoid compiler hook warning
if (compiler.hooks) {
compiler.hooks.done.tap('MakeDirWebpackPlugin', doneHook);
} else {
compiler.plugin('done', doneHook);
}
}
}
module.exports = MakeDirWebpackPlugin;