-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (85 loc) · 3.01 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
var es3ify = require("es3ify");
var jstransform = require("jstransform");
var RawSource = require("webpack-sources").RawSource;
var SourceMapSource = require("webpack-sources").SourceMapSource;
/**
* Plugin to be used in the `plugins` section in webpack configuration.
* @param {object} options - settings for this plugin
* @constructor
*/
function ES5to3OutputPlugin(options) {
this.options = options || {};
}
/**
* Will be called automatically by webpack to register the plugin.
* @param {Compiler} compiler - webpack compiler located in webpack/lib/Compiler
*/
ES5to3OutputPlugin.prototype.apply = function(compiler) {
var options = this.options;
var generateSourceMap = compiler.options.devtool;
// handle JS files only
options.test = options.test || /\.js$/i;
if (typeof options.test === "string") {
options.test = new RegExp("^" + options.test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
}
compiler.plugin("compilation", function(compilation) {
compilation.plugin("optimize-chunk-assets", function(chunks, callback) {
var files = [];
// collect all files
chunks.forEach(function(chunk) {
chunk.files.forEach(function(file) {
files.push(file);
});
});
compilation.additionalChunkAssets.forEach(function(file) {
files.push(file);
});
// filter JS files
files = files.filter(function(elem) {
return options.test.test(elem);
});
// transform each JS file
files.forEach(function(file) {
var asset = compilation.assets[file];
var source;
var inputSourceMap;
var sourceMap;
var result;
var transformOptions = {};
// check if asset was transformed before (and cached)
if (asset.__es5to3) {
compilation.assets[file] = asset.__es5to3;
return;
}
if(generateSourceMap !== false) {
if(asset.sourceAndMap) {
var sourceAndMap = asset.sourceAndMap();
inputSourceMap = sourceAndMap.map;
source = sourceAndMap.source;
} else {
inputSourceMap = asset.map();
source = asset.source();
}
transformOptions.sourceMap = true;
transformOptions.filename = file;
} else {
source = asset.source();
}
// transform source code
result = jstransform.transform(es3ify.visitorList, source, transformOptions);
// convert generated source map result
if (result.sourceMap) {
sourceMap = result.sourceMap.toJSON();
sourceMap.sources = [file]; // override due to wrong result from jstransform
sourceMap.sourcesContent = [source];
result.sourceMap = sourceMap;
}
asset.__es5to3 = compilation.assets[file] = result.sourceMap ?
new SourceMapSource(result.code, file, result.sourceMap, source, inputSourceMap) :
new RawSource(result.code);
});
callback();
});
});
};
module.exports = ES5to3OutputPlugin;