Skip to content

Commit bea08dc

Browse files
committed
added node-webkit target (experimental)
added option to provide no target added defaults for node-webkit allow browsertests to run in node-webkit
1 parent a942f67 commit bea08dc

9 files changed

Lines changed: 90 additions & 49 deletions

File tree

‎lib/JsonpTemplatePlugin.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,4 @@ JsonpTemplatePlugin.prototype.apply = function(compiler) {
1515
compiler.mainTemplate = new JsonpMainTemplate(options);
1616
compiler.chunkTemplate = new JsonpChunkTemplate(options);
1717
compiler.hotUpdateChunkTemplate = new JsonpHotUpdateChunkTemplate(options);
18-
compiler.plugin("compilation", function(compilation) {
19-
compilation.plugin("normal-module-loader", function(loaderContext) {
20-
loaderContext.target = "web";
21-
});
22-
});
2318
};

‎lib/LoaderTargetPlugin.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var JsonpMainTemplate = require("./JsonpMainTemplate");
6+
var JsonpChunkTemplate = require("./JsonpChunkTemplate");
7+
var JsonpHotUpdateChunkTemplate = require("./JsonpHotUpdateChunkTemplate");
8+
9+
function LoaderTargetPlugin(target) {
10+
this.target = target;
11+
}
12+
module.exports = LoaderTargetPlugin;
13+
LoaderTargetPlugin.prototype.apply = function(compiler) {
14+
var target = this.target;
15+
compiler.plugin("compilation", function(compilation) {
16+
compilation.plugin("normal-module-loader", function(loaderContext) {
17+
loaderContext.target = target;
18+
});
19+
});
20+
};

‎lib/WebpackOptionsApply.js‎

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
var OptionsApply = require("./OptionsApply");
66

