Skip to content

Commit 4369dfa

Browse files
authored
Merge pull request webpack#4320 from sendilkumarn/refactor-ww
refactor(es6) Webworker module
2 parents 1092ff4 + f2367c2 commit 4369dfa

File tree

2 files changed

+106
-102
lines changed

2 files changed

+106
-102
lines changed

lib/webworker/WebWorkerMainTemplatePlugin.js

Lines changed: 95 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,106 +2,107 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
var Template = require("../Template");
5+
"use strict";
66

7-
function WebWorkerMainTemplatePlugin() {}
8-
module.exports = WebWorkerMainTemplatePlugin;
7+
var Template = require("../Template");
98

10-
WebWorkerMainTemplatePlugin.prototype.constructor = WebWorkerMainTemplatePlugin;
11-
WebWorkerMainTemplatePlugin.prototype.apply = function(mainTemplate) {
12-
mainTemplate.plugin("local-vars", function(source, chunk) {
13-
if(chunk.chunks.length > 0) {
14-
return this.asString([
15-
source,
16-
"",
17-
"// object to store loaded chunks",
18-
"// \"1\" means \"already loaded\"",
19-
"var installedChunks = {",
20-
this.indent(
21-
chunk.ids.map(function(id) {
22-
return id + ": 1";
23-
}).join(",\n")
24-
),
25-
"};"
26-
]);
27-
}
28-
return source;
29-
});
30-
mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
31-
var chunkFilename = this.outputOptions.chunkFilename;
32-
return this.asString([
33-
"// \"1\" is the signal for \"already loaded\"",
34-
"if(!installedChunks[chunkId]) {",
35-
this.indent([
36-
"importScripts(" +
37-
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
38-
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
39-
hashWithLength: function(length) {
40-
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
41-
}.bind(this),
42-
chunk: {
43-
id: "\" + chunkId + \""
44-
}
45-
}) + ");"
46-
]),
47-
"}",
48-
"return Promise.resolve();"
49-
]);
50-
});
51-
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
52-
if(chunk.chunks.length > 0) {
53-
var chunkCallbackName = this.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (this.outputOptions.library || ""));
9+
class WebWorkerMainTemplatePlugin {
10+
apply(mainTemplate) {
11+
mainTemplate.plugin("local-vars", function(source, chunk) {
12+
if(chunk.chunks.length > 0) {
13+
return this.asString([
14+
source,
15+
"",
16+
"// object to store loaded chunks",
17+
"// \"1\" means \"already loaded\"",
18+
"var installedChunks = {",
19+
this.indent(
20+
chunk.ids.map(function(id) {
21+
return id + ": 1";
22+
}).join(",\n")
23+
),
24+
"};"
25+
]);
26+
}
27+
return source;
28+
});
29+
mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
30+
var chunkFilename = this.outputOptions.chunkFilename;
5431
return this.asString([
55-
source,
56-
"this[" + JSON.stringify(chunkCallbackName) + "] = function webpackChunkCallback(chunkIds, moreModules) {",
32+
"// \"1\" is the signal for \"already loaded\"",
33+
"if(!installedChunks[chunkId]) {",
5734
this.indent([
58-
"for(var moduleId in moreModules) {",
59-
this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
60-
"}",
61-
"while(chunkIds.length)",
62-
this.indent("installedChunks[chunkIds.pop()] = 1;")
35+
"importScripts(" +
36+
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
37+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
38+
hashWithLength: function(length) {
39+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
40+
}.bind(this),
41+
chunk: {
42+
id: "\" + chunkId + \""
43+
}
44+
}) + ");"
6345
]),
64-
"};"
46+
"}",
47+
"return Promise.resolve();"
6548
]);
66-
}
67-
return source;
68-
});
69-
mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
70-
var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
71-
var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
72-
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
73-
var currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
74-
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
75-
hashWithLength: function(length) {
76-
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
77-
}.bind(this),
78-
chunk: {
79-
id: "\" + chunkId + \""
80-
}
8149
});
82-
var currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
83-
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
84-
hashWithLength: function(length) {
85-
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
86-
}.bind(this)
50+
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
51+
if(chunk.chunks.length > 0) {
52+
var chunkCallbackName = this.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (this.outputOptions.library || ""));
53+
return this.asString([
54+
source,
55+
"this[" + JSON.stringify(chunkCallbackName) + "] = function webpackChunkCallback(chunkIds, moreModules) {",
56+
this.indent([
57+
"for(var moduleId in moreModules) {",
58+
this.indent(this.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
59+
"}",
60+
"while(chunkIds.length)",
61+
this.indent("installedChunks[chunkIds.pop()] = 1;")
62+
]),
63+
"};"
64+
]);
65+
}
66+
return source;
8767
});
68+
mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
69+
var hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
70+
var hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
71+
var hotUpdateFunction = this.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (this.outputOptions.library || ""));
72+
var currentHotUpdateChunkFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateChunkFilename), {
73+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
74+
hashWithLength: function(length) {
75+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
76+
}.bind(this),
77+
chunk: {
78+
id: "\" + chunkId + \""
79+
}
80+
});
81+
var currentHotUpdateMainFilename = this.applyPluginsWaterfall("asset-path", JSON.stringify(hotUpdateMainFilename), {
82+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
83+
hashWithLength: function(length) {
84+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
85+
}.bind(this)
86+
});
8887

