forked from hxlniada/webpack-concat-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (128 loc) · 5.47 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @file webpack-concat-plugin
* @author huangxueliang
*/
const fs = require('fs');
const UglifyJS = require('uglify-js');
const md5 = require('md5');
const path = require('path');
class ConcatPlugin {
constructor(options) {
this.settings = options;
// used to determine if we should emit files during compiler emit event
this.startTime = Date.now();
this.prevTimestamps = {};
this.filesToConcatAbsolute = options.filesToConcat
.map(f => path.resolve(f));
}
getFileName(files, filePath = this.settings.fileName) {
const fileRegExp = /\[name\]/;
const hashRegExp = /\[hash\]/;
if (this.settings.useHash || hashRegExp.test(filePath)) {
const fileMd5 = this.md5File(files);
if (!hashRegExp.test(filePath)) {
filePath = filePath.replace(/\.js$/, '.[hash].js');
}
filePath = filePath.replace(hashRegExp, fileMd5.slice(0, 20));
}
return filePath.replace(fileRegExp, this.settings.name);
}
md5File(files) {
if (this.fileMd5) {
return this.fileMd5;
}
const content = Object.keys(files)
.reduce((fileContent, fileName) => (fileContent + files[fileName]), '');
this.fileMd5 = md5(content);
return this.fileMd5;
}
apply(compiler) {
const self = this;
let content = '';
const concatPromise = () => self.settings.filesToConcat.map(fileName =>
new Promise((resolve, reject) => {
fs.readFile(fileName, (err, data) => {
if (err) {
throw err;
}
resolve({
[fileName]: data.toString()
});
});
})
);
const dependenciesChanged = compilation => {
const fileTimestampsKeys = Object.keys(compilation.fileTimestamps);
// Since there are no time stamps, assume this is the first run and emit files
if (!fileTimestampsKeys.length) {
return true;
}
const changed = fileTimestampsKeys.filter(watchfile =>
(self.prevTimestamps[watchfile] || self.startTime) < (compilation.fileTimestamps[watchfile] || Infinity)
).some(f => self.filesToConcatAbsolute.includes(f));
this.prevTimestamps = compilation.fileTimestamps;
return changed;
};
compiler.plugin('emit', (compilation, callback) => {
compilation.fileDependencies.push(...self.filesToConcatAbsolute);
if (!dependenciesChanged(compilation)) {
return callback();
}
Promise.all(concatPromise()).then(files => {
const allFiles = files.reduce((file1, file2) => Object.assign(file1, file2));
self.settings.fileName = self.getFileName(allFiles);
if (process.env.NODE_ENV === 'production' || self.settings.uglify) {
let options = {
fromString: true
};
if (typeof self.settings.uglify === 'object') {
options = Object.assign({}, self.settings.uglify, options);
}
if (self.settings.sourceMap) {
options.outSourceMap = `${self.settings.fileName.split(path.sep).slice(-1).join(path.sep)}.map`;
}
const result = UglifyJS.minify(allFiles, options);
content = result.code;
if (self.settings.sourceMap) {
const mapContent = result.map.toString();
compilation.assets[`${self.settings.fileName}.map`] = {
source() {
return mapContent;
},
size() {
return mapContent.length;
}
};
}
}
else {
content = Object.keys(allFiles)
.map(fileName => allFiles[fileName])
.reduce((content1, content2) => (`${content1}\n${content2}`), '');
}
compilation.assets[self.settings.fileName] = {
source() {
return content;
},
size() {
return content.length;
}
};
callback();
});
});
compiler.plugin('compilation', compilation => {
compilation.plugin('html-webpack-plugin-before-html-generation', (htmlPluginData, callback) => {
Promise.all(concatPromise()).then(files => {
const allFiles = files.reduce((file1, file2) => Object.assign(file1, file2));
htmlPluginData.assets.webpackConcat = htmlPluginData.assets.webpackConcat || {};
const relativePath = path.relative(htmlPluginData.outputName, self.settings.fileName)
.split(path.sep).slice(1).join('/');
htmlPluginData.assets.webpackConcat[self.settings.name] = self.getFileName(allFiles, relativePath);
callback(null, htmlPluginData);
});
});
});
}
}
module.exports = ConcatPlugin;