forked from AwesomeDevin/drop-console-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-plugin.js
85 lines (71 loc) · 2.08 KB
/
test-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
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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class HelloWorldPlugin {
constructor(options) {
this.options = options;
this.fileList = [];
this.pluginName = 'HelloWorldPlugin'
this.emitCompilation = null;
}
saveToFile(compiler,compilation){
console.log('saveToFile')
for(var item of this.fileList)
{
const filePath = _path2.default.join(compiler.outputPath,item.fileName)
item.filePath = filePath;
console.log('--------------',filePath);
var self = this;
(function(item){
_fs2.default.readFile(filePath, function (err, data) {
if (err) {
return console.error(err);
}
const assets = data.toString()
console.log(assets)
item.assets = assets
});
})(item)
}
}
apply(compiler) {
compiler.hooks.emit.tap(this.pluginName, (compilation) => {
compilation.assets['file.txt'] = {
source() {
return '123456';
},
size() {
return '123456'.length;
}
};
});
compiler.hooks.afterEmit.tapAsync(this.pluginName, (compilation,callback) => {
for (var filename in compilation.assets) {
console.log('>>>>>>>>>>>>>>',filename)
this.fileList.push({'fileName':filename})
}
// compilation.assets['file.txt'] = {
// source() {
// return '123456';
// callback()
// },
// size() {
// return '123456'.length;
// }
// };
callback(this.saveToFile(compiler,compilation))
});
compiler.hooks.done.tap(this.pluginName, () => {
console.log('Hello World end!');
console.log(this.options);
// console.log(this.fileList)
});
}
}
module.exports = HelloWorldPlugin;