89-
return source + "\n" +
90-
"var parentHotUpdateCallback = this[" + JSON.stringify(hotUpdateFunction) + "];\n" +
91-
"this[" + JSON.stringify(hotUpdateFunction) + "] = " + Template.getFunctionContent(require("./WebWorkerMainTemplate.runtime.js"))
92-
.replace(/\/\/\$semicolon/g, ";")
93-
.replace(/\$require\$/g, this.requireFn)
94-
.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
95-
.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
96-
.replace(/\$hash\$/g, JSON.stringify(hash));
97-
});
98-
mainTemplate.plugin("hash", function(hash) {
99-
hash.update("webworker");
100-
hash.update("3");
101-
hash.update(this.outputOptions.publicPath + "");
102-
hash.update(this.outputOptions.filename + "");
103-
hash.update(this.outputOptions.chunkFilename + "");
104-
hash.update(this.outputOptions.chunkCallbackName + "");
105-
hash.update(this.outputOptions.library + "");
106-
});
107-
};
88+
return source + "\n" +
89+
"var parentHotUpdateCallback = this[" + JSON.stringify(hotUpdateFunction) + "];\n" +
90+
"this[" + JSON.stringify(hotUpdateFunction) + "] = " + Template.getFunctionContent(require("./WebWorkerMainTemplate.runtime.js"))
91+
.replace(/\/\/\$semicolon/g, ";")
92+
.replace(/\$require\$/g, this.requireFn)
93+
.replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
94+
.replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
95+
.replace(/\$hash\$/g, JSON.stringify(hash));
96+
});
97+
mainTemplate.plugin("hash", function(hash) {
98+
hash.update("webworker");
99+
hash.update("3");
100+
hash.update(`${this.outputOptions.publicPath}`);
101+
hash.update(`${this.outputOptions.filename}`);
102+
hash.update(`${this.outputOptions.chunkFilename}`);
103+
hash.update(`${this.outputOptions.chunkCallbackName}`);
104+
hash.update(`${this.outputOptions.library}`);
105+
});
106+
}
107+
}
108+
module.exports = WebWorkerMainTemplatePlugin;

lib/webworker/WebWorkerTemplatePlugin.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
"use strict";
6+
57
var WebWorkerMainTemplatePlugin = require("./WebWorkerMainTemplatePlugin");
68
var WebWorkerChunkTemplatePlugin = require("./WebWorkerChunkTemplatePlugin");
79
var WebWorkerHotUpdateChunkTemplatePlugin = require("./WebWorkerHotUpdateChunkTemplatePlugin");
810

9-
function WebWorkerTemplatePlugin() {}
11+
class WebWorkerTemplatePlugin {
12+
apply(compiler) {
13+
compiler.plugin("this-compilation", compilation => {
14+
compilation.mainTemplate.apply(new WebWorkerMainTemplatePlugin());
15+
compilation.chunkTemplate.apply(new WebWorkerChunkTemplatePlugin());
16+
compilation.hotUpdateChunkTemplate.apply(new WebWorkerHotUpdateChunkTemplatePlugin());
17+
});
18+
}
19+
}
1020
module.exports = WebWorkerTemplatePlugin;
11-
WebWorkerTemplatePlugin.prototype.apply = function(compiler) {
12-
compiler.plugin("this-compilation", function(compilation) {
13-
compilation.mainTemplate.apply(new WebWorkerMainTemplatePlugin());
14-
compilation.chunkTemplate.apply(new WebWorkerChunkTemplatePlugin());
15-
compilation.hotUpdateChunkTemplate.apply(new WebWorkerHotUpdateChunkTemplatePlugin());
16-
});
17-
};

0 commit comments

Comments
 (0)