Skip to content

Commit 2ea9322

Browse files
author
Chuck Dumont
authored
Merge pull request #28 from chuckdumont/work
Another try at fixing Issue #23
2 parents 69f510a + ffd165e commit 2ea9322

4 files changed

+53
-48
lines changed

lib/DojoAMDDefineDependencyParserPlugin.js

-10
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
MIT License http://www.opensource.org/licenses/mit-license.php
1818
Author Tobias Koppers @sokra
1919
*/
20-
const util = require("util");
2120
const DojoAMDRequireItemDependency = require("./DojoAMDRequireItemDependency");
2221
const DojoAMDRequireArrayDependency = require("./DojoAMDRequireArrayDependency");
23-
const CommonJsRequireDependency = require("webpack/lib/dependencies/CommonJsRequireDependency");
2422
const ConstDependency = require("webpack/lib/dependencies/ConstDependency");
2523
const DojoAMDDefineDependency = require("./DojoAMDDefineDependency");
2624
const LocalModuleDependency = require("webpack/lib/dependencies/LocalModuleDependency");
@@ -37,14 +35,6 @@ module.exports = class DojoAMDDefineDependencyParserPlugin {
3735
parser.plugin("call define", (expr) => {
3836
if (expr.dojoSkipFlag) return;
3937
expr.dojoSkipFlag = true;
40-
41-
if (!parser.state.compilation.dojoLoaderDependenciesAdded) {
42-
parser.state.current.addDependency(new CommonJsRequireDependency(this.options.loader));
43-
if (util.isString(this.options.loaderConfig)) {
44-
parser.state.current.addDependency(new CommonJsRequireDependency(this.options.loaderConfig));
45-
}
46-
parser.state.compilation.dojoLoaderDependenciesAdded = true;
47-
}
4838
parser.state.current.isAMD = true;
4939
const result = parser.applyPluginsBailResult("call define", expr);
5040
delete expr.dojoSkipFlag;

lib/DojoAMDMainTemplatePlugin.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ module.exports = class DojoAMDMainTemplatePlugin {
198198
buf.push("\th: resolveTernaryHasExpression,");
199199
buf.push("\tg: (function(){return this;})() // Easy access to global scope");
200200
buf.push("};");
201-
buf.push("var loaderScope = {document:document};");
201+
buf.push("var globalScope = (function(){return this;})();");
202+
buf.push("var loaderScope = {document:globalScope.document};");
202203
const dojoLoaderModule = compilation.modules.find((module) => { return module.rawRequest === options.loader;});
203204
if (!dojoLoaderModule) {
204205
throw Error("Can't locate " + options.loader + " in compilation");

lib/DojoAMDPlugin.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ module.exports = class DojoAMDPlugin {
8888

8989
compilation.apply(new DojoAMDMainTemplatePlugin(this.options));
9090
compilation.apply(new DojoAMDChunkTemplatePlugin(this.options));
91-
compilation.apply(new DojoLoaderEnsurePlugin(this.options));
9291

9392
params.normalModuleFactory.plugin("parser", (parser) => {
9493
parser.plugin("expression module", () => {
@@ -116,7 +115,8 @@ module.exports = class DojoAMDPlugin {
116115
});
117116

118117
compiler.apply(
119-
new DojoAMDModuleFactoryPlugin(this.options, reqWrapper)
118+
new DojoAMDModuleFactoryPlugin(this.options, reqWrapper),
119+
new DojoLoaderEnsurePlugin(this.options)
120120
);
121121

122122
compiler.plugin("compilation", (__, params) => {
@@ -142,7 +142,7 @@ module.exports = class DojoAMDPlugin {
142142
const resolveLoader = compiler.options.resolveLoader = compiler.options.resolveLoader || {};
143143
const modules = resolveLoader.modules = resolveLoader.modules || [];
144144
modules.push(path.join(__dirname, "..", "loaders"));
145-
}
145+
}
146146

147147
getDojoLoader(options__, loaderConfig, callback) {
148148
var dojoLoader;

lib/DojoLoaderEnsurePlugin.js

+48-34
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
const util = require('util');
17+
const CommonJsRequireDependency = require("webpack/lib/dependencies/CommonJsRequireDependency");
1718

1819
function containsModule(chunk, module) {
1920
if (chunk.containsModule) {
@@ -27,43 +28,56 @@ module.exports = class DojoLoaderEnsurePlugin {
2728
constructor(options) {
2829
this.options = options;
2930
}
30-
apply(compilation) {
31-
// Ensure that the Dojo loader, and optionally the loader config, are included
32-
// in the entry chunks, and only the entry chunks.
33-
compilation.plugin("after-optimize-chunks", (chunks) => {
34-
// Get the loader and loader config
35-
const loaderModule = compilation.modules.find((module) => { return module.rawRequest === this.options.loader;});
36-
if (!loaderModule) {
37-
throw Error("Can't locate " + this.options.loader + " in compilation");
38-
}
39-
let configModule;
40-
if (util.isString(this.options.loaderConfig)) {
41-
configModule = compilation.modules.find((module) => { return module.rawRequest === this.options.loaderConfig;});
42-
if (!configModule) {
43-
throw Error("Can't locate " + this.options.loaderConfig + " in compilation");
44-
}
45-
}
46-
chunks.forEach((chunk) => {
47-
if (chunk.hasRuntime()) {
48-
if (!containsModule(chunk, loaderModule)) {
49-
chunk.addModule(loaderModule);
50-
loaderModule.addChunk(chunk);
51-
}
52-
if (configModule && !containsModule(chunk, configModule)) {
53-
chunk.addModule(configModule);
54-
configModule.addChunk(chunk);
55-
}
56-
} else {
57-
if (containsModule(chunk, loaderModule)) {
58-
chunk.removeModule(loaderModule);
59-
loaderModule.removeChunk(chunk);
60-
}
61-
if (configModule && containsModule(chunk, configModule)) {
62-
chunk.removeModule(configModule);
63-
configModule.removeChunk(chunk);
31+
apply(compiler) {
32+
compiler.plugin("compilation", (compilation) => {
33+
compilation.plugin("succeed-module", (module) => {
34+
if (!module.issuer) {
35+
// No issuer generally means an entry module, so add a Dojo loader dependency. It doesn't
36+
// hurt to add extra dependencies because the Dojo loader module will be removed from chunks
37+
// that don't need it in the 'after-optimize-chunks' handler below.
38+
module.addDependency(new CommonJsRequireDependency(this.options.loader));
39+
if (util.isString(this.options.loaderConfig)) {
40+
module.addDependency(new CommonJsRequireDependency(this.options.loaderConfig));
6441
}
6542
}
6643
});
44+
45+
compilation.plugin("after-optimize-chunks", (chunks) => {
46+
// Get the loader and loader config
47+
const loaderModule = compilation.modules.find((module) => { return module.rawRequest === this.options.loader;});
48+
const configModule = util.isString(this.options.loaderConfig) &&
49+
compilation.modules.find((module) => { return module.rawRequest === this.options.loaderConfig;});
50+
51+
// Ensure that the Dojo loader, and optionally the loader config, are included
52+
// only in the entry chunks that contain the webpack runtime.
53+
chunks.forEach((chunk) => {
54+
if (chunk.hasRuntime()) {
55+
if (!loaderModule) {
56+
throw Error("Can't locate " + this.options.loader + " in compilation");
57+
}
58+
if (util.isString(this.options.loaderConfig) && !configModule) {
59+
throw Error("Can't locate " + this.options.loaderConfig + " in compilation");
60+
}
61+
if (!containsModule(chunk, loaderModule)) {
62+
chunk.addModule(loaderModule);
63+
loaderModule.addChunk(chunk);
64+
}
65+
if (configModule && !containsModule(chunk, configModule)) {
66+
chunk.addModule(configModule);
67+
configModule.addChunk(chunk);
68+
}
69+
} else if (loaderModule) {
70+
if (containsModule(chunk, loaderModule)) {
71+
chunk.removeModule(loaderModule);
72+
loaderModule.removeChunk(chunk);
73+
}
74+
if (configModule && containsModule(chunk, configModule)) {
75+
chunk.removeModule(configModule);
76+
configModule.removeChunk(chunk);
77+
}
78+
}
79+
});
80+
});
6781
});
6882
}
6983
};

0 commit comments

Comments
 (0)