Skip to content

Commit

Permalink
Merge pull request #28 from chuckdumont/work
Browse files Browse the repository at this point in the history
Another try at fixing Issue #23
  • Loading branch information
chuckdumont authored Jul 20, 2017
2 parents 69f510a + ffd165e commit 2ea9322
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 48 deletions.
10 changes: 0 additions & 10 deletions lib/DojoAMDDefineDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion lib/DojoAMDMainTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions lib/DojoAMDPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down
82 changes: 48 additions & 34 deletions lib/DojoLoaderEnsurePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
});
});
});
}
};

0 comments on commit 2ea9322

Please sign in to comment.