forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebWorkerMainTemplate.js
More file actions
86 lines (83 loc) · 3.33 KB
/
WebWorkerMainTemplate.js
File metadata and controls
86 lines (83 loc) · 3.33 KB
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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var StringSource = require("webpack-core/lib/RawSource");
function WebWorkerMainTemplate(outputOptions) {
this.outputOptions = outputOptions || {};
}
module.exports = WebWorkerMainTemplate;
var REGEXP_HASH = /\[hash\]/i;
var REGEXP_NAME = /\[name\]/g;
var REGEXP_ID = /\[id\]/i;
WebWorkerMainTemplate.prototype.render = function(hash, chunk, moduleTemplate, dependencyTemplates) {
var chunkCallbackName = this.outputOptions.chunkCallbackName || ("webpackChunk" + (this.outputOptions.library || ""));
var publicPath = this.outputOptions.publicPath || "";
var filename = this.outputOptions.filename || "bundle.js";
var chunkFilename = this.outputOptions.chunkFilename || "[id]." + filename;
var buf = [];
function addLine(indent, line) {
buf.push("/******/ ");
for(var i = 0; i < indent; i++)
buf.push("\t");
buf.push(line);
buf.push("\n");
}
function addRequireFunc(i) {
addLine(i+0, "function require(moduleId) {");
addLine(i+1, "if(installedModules[moduleId])");
addLine(i+2, "return installedModules[moduleId].exports;");
addLine(i+1, "var module = installedModules[moduleId] = {");
addLine(i+2, "exports: {},");
addLine(i+2, "id: moduleId,");
addLine(i+2, "loaded: false");
addLine(i+1, "};");
addLine(i+1, "modules[moduleId].call(null, module, module.exports, require);");
addLine(i+1, "module.loaded = true;");
addLine(i+1, "return module.exports;");
addLine(i+0, "}");
}
addLine(0, "(function webpackBootstrap(modules) {");
addLine(1, "var installedModules = {};");
addRequireFunc(1);
addLine(1, "require.e = function requireEnsure(chunkId, callback) {");
if(chunk.chunks.length == 0) {
addLine(2, "callback.call(null, require);");
} else {
addLine(2, "if(installedChunks[chunkId] === 1) return callback.call(null, require);");
addLine(2, "importScripts(" + JSON.stringify(chunkFilename.replace(REGEXP_HASH, hash).replace(REGEXP_NAME, "")).replace(REGEXP_ID, "\"+chunkId+\"") + ");");
addLine(2, "callback.call(null, require);");
}
addLine(1, "};");
addLine(1, "require.modules = modules;");
addLine(1, "require.cache = installedModules;");
if(chunk.chunks.length > 0) {
addLine(1, "var installedChunks = {0:1};");
addLine(1, "this[" + JSON.stringify(chunkCallbackName) + "] = function webpackChunkCallback(chunkIds, moreModules) {");
addLine(2, "for(var moduleId in moreModules)");
addLine(3, "modules[moduleId] = moreModules[moduleId];");
addLine(2, "for(var i = 0; i < chunkIds.length; i++)");
addLine(3, "installedChunks[chunkIds[i]] = 1;");
addLine(1, "};");
}
addLine(1, "return require(0);");
addLine(0, "})({");
addLine(0, "c: " + JSON.stringify(publicPath.replace(REGEXP_HASH, hash)) + ",");
chunk.modules.forEach(function(module, idx) {
if(idx != 0) buf.push(",\n");
buf.push("\n/***/ " + module.id + ":\n");
var source = moduleTemplate.render(module, dependencyTemplates);
buf.push(source.source());
});
buf.push("\n");
addLine(0, "})");
return new StringSource(buf.join(""));
};
WebWorkerMainTemplate.prototype.updateHash = function(hash) {
hash.update("webworker");
hash.update("2");
hash.update(this.outputOptions.publicPath + "");
hash.update(this.outputOptions.filename + "");
hash.update(this.outputOptions.chunkFilename + "");
hash.update(this.outputOptions.chunkCallbackName + "");
};