Skip to content

Commit

Permalink
add output.globalObject configuration option
Browse files Browse the repository at this point in the history
remove deprecated atom and electron target
add self libraryTarget
add self external type
  • Loading branch information
sokra committed Dec 27, 2017
1 parent 9bfc7fc commit d95f93f
Show file tree
Hide file tree
Showing 46 changed files with 170 additions and 221 deletions.
10 changes: 6 additions & 4 deletions lib/ExternalModule.js
Expand Up @@ -83,13 +83,15 @@ class ExternalModule extends Module {
return `${missingModuleError}module.exports = ${request};`;
}

getSourceString() {
getSourceString(runtime) {
const request = typeof this.request === "object" ? this.request[this.externalType] : this.request;
switch(this.externalType) {
case "this":
case "window":
case "global":
case "self":
return this.getSourceForGlobalVariableExternal(request, this.externalType);
case "global":
return this.getSourceForGlobalVariableExternal(runtime.outputOptions.globalObject, this.externalType);
case "commonjs":
case "commonjs2":
return this.getSourceForCommonJsExternal(request);
Expand All @@ -110,9 +112,9 @@ class ExternalModule extends Module {
return new RawSource(sourceString);
}

source() {
source(dependencyTemplates, runtime) {
return this.getSource(
this.getSourceString()
this.getSourceString(runtime)
);
}

Expand Down
8 changes: 7 additions & 1 deletion lib/LibraryTemplatePlugin.js
Expand Up @@ -48,13 +48,19 @@ class LibraryTemplatePlugin {
new SetVarMainTemplatePlugin(accessorAccess(undefined, this.name)).apply(compilation);
break;
case "this":
case "self":
case "window":
case "global":
if(this.name)
new SetVarMainTemplatePlugin(accessorAccess(this.target, this.name)).apply(compilation);
else
new SetVarMainTemplatePlugin(this.target, true).apply(compilation);
break;
case "global":
if(this.name)
new SetVarMainTemplatePlugin(accessorAccess(compilation.runtimeTemplate.outputOptions.globalObject, this.name)).apply(compilation);
else
new SetVarMainTemplatePlugin(compilation.runtimeTemplate.outputOptions.globalObject, true).apply(compilation);
break;
case "commonjs":
if(this.name)
new SetVarMainTemplatePlugin(accessorAccess("exports", this.name)).apply(compilation);
Expand Down
21 changes: 11 additions & 10 deletions lib/UmdMainTemplatePlugin.js
Expand Up @@ -42,7 +42,8 @@ class UmdMainTemplatePlugin {
apply(compilation) {
const {
mainTemplate,
chunkTemplate
chunkTemplate,
runtimeTemplate
} = compilation;

const onRenderWithEntry = (source, chunk, hash) => {
Expand Down Expand Up @@ -123,18 +124,18 @@ class UmdMainTemplatePlugin {
"(function webpackUniversalModuleDefinition(root, factory) {\n" +
(this.auxiliaryComment &&
typeof this.auxiliaryComment === "string" ?
" //" + this.auxiliaryComment + "\n" :
" //" + this.auxiliaryComment + "\n" :
this.auxiliaryComment.commonjs2 ?
" //" + this.auxiliaryComment.commonjs2 + "\n" :
" //" + this.auxiliaryComment.commonjs2 + "\n" :
""
) +
" if(typeof exports === 'object' && typeof module === 'object')\n" +
" module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" +
(this.auxiliaryComment &&
typeof this.auxiliaryComment === "string" ?
" //" + this.auxiliaryComment + "\n" :
" //" + this.auxiliaryComment + "\n" :
this.auxiliaryComment.amd ?
" //" + this.auxiliaryComment.amd + "\n" :
" //" + this.auxiliaryComment.amd + "\n" :
""
) +
" else if(typeof define === 'function' && define.amd)\n" +
Expand All @@ -151,18 +152,18 @@ class UmdMainTemplatePlugin {
(this.names.root || this.names.commonjs ?
(this.auxiliaryComment &&
typeof this.auxiliaryComment === "string" ?
" //" + this.auxiliaryComment + "\n" :
" //" + this.auxiliaryComment + "\n" :
this.auxiliaryComment.commonjs ?
" //" + this.auxiliaryComment.commonjs + "\n" :
" //" + this.auxiliaryComment.commonjs + "\n" :
""
) +
" else if(typeof exports === 'object')\n" +
" exports[" + libraryName(this.names.commonjs || this.names.root) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
(this.auxiliaryComment &&
typeof this.auxiliaryComment === "string" ?
" //" + this.auxiliaryComment + "\n" :
" //" + this.auxiliaryComment + "\n" :
this.auxiliaryComment.root ?
" //" + this.auxiliaryComment.root + "\n" :
" //" + this.auxiliaryComment.root + "\n" :
""
) +
" else\n" +
Expand All @@ -175,7 +176,7 @@ class UmdMainTemplatePlugin {
" for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
" }\n"
) +
"})(typeof self !== 'undefined' ? self : this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
`})(${runtimeTemplate.outputOptions.globalObject}, function(${externalsArguments(externals)}) {\nreturn `, "webpack/universalModuleDefinition"), source, ";\n})");
};

for(const template of [mainTemplate, chunkTemplate]) {
Expand Down
2 changes: 0 additions & 2 deletions lib/WebpackOptionsApply.js
Expand Up @@ -123,8 +123,6 @@ class WebpackOptionsApply extends OptionsApply {
new ExternalsPlugin("commonjs", "nw.gui").apply(compiler);
new LoaderTargetPlugin(options.target).apply(compiler);
break;
case "atom":
case "electron":
case "electron-main":
NodeTemplatePlugin = require("./node/NodeTemplatePlugin");
NodeTargetPlugin = require("./node/NodeTargetPlugin");
Expand Down
21 changes: 20 additions & 1 deletion lib/WebpackOptionsDefaulter.js
Expand Up @@ -72,11 +72,30 @@ class WebpackOptionsDefaulter extends OptionsDefaulter {
this.set("output.webassemblyModuleFilename", "[modulehash].module.wasm");
this.set("output.library", "");
this.set("output.hotUpdateFunction", "make", (options) => {
return Template.toIdentifier("webpackHotUpdate" + options.output.library);
return Template.toIdentifier("webpackHotUpdate" + Template.toIdentifier(options.output.library));
});
this.set("output.jsonpFunction", "make", (options) => {
return Template.toIdentifier("webpackJsonp" + Template.toIdentifier(options.output.library));
});
this.set("output.chunkCallbackName", "make", (options) => {
return Template.toIdentifier("webpackChunk" + Template.toIdentifier(options.output.library));
});
this.set("output.globalObject", "make", options => {
switch(options.target) {
case "web":
case "electron-renderer":
return "window";
case "webworker":
return "self";
case "node":
case "async-node":
case "node-webkit":
case "electron-main":
return "global";
default:
return "self";
}
});
this.set("output.devtoolNamespace", "make", (options) => {
return options.output.library || "";
});
Expand Down
5 changes: 3 additions & 2 deletions lib/web/JsonpChunkTemplatePlugin.js
Expand Up @@ -10,8 +10,9 @@ class JsonpChunkTemplatePlugin {
apply(chunkTemplate) {
chunkTemplate.hooks.render.tap("JsonpChunkTemplatePlugin", (modules, chunk) => {
const jsonpFunction = chunkTemplate.outputOptions.jsonpFunction;
const globalObject = chunkTemplate.outputOptions.globalObject;
const source = new ConcatSource();
source.add(`(window[${JSON.stringify(jsonpFunction)}] = window[${JSON.stringify(jsonpFunction)}] || []).push([${JSON.stringify(chunk.ids)},`);
source.add(`(${globalObject}[${JSON.stringify(jsonpFunction)}] = ${globalObject}[${JSON.stringify(jsonpFunction)}] || []).push([${JSON.stringify(chunk.ids)},`);
source.add(modules);
const entries = [chunk.entryModule]
.filter(Boolean)
Expand All @@ -26,7 +27,7 @@ class JsonpChunkTemplatePlugin {
hash.update("JsonpChunkTemplatePlugin");
hash.update("4");
hash.update(`${chunkTemplate.outputOptions.jsonpFunction}`);
hash.update(`${chunkTemplate.outputOptions.library}`);
hash.update(`${chunkTemplate.outputOptions.globalObject}`);
});
}
}
Expand Down
10 changes: 6 additions & 4 deletions lib/web/JsonpMainTemplatePlugin.js
Expand Up @@ -208,8 +208,9 @@ class JsonpMainTemplatePlugin {
mainTemplate.hooks.startup.tap("JsonpMainTemplatePlugin", (source, chunk, hash) => {
if(needChunkLoadingCode(chunk)) {
var jsonpFunction = mainTemplate.outputOptions.jsonpFunction;
var globalObject = mainTemplate.outputOptions.globalObject;
return Template.asString([
`var jsonpArray = window[${JSON.stringify(jsonpFunction)}] = window[${JSON.stringify(jsonpFunction)}] || [];`,
`var jsonpArray = ${globalObject}[${JSON.stringify(jsonpFunction)}] = ${globalObject}[${JSON.stringify(jsonpFunction)}] || [];`,
"var parentJsonpFunction = jsonpArray.push.bind(jsonpArray);",
"jsonpArray.push = webpackJsonpCallback;",
"jsonpArray = jsonpArray.slice();",
Expand All @@ -221,6 +222,7 @@ class JsonpMainTemplatePlugin {
return source;
});
mainTemplate.hooks.hotBootstrap.tap("JsonpMainTemplatePlugin", (source, chunk, hash) => {
const globalObject = mainTemplate.outputOptions.globalObject;
const hotUpdateChunkFilename = mainTemplate.outputOptions.hotUpdateChunkFilename;
const hotUpdateMainFilename = mainTemplate.outputOptions.hotUpdateMainFilename;
const crossOriginLoading = mainTemplate.outputOptions.crossOriginLoading;
Expand All @@ -247,13 +249,13 @@ class JsonpMainTemplatePlugin {
function hotDisposeChunk(chunkId) {
delete installedChunks[chunkId];
}
var parentHotUpdateCallback = window[${JSON.stringify(hotUpdateFunction)}];
window[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
var parentHotUpdateCallback = ${globalObject}[${JSON.stringify(hotUpdateFunction)}];
${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
});
mainTemplate.hooks.hash.tap("JsonpMainTemplatePlugin", hash => {
hash.update("jsonp");
hash.update("5");
hash.update(`${mainTemplate.outputOptions.filename}`);
hash.update(`${mainTemplate.outputOptions.globalObject}`);
hash.update(`${mainTemplate.outputOptions.chunkFilename}`);
hash.update(`${mainTemplate.outputOptions.jsonpFunction}`);
hash.update(`${mainTemplate.outputOptions.hotUpdateFunction}`);
Expand Down
8 changes: 4 additions & 4 deletions lib/webworker/WebWorkerChunkTemplatePlugin.js
Expand Up @@ -5,15 +5,15 @@
"use strict";

const ConcatSource = require("webpack-sources").ConcatSource;
const Template = require("../Template");

class WebWorkerChunkTemplatePlugin {

apply(chunkTemplate) {
chunkTemplate.hooks.render.tap("WebWorkerChunkTemplatePlugin", (modules, chunk) => {
const chunkCallbackName = chunkTemplate.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (chunkTemplate.outputOptions.library || ""));
const chunkCallbackName = chunkTemplate.outputOptions.chunkCallbackName;
const globalObject = chunkTemplate.outputOptions.globalObject;
const source = new ConcatSource();
source.add(`self[${JSON.stringify(chunkCallbackName)}](${JSON.stringify(chunk.ids)},`);
source.add(`${globalObject}[${JSON.stringify(chunkCallbackName)}](${JSON.stringify(chunk.ids)},`);
source.add(modules);
source.add(")");
return source;
Expand All @@ -22,7 +22,7 @@ class WebWorkerChunkTemplatePlugin {
hash.update("webworker");
hash.update("3");
hash.update(`${chunkTemplate.outputOptions.chunkCallbackName}`);
hash.update(`${chunkTemplate.outputOptions.library}`);
hash.update(`${chunkTemplate.outputOptions.globalObject}`);
});
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/webworker/WebWorkerHotUpdateChunkTemplatePlugin.js
Expand Up @@ -4,15 +4,15 @@
*/
"use strict";
const ConcatSource = require("webpack-sources").ConcatSource;
const Template = require("../Template");

class WebWorkerHotUpdateChunkTemplatePlugin {

apply(hotUpdateChunkTemplate) {
hotUpdateChunkTemplate.hooks.render.tap("WebWorkerHotUpdateChunkTemplatePlugin", (modulesSource, modules, removedModules, hash, id) => {
const chunkCallbackName = hotUpdateChunkTemplate.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (hotUpdateChunkTemplate.outputOptions.library || ""));
const hotUpdateFunction = hotUpdateChunkTemplate.outputOptions.hotUpdateFunction;
const globalObject = hotUpdateChunkTemplate.outputOptions.globalObject;
const source = new ConcatSource();
source.add(chunkCallbackName + "(" + JSON.stringify(id) + ",");
source.add(`${globalObject}[${JSON.stringify(hotUpdateFunction)}](${JSON.stringify(id)},`);
source.add(modulesSource);
source.add(")");
return source;
Expand All @@ -21,7 +21,7 @@ class WebWorkerHotUpdateChunkTemplatePlugin {
hash.update("WebWorkerHotUpdateChunkTemplatePlugin");
hash.update("3");
hash.update(hotUpdateChunkTemplate.outputOptions.hotUpdateFunction + "");
hash.update(hotUpdateChunkTemplate.outputOptions.library + "");
hash.update(hotUpdateChunkTemplate.outputOptions.globalObject + "");
});
}
}
Expand Down
14 changes: 8 additions & 6 deletions lib/webworker/WebWorkerMainTemplatePlugin.js
Expand Up @@ -48,10 +48,11 @@ class WebWorkerMainTemplatePlugin {
});
mainTemplate.hooks.bootstrap.tap("WebWorkerMainTemplatePlugin", (source, chunk, hash) => {
if(chunk.getNumberOfChunks() > 0) {
const chunkCallbackName = mainTemplate.outputOptions.chunkCallbackName || Template.toIdentifier("webpackChunk" + (mainTemplate.outputOptions.library || ""));
const chunkCallbackName = mainTemplate.outputOptions.chunkCallbackName;
const globalObject = mainTemplate.outputOptions.globalObject;
return Template.asString([
source,
`self[${JSON.stringify(chunkCallbackName)}] = function webpackChunkCallback(chunkIds, moreModules) {`,
`${globalObject}[${JSON.stringify(chunkCallbackName)}] = function webpackChunkCallback(chunkIds, moreModules) {`,
Template.indent([
"for(var moduleId in moreModules) {",
Template.indent(mainTemplate.renderAddModule(hash, chunk, "moduleId", "moreModules[moduleId]")),
Expand All @@ -67,7 +68,8 @@ class WebWorkerMainTemplatePlugin {
mainTemplate.hooks.hotBootstrap.tap("WebWorkerMainTemplatePlugin", (source, chunk, hash) => {
const hotUpdateChunkFilename = mainTemplate.outputOptions.hotUpdateChunkFilename;
const hotUpdateMainFilename = mainTemplate.outputOptions.hotUpdateMainFilename;
const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction || Template.toIdentifier("webpackHotUpdate" + (mainTemplate.outputOptions.library || ""));
const hotUpdateFunction = mainTemplate.outputOptions.hotUpdateFunction;
const globalObject = mainTemplate.outputOptions.globalObject;
const currentHotUpdateChunkFilename = mainTemplate.getAssetPath(JSON.stringify(hotUpdateChunkFilename), {
hash: `" + ${mainTemplate.renderCurrentHashCode(hash)} + "`,
hashWithLength: (length) => `" + ${mainTemplate.renderCurrentHashCode(hash, length)} + "`,
Expand All @@ -81,8 +83,8 @@ class WebWorkerMainTemplatePlugin {
});

return source + "\n" +
`var parentHotUpdateCallback = self[${JSON.stringify(hotUpdateFunction)}];\n` +
`self[${JSON.stringify(hotUpdateFunction)}] = ` +
`var parentHotUpdateCallback = ${globalObject}[${JSON.stringify(hotUpdateFunction)}];\n` +
`${globalObject}[${JSON.stringify(hotUpdateFunction)}] = ` +
Template.getFunctionContent(require("./WebWorkerMainTemplate.runtime.js"))
.replace(/\/\/\$semicolon/g, ";")
.replace(/\$require\$/g, mainTemplate.requireFn)
Expand All @@ -97,7 +99,7 @@ class WebWorkerMainTemplatePlugin {
hash.update(`${mainTemplate.outputOptions.filename}`);
hash.update(`${mainTemplate.outputOptions.chunkFilename}`);
hash.update(`${mainTemplate.outputOptions.chunkCallbackName}`);
hash.update(`${mainTemplate.outputOptions.library}`);
hash.update(`${mainTemplate.outputOptions.globalObject}`);
});
}
}
Expand Down
3 changes: 1 addition & 2 deletions schemas/WebpackOptions.json
Expand Up @@ -480,6 +480,7 @@
"assign",
"this",
"window",
"self",
"global",
"commonjs",
"commonjs2",
Expand Down Expand Up @@ -1649,8 +1650,6 @@
"node",
"async-node",
"node-webkit",
"atom",
"electron",
"electron-main",
"electron-renderer"
]
Expand Down

0 comments on commit d95f93f

Please sign in to comment.