-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
39 lines (31 loc) · 1.16 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
'use strict';
const write = require('write');
const path = require('path');
module.exports = class CreateFilePlugin {
constructor(options) {
if (!options) {
throw new Error(`Please provide 'options' for the CreateFilePlugin config`);
}
const missingOptions = [];
if (options.filePath == null) missingOptions.push('filePath');
if (options.fileName == null) missingOptions.push('fileName');
if (options.content == null) missingOptions.push('content');
if (missingOptions.length)
throw new Error(
`Please provide the following option${
missingOptions.length > 1 ? 's' : ''
} in the CreateFilePlugin config: ${missingOptions.join(', ')}`
);
this.options = options;
}
_createFile({ filePath, fileName, content }, compilation) {
const fullPath = path.join(filePath, fileName);
const contentData = typeof content === 'function' ? content({ filePath, fileName, compilation }) : content;
write.sync(fullPath, contentData);
}
apply(compiler) {
compiler.hooks.emit.tapPromise('CreateFilePlugin', async compilation => {
this._createFile(this.options, compilation);
});
}
};