Skip to content

Commit 6220b4e

Browse files
willmendesnetoTheLarkInn
authored andcommitted
refactor(ES6): upgrade SetVarMainTemplatePlugin to ES6 (webpack#3726)
1 parent ffec5bb commit 6220b4e

1 file changed

Lines changed: 34 additions & 29 deletions

File tree

lib/SetVarMainTemplatePlugin.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,40 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var ConcatSource = require("webpack-sources").ConcatSource;
5+
"use strict";
66

7-
function SetVarMainTemplatePlugin(varExpression, copyObject) {
8-
this.varExpression = varExpression;
9-
this.copyObject = copyObject;
7+
const ConcatSource = require("webpack-sources").ConcatSource;
8+
9+
class SetVarMainTemplatePlugin {
10+
constructor(varExpression, copyObject) {
11+
this.varExpression = varExpression;
12+
this.copyObject = copyObject;
13+
}
14+
15+
apply(compilation) {
16+
const mainTemplate = compilation.mainTemplate;
17+
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
18+
const varExpression = mainTemplate.applyPluginsWaterfall("asset-path", this.varExpression, {
19+
hash,
20+
chunk
21+
});
22+
if(this.copyObject) {
23+
return new ConcatSource(`(function(e, a) { for(var i in a) e[i] = a[i]; }(${varExpression}, `, source, "))");
24+
} else {
25+
const prefix = `${varExpression} =\n`;
26+
return new ConcatSource(prefix, source);
27+
}
28+
});
29+
mainTemplate.plugin("global-hash-paths", function(paths) {
30+
if(this.varExpression) paths.push(this.varExpression);
31+
return paths;
32+
});
33+
mainTemplate.plugin("hash", hash => {
34+
hash.update("set var");
35+
hash.update(`${this.varExpression}`);
36+
hash.update(`${this.copyObject}`);
37+
});
38+
}
1039
}
40+
1141
module.exports = SetVarMainTemplatePlugin;
12-
SetVarMainTemplatePlugin.prototype.apply = function(compilation) {
13-
var mainTemplate = compilation.mainTemplate;
14-
compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) {
15-
var varExpression = mainTemplate.applyPluginsWaterfall("asset-path", this.varExpression, {
16-
hash: hash,
17-
chunk: chunk
18-
});
19-
if(this.copyObject) {
20-
return new ConcatSource("(function(e, a) { for(var i in a) e[i] = a[i]; }(" +
21-
varExpression + ", ", source, "))");
22-
} else {
23-
var prefix = varExpression + " =\n";
24-
return new ConcatSource(prefix, source);
25-
}
26-
}.bind(this));
27-
mainTemplate.plugin("global-hash-paths", function(paths) {
28-
if(this.varExpression) paths.push(this.varExpression);
29-
return paths;
30-
});
31-
mainTemplate.plugin("hash", function(hash) {
32-
hash.update("set var");
33-
hash.update(this.varExpression + "");
34-
hash.update(this.copyObject + "");
35-
}.bind(this));
36-
};

0 commit comments

Comments
 (0)