7+
var LoaderTargetPlugin = require("./LoaderTargetPlugin");
78
var FunctionModulePlugin = require("./FunctionModulePlugin");
89
var EvalDevToolModulePlugin = require("./EvalDevToolModulePlugin");
910
var SourceMapDevToolPlugin = require("./SourceMapDevToolPlugin");
@@ -64,36 +65,56 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
6465
compiler.outputPath = options.output.path;
6566
compiler.recordsInputPath = options.recordsInputPath || options.recordsPath;
6667
compiler.recordsOutputPath = options.recordsOutputPath || options.recordsPath;
67-
switch(options.target) {
68-
case "web":
69-
var JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
70-
var NodeSourcePlugin = require("./node/NodeSourcePlugin");
71-
compiler.apply(
72-
new JsonpTemplatePlugin(options.output),
73-
new FunctionModulePlugin(options.output),
74-
new NodeSourcePlugin(options.node)
75-
);
76-
break;
77-
case "webworker":
78-
var WebWorkerTemplatePlugin = require("./webworker/WebWorkerTemplatePlugin");
79-
var NodeSourcePlugin = require("./node/NodeSourcePlugin");
80-
compiler.apply(
81-
new WebWorkerTemplatePlugin(options.output),
82-
new FunctionModulePlugin(options.output),
83-
new NodeSourcePlugin(options.node)
84-
);
85-
break;
86-
case "node":
87-
case "async-node":
88-
var NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
89-
var NodeTargetPlugin = require("./node/NodeTargetPlugin");
90-
compiler.apply(
91-
new NodeTemplatePlugin(options.output, options.target === "async-node"),
92-
new FunctionModulePlugin(options.output),
93-
new NodeTargetPlugin()
94-
);
95-
break;
96-
default:
68+
if(typeof options.target === "string") {
69+
switch(options.target) {
70+
case "web":
71+
var JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
72+
var NodeSourcePlugin = require("./node/NodeSourcePlugin");
73+
compiler.apply(
74+
new JsonpTemplatePlugin(options.output),
75+
new FunctionModulePlugin(options.output),
76+
new NodeSourcePlugin(options.node),
77+
new LoaderTargetPlugin("web")
78+
);
79+
break;
80+
case "webworker":
81+
var WebWorkerTemplatePlugin = require("./webworker/WebWorkerTemplatePlugin");
82+
var NodeSourcePlugin = require("./node/NodeSourcePlugin");
83+
compiler.apply(
84+
new WebWorkerTemplatePlugin(options.output),
85+
new FunctionModulePlugin(options.output),
86+
new NodeSourcePlugin(options.node),
87+
new LoaderTargetPlugin("webworker")
88+
);
89+
break;
90+
case "node":
91+
case "async-node":
92+
var NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
93+
var NodeTargetPlugin = require("./node/NodeTargetPlugin");
94+
compiler.apply(
95+
new NodeTemplatePlugin(options.output, options.target === "async-node"),
96+
new FunctionModulePlugin(options.output),
97+
new NodeTargetPlugin(),
98+
new LoaderTargetPlugin("node")
99+
);
100+
break;
101+
case "node-webkit":
102+
var JsonpTemplatePlugin = require("./JsonpTemplatePlugin");
103+
var NodeTargetPlugin = require("./node/NodeTargetPlugin");
104+
compiler.apply(
105+
new JsonpTemplatePlugin(options.output),
106+
new FunctionModulePlugin(options.output),
107+
new NodeTargetPlugin(),
108+
new ExternalsPlugin("commonjs", "nw.gui"),
109+
new LoaderTargetPlugin("node-webkit")
110+
);
111+
break;
112+
default:
113+
throw new Error("Unsupported target '" + options.target + "'.");
114+
}
115+
} else if(options.target !== false) {
116+
options.target(compiler);
117+
} else {
97118
throw new Error("Unsupported target '" + options.target + "'.");
98119
}
99120
if(options.output.library || options.output.libraryTarget != "var") {

‎lib/WebpackOptionsDefaulter.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ WebpackOptionsDefaulter.prototype.process = function(options) {
6868
options.resolve.packageAlias = "browser";
6969
}
7070

71-
function defaultByTarget(value, web, webworker, node, def) {
71+
function defaultByTarget(value, web, webworker, node, nodeWebkit, def) {
7272
if(value !== undefined) return value;
7373
switch(options.target) {
7474
case "web": return web;
7575
case "webworker": return webworker;
7676
case "node": case "async-node": return node;
77+
case "node-webkit": return nodeWebkit;
7778
default: return def;
7879
}
7980
}
@@ -82,47 +83,55 @@ WebpackOptionsDefaulter.prototype.process = function(options) {
8283
["web_modules", "node_modules"],
8384
["webworker_modules", "web_modules", "node_modules"],
8485
["node_modules"],
86+
["node_modules", "web_modules"],
8587
["node_modules"]);
8688

8789
options.resolveLoader.modulesDirectories = defaultByTarget(options.resolveLoader.modulesDirectories,
8890
["web_loaders", "web_modules", "node_loaders", "node_modules"],
8991
["webworker_loaders", "web_loaders", "web_modules", "node_loaders", "node_modules"],
9092
["node_loaders", "node_modules"],
93+
["node_loaders", "web_loaders", "node_modules", "web_modules"],
9194
["node_modules"]);
9295

9396
options.resolve.packageMains = defaultByTarget(options.resolve.packageMains,
9497
["webpack", "browser", "web", "browserify", ["jam", "main"], "main"],
9598
["webpackWorker", "webworker", "webpack", "browser", "web", "browserify", ["jam", "main"], "main"],
9699
["webpackNode", "node", "main"],
100+
["webpackNodeWebkit", "webpackNode", "node", "main", "web"],
97101
["main"]);
98102

99103
options.resolve.packageAlias = defaultByTarget(options.resolve.packageAlias,
100104
"browser",
101105
"browser",
102106
false,
107+
"node-webkit",
103108
false);
104109

105110
options.resolveLoader.packageMains = defaultByTarget(options.resolveLoader.packageMains,
106111
["webpackLoader", "webLoader", "loader", "main"],
107112
["webpackWorkerLoader", "webworkerLoader", "webLoader", "loader", "main"],
108113
["webpackNodeLoader", "nodeLoader", "loader", "main"],
114+
["webpackNodeWebkitLoader", "webpackNodeLoader", "nodeLoader", "loader", "webLoader", "main"],
109115
["loader", "main"]);
110116

111117
options.resolve.extensions = defaultByTarget(options.resolve.extensions,
112118
["", ".webpack.js", ".web.js", ".js", ".json"],
113119
["", ".webpack-worker.js", ".webworker.js", ".web.js", ".js", ".json"],
114120
["", ".webpack-node.js", ".js", ".json", ".node"],
121+
["", ".webpack-node-webkit.js", ".webpack-node.js", ".js", ".json", ".node", ".web.js"],
115122
["", ".js", ".json"]);
116123

117124
options.resolveLoader.extensions = defaultByTarget(options.resolveLoader.extensions,
118125
["", ".webpack-loader.js", ".web-loader.js", ".loader.js", ".js"],
119126
["", ".webpack-worker-loader.js", ".webpack-loader.js", ".webworker-loader.js", ".web-loader.js", ".loader.js", ".js"],
120127
["", ".webpack-node-loader.js", ".loader.js", ".js"],
128+
["", ".webpack-node-webkit-loader.js", ".webpack-node-loader.js", ".loader.js", ".web-loader.js", ".js"],
121129
["", ".js"]);
122130

123131
options.resolveLoader.moduleTemplates = defaultByTarget(options.resolveLoader.moduleTemplates,
124132
["*-webpack-loader", "*-web-loader", "*-loader", "*"],
125133
["*-webpack-worker-loader", "*-webworker-loader", "*-web-loader", "*-loader", "*"],
126134
["*-webpack-node-loader", "*-node-loader", "*-loader", "*"],
135+
["*-webpack-node-webkit-loader", "*-webpack-node-loader", "*-node-loader", "*-loader", "*-web-loader", "*"],
127136
["*-loader", "*"]);
128137
};

‎lib/node/NodeTemplatePlugin.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,4 @@ NodeTemplatePlugin.prototype.apply = function(compiler) {
1616
compiler.mainTemplate = new NodeMainTemplate(options, this.asyncChunkLoading);
1717
compiler.chunkTemplate = new NodeChunkTemplate(options);
1818
compiler.hotUpdateChunkTemplate = new NodeHotUpdateChunkTemplate(options);
19-
compiler.plugin("compilation", function(compilation) {
20-
compilation.plugin("normal-module-loader", function(loaderContext) {
21-
loaderContext.target = "node";
22-
});
23-
});
2419
};

‎lib/webworker/WebWorkerTemplatePlugin.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,4 @@ WebWorkerTemplatePlugin.prototype.apply = function(compiler) {
1313
var options = this.options;
1414
compiler.mainTemplate = new WebWorkerMainTemplate(options);
1515
compiler.chunkTemplate = new WebWorkerChunkTemplate(options);
16-
compiler.plugin("compilation", function(compilation) {
17-
compilation.plugin("normal-module-loader", function(loaderContext) {
18-
loaderContext.target = "webworker";
19-
});
20-
});
2116
};

‎test/browsertest/lib/index.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
require("./index.web.js");
2+
13
describe("main", function() {
24
it("should load index.web.js instead of index.js", function() {
3-
should.fail();
5+
throw new Error("Fail");
46
});
57
});

‎test/browsertest/mocha.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4802,10 +4802,10 @@ exports.highlightTags = function(name) {
48024802
* the browser.
48034803
*/
48044804

4805-
process = {};
4805+
var process = {};
48064806
process.exit = function(status){};
48074807
process.stdout = {};
4808-
global = window;
4808+
var global = window;
48094809

48104810
/**
48114811
* next tick implementation.

‎test/browsertest/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "node-webkit-tests",
3+
"main": "tests.html"
4+
}

0 commit comments

Comments
 (0)