Skip to content

Commit 3dc6274

Browse files
committed
moved webworker template, added node.js template webpack#57
1 parent 9252254 commit 3dc6274

18 files changed

+363
-30
lines changed

lib/NodeStuffPlugin.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var path = require("path");
6+
var ModuleAliasPlugin = require("enhanced-resolve/lib/ModuleAliasPlugin");
7+
var ModuleParserHelpers = require("./ModuleParserHelpers");
8+
var ConstDependency = require("./dependencies/ConstDependency");
9+
10+
function NodeStuffPlugin() {
11+
}
12+
module.exports = NodeStuffPlugin;
13+
NodeStuffPlugin.prototype.apply = function(compiler) {
14+
function ignore() { return true; }
15+
compiler.parser.plugin("expression __filename", function(expr) {
16+
this.state.current.addVariable("__filename", JSON.stringify("/index.js"));
17+
return true;
18+
});
19+
compiler.parser.plugin("expression __dirname", function(expr) {
20+
this.state.current.addVariable("__dirname", JSON.stringify("/"));
21+
return true;
22+
});
23+
compiler.parser.plugin("expression require.main", function(expr) {
24+
var dep = new ConstDependency("require.cache[0]", expr.range);
25+
dep.loc = expr.loc;
26+
this.state.current.addDependency(dep);
27+
return true;
28+
});
29+
compiler.parser.plugin("expression module.exports", ignore);
30+
compiler.parser.plugin("expression module.loaded", ignore);
31+
compiler.parser.plugin("expression module.id", ignore);
32+
compiler.parser.plugin("expression module", function(expr) {
33+
return ModuleParserHelpers.addParsedVariable(this, "module", "require(" + JSON.stringify(path.join(__dirname, "..", "buildin", "module.js")) + ")(module)");
34+
});
35+
};
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
*/
55
var path = require("path");
66

