diff --git a/lib/DojoAMDDefineDependencyParserPlugin.js b/lib/DojoAMDDefineDependencyParserPlugin.js index 6015a29..920b66d 100644 --- a/lib/DojoAMDDefineDependencyParserPlugin.js +++ b/lib/DojoAMDDefineDependencyParserPlugin.js @@ -17,10 +17,8 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -const util = require("util"); const DojoAMDRequireItemDependency = require("./DojoAMDRequireItemDependency"); const DojoAMDRequireArrayDependency = require("./DojoAMDRequireArrayDependency"); -const CommonJsRequireDependency = require("webpack/lib/dependencies/CommonJsRequireDependency"); const ConstDependency = require("webpack/lib/dependencies/ConstDependency"); const DojoAMDDefineDependency = require("./DojoAMDDefineDependency"); const LocalModuleDependency = require("webpack/lib/dependencies/LocalModuleDependency"); @@ -37,14 +35,6 @@ module.exports = class DojoAMDDefineDependencyParserPlugin { parser.plugin("call define", (expr) => { if (expr.dojoSkipFlag) return; expr.dojoSkipFlag = true; - - if (!parser.state.compilation.dojoLoaderDependenciesAdded) { - parser.state.current.addDependency(new CommonJsRequireDependency(this.options.loader)); - if (util.isString(this.options.loaderConfig)) { - parser.state.current.addDependency(new CommonJsRequireDependency(this.options.loaderConfig)); - } - parser.state.compilation.dojoLoaderDependenciesAdded = true; - } parser.state.current.isAMD = true; const result = parser.applyPluginsBailResult("call define", expr); delete expr.dojoSkipFlag; diff --git a/lib/DojoAMDMainTemplatePlugin.js b/lib/DojoAMDMainTemplatePlugin.js index b27ee8f..0993aa6 100644 --- a/lib/DojoAMDMainTemplatePlugin.js +++ b/lib/DojoAMDMainTemplatePlugin.js @@ -198,7 +198,8 @@ module.exports = class DojoAMDMainTemplatePlugin { buf.push("\th: resolveTernaryHasExpression,"); buf.push("\tg: (function(){return this;})() // Easy access to global scope"); buf.push("};"); - buf.push("var loaderScope = {document:document};"); + buf.push("var globalScope = (function(){return this;})();"); + buf.push("var loaderScope = {document:globalScope.document};"); const dojoLoaderModule = compilation.modules.find((module) => { return module.rawRequest === options.loader;}); if (!dojoLoaderModule) { throw Error("Can't locate " + options.loader + " in compilation"); diff --git a/lib/DojoAMDPlugin.js b/lib/DojoAMDPlugin.js index 36e44b8..5e4607c 100644 --- a/lib/DojoAMDPlugin.js +++ b/lib/DojoAMDPlugin.js @@ -88,7 +88,6 @@ module.exports = class DojoAMDPlugin { compilation.apply(new DojoAMDMainTemplatePlugin(this.options)); compilation.apply(new DojoAMDChunkTemplatePlugin(this.options)); - compilation.apply(new DojoLoaderEnsurePlugin(this.options)); params.normalModuleFactory.plugin("parser", (parser) => { parser.plugin("expression module", () => { @@ -116,7 +115,8 @@ module.exports = class DojoAMDPlugin { }); compiler.apply( - new DojoAMDModuleFactoryPlugin(this.options, reqWrapper) + new DojoAMDModuleFactoryPlugin(this.options, reqWrapper), + new DojoLoaderEnsurePlugin(this.options) ); compiler.plugin("compilation", (__, params) => { @@ -142,7 +142,7 @@ module.exports = class DojoAMDPlugin { const resolveLoader = compiler.options.resolveLoader = compiler.options.resolveLoader || {}; const modules = resolveLoader.modules = resolveLoader.modules || []; modules.push(path.join(__dirname, "..", "loaders")); - } + } getDojoLoader(options__, loaderConfig, callback) { var dojoLoader; diff --git a/lib/DojoLoaderEnsurePlugin.js b/lib/DojoLoaderEnsurePlugin.js index 934c7cc..4af845c 100644 --- a/lib/DojoLoaderEnsurePlugin.js +++ b/lib/DojoLoaderEnsurePlugin.js @@ -14,6 +14,7 @@ * limitations under the License. */ const util = require('util'); + const CommonJsRequireDependency = require("webpack/lib/dependencies/CommonJsRequireDependency"); function containsModule(chunk, module) { if (chunk.containsModule) { @@ -27,43 +28,56 @@ module.exports = class DojoLoaderEnsurePlugin { constructor(options) { this.options = options; } - apply(compilation) { - // Ensure that the Dojo loader, and optionally the loader config, are included - // in the entry chunks, and only the entry chunks. - compilation.plugin("after-optimize-chunks", (chunks) => { - // Get the loader and loader config - const loaderModule = compilation.modules.find((module) => { return module.rawRequest === this.options.loader;}); - if (!loaderModule) { - throw Error("Can't locate " + this.options.loader + " in compilation"); - } - let configModule; - if (util.isString(this.options.loaderConfig)) { - configModule = compilation.modules.find((module) => { return module.rawRequest === this.options.loaderConfig;}); - if (!configModule) { - throw Error("Can't locate " + this.options.loaderConfig + " in compilation"); - } - } - chunks.forEach((chunk) => { - if (chunk.hasRuntime()) { - if (!containsModule(chunk, loaderModule)) { - chunk.addModule(loaderModule); - loaderModule.addChunk(chunk); - } - if (configModule && !containsModule(chunk, configModule)) { - chunk.addModule(configModule); - configModule.addChunk(chunk); - } - } else { - if (containsModule(chunk, loaderModule)) { - chunk.removeModule(loaderModule); - loaderModule.removeChunk(chunk); - } - if (configModule && containsModule(chunk, configModule)) { - chunk.removeModule(configModule); - configModule.removeChunk(chunk); + apply(compiler) { + compiler.plugin("compilation", (compilation) => { + compilation.plugin("succeed-module", (module) => { + if (!module.issuer) { + // No issuer generally means an entry module, so add a Dojo loader dependency. It doesn't + // hurt to add extra dependencies because the Dojo loader module will be removed from chunks + // that don't need it in the 'after-optimize-chunks' handler below. + module.addDependency(new CommonJsRequireDependency(this.options.loader)); + if (util.isString(this.options.loaderConfig)) { + module.addDependency(new CommonJsRequireDependency(this.options.loaderConfig)); } } }); + + compilation.plugin("after-optimize-chunks", (chunks) => { + // Get the loader and loader config + const loaderModule = compilation.modules.find((module) => { return module.rawRequest === this.options.loader;}); + const configModule = util.isString(this.options.loaderConfig) && + compilation.modules.find((module) => { return module.rawRequest === this.options.loaderConfig;}); + + // Ensure that the Dojo loader, and optionally the loader config, are included + // only in the entry chunks that contain the webpack runtime. + chunks.forEach((chunk) => { + if (chunk.hasRuntime()) { + if (!loaderModule) { + throw Error("Can't locate " + this.options.loader + " in compilation"); + } + if (util.isString(this.options.loaderConfig) && !configModule) { + throw Error("Can't locate " + this.options.loaderConfig + " in compilation"); + } + if (!containsModule(chunk, loaderModule)) { + chunk.addModule(loaderModule); + loaderModule.addChunk(chunk); + } + if (configModule && !containsModule(chunk, configModule)) { + chunk.addModule(configModule); + configModule.addChunk(chunk); + } + } else if (loaderModule) { + if (containsModule(chunk, loaderModule)) { + chunk.removeModule(loaderModule); + loaderModule.removeChunk(chunk); + } + if (configModule && containsModule(chunk, configModule)) { + chunk.removeModule(configModule); + configModule.removeChunk(chunk); + } + } + }); + }); }); } };