Skip to content

Commit 0f9ac60

Browse files
author
Chuck Dumont
authored
Merge pull request #25 from chuckdumont/revs/heads/work2
Ensure Dojo loader module isn't removed from entry chunks by chunk optimizers
2 parents 8e8cc31 + e6d7b37 commit 0f9ac60

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

lib/DojoAMDMainTemplatePlugin.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,14 @@ module.exports = class DojoAMDMainTemplatePlugin {
122122
}
123123
}
124124

125+
function hasAMD(chunk) {
126+
return chunk.getModules().some((module) => {
127+
return module.isAMD;
128+
});
129+
}
130+
125131
compilation.mainTemplate.plugin("bootstrap", function(source, chunk) {
126-
if (!compilation.dojoLoaderDependenciesAdded) {
132+
if (!compilation.dojoLoaderDependenciesAdded || !hasAMD(chunk)) {
127133
return source;
128134
}
129135
const buf = [];
@@ -186,15 +192,15 @@ module.exports = class DojoAMDMainTemplatePlugin {
186192
return this.asString(buf);
187193
});
188194

189-
compilation.mainTemplate.plugin("module-obj", function(source) {
190-
if (compilation.dojoLoaderDependenciesAdded) {
195+
compilation.mainTemplate.plugin("module-obj", function(source, chunk) {
196+
if (compilation.dojoLoaderDependenciesAdded && hasAMD(chunk)) {
191197
source = source.replace("i: moduleId,", "i: req.absMidsById[moduleId] || moduleId,");
192198
}
193199
return source;
194200
});
195201

196-
compilation.mainTemplate.plugin("require-extensions", function(source) {
197-
if (!compilation.dojoLoaderDependenciesAdded) {
202+
compilation.mainTemplate.plugin("require-extensions", function(source, chunk) {
203+
if (!compilation.dojoLoaderDependenciesAdded || !hasAMD(chunk)) {
198204
return source;
199205
}
200206
const buf = [];
@@ -208,14 +214,13 @@ module.exports = class DojoAMDMainTemplatePlugin {
208214
buf.push("\tg: (function(){return this;})() // Easy access to global scope");
209215
buf.push("};");
210216
buf.push("var loaderScope = {document:document};");
211-
const modules = compilation.getModules ? compilation.getModules() : compilation.modules;
212-
const dojoLoaderModule = modules.find((module) => { return module.rawRequest === options.loader;});
217+
const dojoLoaderModule = compilation.modules.find((module) => { return module.rawRequest === options.loader;});
213218
if (!dojoLoaderModule) {
214219
throw Error("Can't locate " + options.loader + " in compilation");
215220
}
216221
var s = "var loaderConfig = ";
217222
if (util.isString(options.loaderConfig)) {
218-
const dojoLoaderConfig = modules.find((module) => { return module.rawRequest === options.loaderConfig;});
223+
const dojoLoaderConfig = compilation.modules.find((module) => { return module.rawRequest === options.loaderConfig;});
219224
s += this.requireFn + "(" + JSON.stringify(dojoLoaderConfig.id) + ");";
220225
} else {
221226
s += JSON.stringify(options.loaderConfig);

lib/DojoAMDPlugin.js

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const DojoAMDMainTemplatePlugin = require("./DojoAMDMainTemplatePlugin");
2828
const DojoAMDChunkTemplatePlugin = require("./DojoAMDChunkTemplatePlugin");
2929
const DojoAMDResolverPlugin = require("./DojoAMDResolverPlugin");
3030
const DojoAMDModuleFactoryPlugin = require("./DojoAMDModuleFactoryPlugin");
31+
const DojoLoaderEnsurePlugin = require("./DojoLoaderEnsurePlugin");
3132

3233
module.exports = class DojoAMDPlugin {
3334
constructor(options) {
@@ -87,6 +88,7 @@ module.exports = class DojoAMDPlugin {
8788

8889
compilation.apply(new DojoAMDMainTemplatePlugin(this.options));
8990
compilation.apply(new DojoAMDChunkTemplatePlugin(this.options));
91+
compilation.apply(new DojoLoaderEnsurePlugin(this.options));
9092

9193
params.normalModuleFactory.plugin("parser", (parser) => {
9294
parser.plugin("expression module", () => {

lib/DojoLoaderEnsurePlugin.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* (C) Copyright IBM Corp. 2012, 2016 All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
const util = require('util');
17+
18+
function hasAMD(chunk) {
19+
return chunk.getModules().some((module) => {
20+
return module.isAMD;
21+
});
22+
}
23+
24+
module.exports = class DojoLoaderEnsurePlugin {
25+
constructor(options) {
26+
this.options = options;
27+
}
28+
apply(compilation) {
29+
// Ensure that the Dojo loader, and optionally the loader config, are included
30+
// in each entry chunk that has any AMD modules.
31+
compilation.plugin("after-optimize-chunks", (chunks) => {
32+
debugger;
33+
if (!compilation.dojoLoaderDependenciesAdded) {
34+
return; // Nothing to do for this compilation
35+
}
36+
// Get the loader and loader config
37+
const loaderModule = compilation.modules.find((module) => { return module.rawRequest === this.options.loader;});
38+
if (!loaderModule) {
39+
throw Error("Can't locate " + this.options.loader + " in compilation");
40+
}
41+
let configModule;
42+
if (util.isString(this.options.loaderConfig)) {
43+
configModule = compilation.modules.find((module) => { return module.rawRequest === this.options.loaderConfig;});
44+
if (!configModule) {
45+
throw Error("Can't locate " + this.options.loaderConfig + " in compilation");
46+
}
47+
}
48+
chunks.forEach((chunk) => {
49+
if (chunk.hasRuntime() && hasAMD(chunk)) {
50+
if (!chunk.containsModule(loaderModule)) {
51+
chunk.addModule(loaderModule);
52+
loaderModule.addChunk(chunk);
53+
}
54+
if (configModule && !chunk.containsModule(configModule)) {
55+
chunk.addModule(configModule);
56+
configModule.addChunk(chunk);
57+
}
58+
}
59+
});
60+
});
61+
}
62+
};

0 commit comments

Comments
 (0)