Skip to content

Commit

Permalink
Merge pull request #50 from chuckdumont/work2
Browse files Browse the repository at this point in the history
Fix build break caused by loader changes in Dojo
  • Loading branch information
chuckdumont authored Oct 4, 2017
2 parents ea4e71e + 27b14c2 commit 1f2c45d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Introduction

**dojo-webpack-plugin** is a [Webpack](https://webpack.github.io/) plugin that supports using Webpack to build Dojo 1.x applications that use Asyncronous Module Definition (AMD). This version supports Webpack 2 and greater. The plugin has been tested with Webpack 2.2.0 and 3.0.0, and Dojo versions 1.10 and 1.12. For Webpack 1.x, use the v1 branch of this project. Features include:
**dojo-webpack-plugin** is a [Webpack](https://webpack.github.io/) plugin that supports using Webpack to build Dojo 1.x applications that use Asyncronous Module Definition (AMD). This version supports Webpack 2 and greater. The plugin has been tested with Webpack 2.2.0 and 3.0.0, and Dojo versions 1.10 through 1.13. For Webpack 1.x, use the v1 branch of this project. Features include:

* Support for Dojo loader config properties, including `baseUrl`, `paths`, `packages`, `map` and `aliases`
* Support for client-side synchronous and asynchronous `require()` calls for packed modules.
Expand Down Expand Up @@ -235,3 +235,14 @@ The best way to ensure that the requirement is met is to make sure that both thi
See the sample application at https://github.com/OpenNTF/dojo-webpack-plugin-sample.

https://openntf.github.io/dojo-webpack-plugin-sample/test.html.

# Release Notes

The versions of Dojo listed below require version 2.1.0 of this plugin to work correctly. Attempting to use earlier versions of this plugin with the listed versions of Dojo will result in the error "Dojo require not yet initialized" when building.

* 1.13.0 and later
* 1.12.3 and later
* 1.11.5 and later
* 1.10.9 and later

In addition, Dojo loaders built with earlier versions of the plugin will not work with 2.1.0 or later, even if you have not changed the version of Dojo you are building with. If you are using a pre-built loader with the [loader](#loader) config option, then you will need to rebuild it when upgrading to 2.1.
8 changes: 5 additions & 3 deletions buildDojo/transforms/writeDojo.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ define([
"util/build/buildControl",
"util/build/fileUtils",
"util/build/fs",
"util/build/transforms/writeAmd"
"util/build/transforms/writeAmd",
"dojo/text!dojo/package.json"

], function(bc, fileUtils, fs, writeAmd){
], function(bc, fileUtils, fs, writeAmd, pkg){
return function(resource, callback){
var
waitCount = 1, // matches *1*
Expand All @@ -51,8 +52,9 @@ define([

// the writeDojo transform...
try{
const version = JSON.stringify(JSON.parse(pkg).version);
// assemble and write the dojo layer
resource.uncompressedText = "module.exports = " + resource.getText() + ";";
resource.uncompressedText = "module.exports = function(userConfig, defaultConfig, global, window) { this.loaderVersion = " + version + "; " + resource.getText() + ".call(this, userConfig, defaultConfig);};";
doWrite(writeAmd.getDestFilename(resource), resource.uncompressedText);

onWriteComplete(0); // matches *1*
Expand Down
3 changes: 2 additions & 1 deletion lib/DojoAMDMainTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ module.exports = class DojoAMDMainTemplatePlugin {
buf.push("};");
buf.push("var globalScope = (function(){return this;})();");
buf.push("var loaderScope = {document:globalScope.document};");
buf.push("loaderScope.global = loaderScope.window = loaderScope;");
const dojoLoaderModule = compilation.modules.find((module) => { return module.rawRequest === options.loader;});
if (!dojoLoaderModule) {
throw Error("Can't locate " + options.loader + " in compilation");
Expand All @@ -134,7 +135,7 @@ module.exports = class DojoAMDMainTemplatePlugin {
buf.push("if (!loaderConfig.has) loaderConfig.has = {};");
buf.push("if (!('webpack' in loaderConfig.has)) loaderConfig.has.webpack = true;");
buf.push("var dojoLoader = " + this.requireFn + "(" + JSON.stringify(dojoLoaderModule.id) + ");");
buf.push("dojoLoader.call(loaderScope, loaderConfig, {hasCache:" + JSON.stringify(require("./defaultFeatures")) + "});");
buf.push("dojoLoader.call(loaderScope, loaderConfig, {hasCache:" + JSON.stringify(require("./defaultFeatures")) + "}, loaderScope, loaderScope);");
buf.push("req.has = loaderScope.require.has;");
buf.push("req.rawConfig = loaderScope.require.rawConfig");
buf.push("");
Expand Down
26 changes: 23 additions & 3 deletions lib/DojoAMDPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module.exports = class DojoAMDPlugin {

apply(compiler) {
const loaderScope = {};
loaderScope.global = loaderScope.window = loaderScope;

// Wrapper for Dojo require since we need to pass require to plugin constructors
// before the actual require function is available.
Expand Down Expand Up @@ -75,9 +76,7 @@ module.exports = class DojoAMDPlugin {
if (!loaderScope.require) {
this.getDojoLoader(this.options, loaderConfig, (err, dojoLoader) => {
if (!err) {
dojoLoader.call(loaderScope, loaderConfig, {hasCache:{'dojo-config-api':1, 'dojo-inject-api':1, 'host-node':0, 'dom':0, 'dojo-sniff':0}});
} else {
console.error(err);
dojoLoader.call(loaderScope, loaderConfig, {hasCache:{'dojo-config-api':1, 'dojo-inject-api':1, 'host-node':0, 'dom':0, 'dojo-sniff':0}}, loaderScope, loaderScope);
}
callback(err);
});
Expand Down Expand Up @@ -109,6 +108,27 @@ module.exports = class DojoAMDPlugin {
}
});
});

compiler.plugin("make", (compilation__, callback) => {
// Vefiry that loader version and dojo version are the same
params.normalModuleFactory.create({
dependencies: [{request: "dojo/package.json"}]
}, (err, module) => {
if (!err) {
const dojoVersion = require(module.request).version;
if (dojoVersion !== loaderScope.loaderVersion) {
err = new Error(
`Dojo loader version does not match the version of Dojo.
Loader version = ${loaderScope.loaderVersion}.
Dojo version = ${dojoVersion}.
You may need to rebuild the Dojo loader.
Refer to https://github.com/OpenNTF/dojo-webpack-plugin/blob/master/README.md#building-the-dojo-loader`);
}
return callback(err);
}
callback();
});
});
});

compiler.plugin("normal-module-factory", () => {
Expand Down

0 comments on commit 1f2c45d

Please sign in to comment.