7-
function NodeSourceRealPathsPlugin(context, onlyIfInContext) {
7+
function NodeStuffRealPathsPlugin(context, onlyIfInContext) {
88
this.context = context;
99
this.onlyIfInContext = onlyIfInContext;
1010
}
11-
module.exports = NodeSourceRealPathsPlugin;
12-
NodeSourceRealPathsPlugin.prototype.apply = function(compiler) {
11+
module.exports = NodeStuffRealPathsPlugin;
12+
NodeStuffRealPathsPlugin.prototype.apply = function(compiler) {
1313
var context = this.context;
1414
var onlyIfInContext = this.onlyIfInContext;
1515
compiler.parser.plugin("expression __filename", function(expr) {

lib/WebpackOptionsApply.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var OptionsApply = require("./OptionsApply");
66

77
var FunctionModulePlugin = require("./FunctionModulePlugin");
88
var JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
9+
var WebWorkerTemplatePlugin = require("./webworker/WebWorkerTemplatePlugin");
10+
var NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
911
var EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
1012
var LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
1113

@@ -18,8 +20,11 @@ var UglifyJsPlugin = require("./optimize/UglifyJsPlugin");
1820
var ConsolePlugin = require("./ConsolePlugin");
1921
var APIPlugin = require("./APIPlugin");
2022
var ConstPlugin = require("./ConstPlugin");
23+
var NodeStuffPlugin = require("./NodeStuffPlugin");
2124
var CompatibilityPlugin = require("./CompatibilityPlugin");
2225
var ProvidePlugin = require("./ProvidePlugin");
26+
var NodeSourcePlugin = require("./node/NodeSourcePlugin");
27+
var NodeTargetPlugin = require("./node/NodeTargetPlugin");
2328

2429
var CommonJsPlugin = require("./dependencies/CommonJsPlugin");
2530
var AMDPlugin = require("./dependencies/AMDPlugin");
@@ -55,10 +60,29 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
5560
compiler.apply.apply(compiler, options.plugins);
5661
}
5762
compiler.outputPath = options.output.path;
58-
compiler.apply(
59-
new JsonpTemplatePlugin(options.output),
60-
new FunctionModulePlugin(options.context, options.output)
61-
);
63+
switch(options.target) {
64+
case "web":
65+
compiler.apply(
66+
new JsonpTemplatePlugin(options.output),
67+
new FunctionModulePlugin(options.context, options.output),
68+
new NodeSourcePlugin()
69+
);
70+
break;
71+
case "webworker":
72+
compiler.apply(
73+
new WebWorkerTemplatePlugin(options.output),
74+
new FunctionModulePlugin(options.context, options.output),
75+
new NodeSourcePlugin()
76+
);
77+
break;
78+
case "node":
79+
compiler.apply(
80+
new NodeTemplatePlugin(options.output),
81+
new FunctionModulePlugin(options.context, options.output),
82+
new NodeTargetPlugin()
83+
);
84+
break;
85+
}
6286
if(options.output.library || options.output.libraryTarget != "var")
6387
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget));
6488
if(options.devtool == "eval")
@@ -77,6 +101,7 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
77101
});
78102
}
79103
compiler.apply(
104+
new NodeStuffPlugin(),
80105
new CompatibilityPlugin(),
81106
new APIPlugin(),
82107
new ConstPlugin(),

lib/WebpackOptionsDefaulter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function WebpackOptionsDefaulter(str) {
1212
this.set("minimize", true);
1313

1414
this.set("context", process.cwd());
15+
this.set("target", "web");
1516
this.set("output", {});
1617
this.set("optimize", {});
1718
this.set("resolve", {});

lib/node/NodeChunkTemplate.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var RawSource = require("webpack-core/lib/RawSource");
6+
7+
function NodeChunkTemplate(outputOptions) {
8+
this.outputOptions = outputOptions || {};
9+
}
10+
module.exports = NodeChunkTemplate;
11+
12+
NodeChunkTemplate.prototype.render = function(chunk, moduleTemplate, dependencyTemplates) {
13+
var buf = [];
14+
buf.push("module.exports = ({\n");
15+
chunk.modules.forEach(function(module, idx) {
16+
if(idx != 0) buf.push(",\n");
17+
buf.push("\n/***/ " + module.id + ":\n");
18+
var source = moduleTemplate.render(module, dependencyTemplates);
19+
buf.push(source.source());
20+
});
21+
buf.push("\n\n})");
22+
return new RawSource(buf.join(""));
23+
};
24+
25+
NodeChunkTemplate.prototype.updateHash = function(hash) {
26+
hash.update("node");
27+
hash.update("1");
28+
};

lib/node/NodeMainTemplate.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var StringSource = require("webpack-core/lib/RawSource");
6+
7+
function NodeMainTemplate(outputOptions) {
8+
this.outputOptions = outputOptions || {};
9+
}
10+
module.exports = NodeMainTemplate;
11+
12+
var REGEXP_HASH = /\[hash\]/i;
13+
var REGEXP_NAME = /\[name\]/g;
14+
var REGEXP_ID = /\[id\]/i;
15+
NodeMainTemplate.prototype.render = function(hash, chunk, moduleTemplate, dependencyTemplates) {
16+
var filename = this.outputOptions.filename || "bundle.js";
17+
var chunkFilename = this.outputOptions.chunkFilename || "[id]." + filename;
18+
var buf = [];
19+
function addLine(indent, line) {
20+
buf.push("/******/ ");
21+
for(var i = 0; i < indent; i++)
22+
buf.push("\t");
23+
buf.push(line);
24+
buf.push("\n");
25+
}
26+
function addRequireFunc(i) {
27+
addLine(i+0, "function __require(moduleId) {");
28+
addLine(i+1, "if(typeof moduleId !== \"number\") throw new Error(\"Cannot find module '\"+moduleId+\"'\");");
29+
addLine(i+1, "if(installedModules[moduleId])");
30+
addLine(i+2, "return installedModules[moduleId].exports;");
31+
addLine(i+1, "var module = installedModules[moduleId] = {");
32+
addLine(i+2, "exports: {},");
33+
addLine(i+2, "id: moduleId,");
34+
addLine(i+2, "loaded: false");
35+
addLine(i+1, "};");
36+
addLine(i+1, "modules[moduleId].call(null, module, module.exports, __require);");
37+
addLine(i+1, "module.loaded = true;");
38+
addLine(i+1, "return module.exports;");
39+
addLine(i+0, "}");
40+
}
41+
addLine(0, "(function webpackBootstrap(modules) {");
42+
addLine(1, "var installedModules = {};");
43+
addRequireFunc(1);
44+
addLine(1, "__require.e = function requireEnsure(chunkId, callback) {");
45+
if(chunk.chunks.length == 0) {
46+
addLine(2, "callback.call(null, __require);");
47+
} else {
48+
addLine(2, "if(installedChunks[chunkId] === 1) return callback.call(null, __require);");
49+
addLine(2, "var moreModules = require(" + JSON.stringify("./" + chunkFilename.replace(REGEXP_HASH, hash).replace(REGEXP_NAME, "")).replace(REGEXP_ID, "\"+chunkId+\"") + ");");
50+
addLine(2, "for(var moduleId in moreModules)");
51+
addLine(3, "modules[moduleId] = moreModules[moduleId];");
52+
addLine(2, "callback.call(null, __require);");
53+
}
54+
addLine(1, "};");
55+
addLine(1, "__require.modules = modules;");
56+
addLine(1, "__require.cache = installedModules;");
57+
addLine(1, "__require.parentRequire = require;");
58+
if(chunk.chunks.length > 0) {
59+
addLine(1, "var installedChunks = {0:1};");
60+
}
61+
addLine(1, "return __require(0);");
62+
addLine(0, "})({");
63+
addLine(0, "c: __dirname,");
64+
chunk.modules.forEach(function(module, idx) {
65+
if(idx != 0) buf.push(",\n");
66+
buf.push("\n/***/ " + module.id + ":\n");
67+
var source = moduleTemplate.render(module, dependencyTemplates);
68+
buf.push(source.source());
69+
});
70+
buf.push("\n");
71+
addLine(0, "})");
72+
return new StringSource(buf.join(""));
73+
};
74+
75+
NodeMainTemplate.prototype.updateHash = function(hash) {
76+
hash.update("node");
77+
hash.update("1");
78+
hash.update(this.outputOptions.filename + "");
79+
hash.update(this.outputOptions.chunkFilename + "");
80+
};

lib/node/NodeSourcePlugin.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,6 @@ NodeSourcePlugin.prototype.apply = function(compiler) {
1919
this.state.current.addVariable("global", "this");
2020
return true;
2121
});
22-
compiler.parser.plugin("expression __filename", function(expr) {
23-
this.state.current.addVariable("__filename", JSON.stringify("/index.js"));
24-
return true;
25-
});
26-
compiler.parser.plugin("expression __dirname", function(expr) {
27-
this.state.current.addVariable("__dirname", JSON.stringify("/"));
28-
return true;
29-
});
30-
compiler.parser.plugin("expression require.main", function(expr) {
31-
var dep = new ConstDependency("require.cache[0]", expr.range);
32-
dep.loc = expr.loc;
33-
this.state.current.addDependency(dep);
34-
return true;
35-
});
36-
compiler.parser.plugin("expression module.exports", ignore);
37-
compiler.parser.plugin("expression module.loaded", ignore);
38-
compiler.parser.plugin("expression module.id", ignore);
39-
compiler.parser.plugin("expression module", function(expr) {
40-
return ModuleParserHelpers.addParsedVariable(this, "module", "require(" + JSON.stringify(path.join(__dirname, "..", "..", "buildin", "module.js")) + ")(module)");
41-
});
4222
compiler.resolvers.normal.apply(
4323
new ModuleAliasPlugin((function() {
4424
return [

lib/node/NodeTargetPlugin.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var Dependency = require("../Dependency");
6+
var Module = require("../Module");
7+
var RawSource = require("webpack-core/lib/RawSource");
8+
9+
10+
11+
function NodeNativeDependency(request, range) {
12+
Dependency.call(this);
13+
this.Class = NodeNativeDependency;
14+
this.userRequest = request;
15+
this.request = request;
16+
this.range = range;
17+
}
18+
19+
NodeNativeDependency.prototype = Object.create(Dependency.prototype);
20+
NodeNativeDependency.prototype.type = "native module";
21+
22+
NodeNativeDependency.prototype.isEqualResource = function isEqualResource(other) {
23+
if(!(other instanceof NodeNativeDependency))
24+
return false;
25+
return this.request == other.request;
26+
};
27+
28+
NodeNativeDependency.Template = require("../dependencies/ModuleDependencyTemplateAsRequireId");
29+
30+
31+
32+
function NodeNativeCommonJsDependency(request, range) {
33+
NodeNativeDependency.call(this, request, range);
34+
this.Class = NodeNativeCommonJsDependency;
35+
}
36+
37+
NodeNativeCommonJsDependency.prototype = Object.create(NodeNativeDependency.prototype);
38+
NodeNativeCommonJsDependency.Template = require("../dependencies/ModuleDependencyTemplateAsId");
39+
40+
41+
42+
function NodeNativeModule(request) {
43+
Module.call(this);
44+
this.request = request;
45+
this.built = false;
46+
this.cacheable = true;
47+
}
48+
NodeNativeModule.prototype = Object.create(Module.prototype);
49+
50+
NodeNativeModule.prototype.identifier = NodeNativeModule.prototype.readableIdentifier = function() {
51+
return this.request;
52+
};
53+
54+
NodeNativeModule.prototype.build = function(options, compilation, resolver, fs, callback) {callback()};
55+
56+
NodeNativeModule.prototype.source = function() {
57+
return new RawSource("module.exports = require.parentRequire(" + JSON.stringify(this.request) + ");");
58+
};
59+
60+
NodeNativeModule.prototype.needRebuild = function() {
61+
return false;
62+
};
63+
64+
NodeNativeModule.prototype.size = function() {
65+
return 42 + this.request.length;
66+
};
67+
68+
69+
70+
function NodeNativeModuleFactory() {
71+
}
72+
NodeNativeModuleFactory.prototype.create = function(context, dependency, callback) {
73+
return callback(null, new NodeNativeModule(dependency.request));
74+
}
75+
76+
77+
78+
79+
function NodeTargetPlugin() {
80+
}
81+
module.exports = NodeTargetPlugin;
82+
NodeTargetPlugin.prototype.apply = function(compiler) {
83+
compiler.plugin("compilation", function(compilation, params) {
84+
compilation.dependencyFactories.set(NodeNativeDependency, new NodeNativeModuleFactory());
85+
compilation.dependencyTemplates.set(NodeNativeDependency, new NodeNativeDependency.Template());
86+
87+
compilation.dependencyFactories.set(NodeNativeCommonJsDependency, new NodeNativeModuleFactory());
88+
compilation.dependencyTemplates.set(NodeNativeCommonJsDependency, new NodeNativeCommonJsDependency.Template());
89+
});
90+
91+
var natives = Object.keys(process.binding("natives"));
92+
compiler.parser.plugin("call require:commonjs:item", function(expr, param) {
93+
if(param.isString() && natives.indexOf(param.string) >= 0) {
94+
var dep = new NodeNativeCommonJsDependency(param.string, param.range);
95+
this.state.current.addDependency(dep);
96+
return true;
97+
}
98+
});
99+
compiler.parser.plugin("call define:amd:item", function(expr, param) {
100+
if(param.isString() && natives.indexOf(param.string) >= 0) {
101+
var dep = new NodeNativeDependency(param.string, param.range);
102+
this.state.current.addDependency(dep);
103+
return true;
104+
}
105+
});
106+
compiler.parser.plugin("call require:amd:item", function(expr, param) {
107+
if(param.isString() && natives.indexOf(param.string) >= 0) {
108+
var dep = new NodeNativeDependency(param.string, param.range);
109+
this.state.current.addDependency(dep);
110+
return true;
111+
}
112+
});
113+
};

0 commit comments

Comments
 (0)