forked from leftstick/unminified-webpack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (52 loc) · 1.93 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
'use strict';
var webpack = require('webpack');
var ModuleFilenameHelpers = require('webpack/lib/ModuleFilenameHelpers');
var getFileName = function(name, opts) {
if (name.match(/([-_.]min)[-_.]/)) {
return name.replace(/[-_.]min/, '');
}
var suffix = (opts.postfix || 'nomin') + '.js';
if (name.match(/\.js$/)) {
return name.replace(/js$/, suffix)
}
return name + suffix;
};
var UnminifiedWebpackPlugin = function(opts) {
this.options = opts || {};
};
UnminifiedWebpackPlugin.prototype.apply = function(compiler) {
var options = this.options;
options.test = options.test || /\.js($|\?)/i;
var containUgly = compiler.options.plugins.filter(function(plugin) {
return plugin instanceof webpack.optimize.UglifyJsPlugin;
});
if (!containUgly.length) {
return console.log('Ignore generating unminified version, since no UglifyJsPlugin provided');
}
compiler.plugin('compilation', function(compilation) {
compilation.plugin('additional-chunk-assets', function(chunks, cb) {
var files = [];
chunks.forEach(function(chunk) {
chunk.files.forEach(function(file) {
files.push(file);
});
});
compilation.additionalChunkAssets.forEach(function(file) {
files.push(file);
});
files = files.filter(ModuleFilenameHelpers.matchObject.bind(null, options));
files.forEach(function(file) {
var asset = compilation.assets[file];
compilation.assets[getFileName(file, options)] = {
source: function() {
return asset.source();
},
size: function() {
return asset.source().length;
}
};
});
});
});
};
module.exports = UnminifiedWebpackPlugin;