Skip to content

Commit 1f2c45d

Browse files
author
Chuck Dumont
authored
Merge pull request #50 from chuckdumont/work2
Fix build break caused by loader changes in Dojo
2 parents ea4e71e + 27b14c2 commit 1f2c45d

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Introduction
88

9-
**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:
9+
**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:
1010

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

237237
https://openntf.github.io/dojo-webpack-plugin-sample/test.html.
238+
239+
# Release Notes
240+
241+
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.
242+
243+
* 1.13.0 and later
244+
* 1.12.3 and later
245+
* 1.11.5 and later
246+
* 1.10.9 and later
247+
248+
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.

buildDojo/transforms/writeDojo.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ define([
2525
"util/build/buildControl",
2626
"util/build/fileUtils",
2727
"util/build/fs",
28-
"util/build/transforms/writeAmd"
28+
"util/build/transforms/writeAmd",
29+
"dojo/text!dojo/package.json"
2930

30-
], function(bc, fileUtils, fs, writeAmd){
31+
], function(bc, fileUtils, fs, writeAmd, pkg){
3132
return function(resource, callback){
3233
var
3334
waitCount = 1, // matches *1*
@@ -51,8 +52,9 @@ define([
5152

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

5860
onWriteComplete(0); // matches *1*

lib/DojoAMDMainTemplatePlugin.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ module.exports = class DojoAMDMainTemplatePlugin {
117117
buf.push("};");
118118
buf.push("var globalScope = (function(){return this;})();");
119119
buf.push("var loaderScope = {document:globalScope.document};");
120+
buf.push("loaderScope.global = loaderScope.window = loaderScope;");
120121
const dojoLoaderModule = compilation.modules.find((module) => { return module.rawRequest === options.loader;});
121122
if (!dojoLoaderModule) {
122123
throw Error("Can't locate " + options.loader + " in compilation");
@@ -134,7 +135,7 @@ module.exports = class DojoAMDMainTemplatePlugin {
134135
buf.push("if (!loaderConfig.has) loaderConfig.has = {};");
135136
buf.push("if (!('webpack' in loaderConfig.has)) loaderConfig.has.webpack = true;");
136137
buf.push("var dojoLoader = " + this.requireFn + "(" + JSON.stringify(dojoLoaderModule.id) + ");");
137-
buf.push("dojoLoader.call(loaderScope, loaderConfig, {hasCache:" + JSON.stringify(require("./defaultFeatures")) + "});");
138+
buf.push("dojoLoader.call(loaderScope, loaderConfig, {hasCache:" + JSON.stringify(require("./defaultFeatures")) + "}, loaderScope, loaderScope);");
138139
buf.push("req.has = loaderScope.require.has;");
139140
buf.push("req.rawConfig = loaderScope.require.rawConfig");
140141
buf.push("");

lib/DojoAMDPlugin.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports = class DojoAMDPlugin {
3939

4040
apply(compiler) {
4141
const loaderScope = {};
42+
loaderScope.global = loaderScope.window = loaderScope;
4243

4344
// Wrapper for Dojo require since we need to pass require to plugin constructors
4445
// before the actual require function is available.
@@ -75,9 +76,7 @@ module.exports = class DojoAMDPlugin {
7576
if (!loaderScope.require) {
7677
this.getDojoLoader(this.options, loaderConfig, (err, dojoLoader) => {
7778
if (!err) {
78-
dojoLoader.call(loaderScope, loaderConfig, {hasCache:{'dojo-config-api':1, 'dojo-inject-api':1, 'host-node':0, 'dom':0, 'dojo-sniff':0}});
79-
} else {
80-
console.error(err);
79+
dojoLoader.call(loaderScope, loaderConfig, {hasCache:{'dojo-config-api':1, 'dojo-inject-api':1, 'host-node':0, 'dom':0, 'dojo-sniff':0}}, loaderScope, loaderScope);
8180
}
8281
callback(err);
8382
});
@@ -109,6 +108,27 @@ module.exports = class DojoAMDPlugin {
109108
}
110109
});
111110
});
111+
112+
compiler.plugin("make", (compilation__, callback) => {
113+
// Vefiry that loader version and dojo version are the same
114+
params.normalModuleFactory.create({
115+
dependencies: [{request: "dojo/package.json"}]
116+
}, (err, module) => {
117+
if (!err) {
118+
const dojoVersion = require(module.request).version;
119+
if (dojoVersion !== loaderScope.loaderVersion) {
120+
err = new Error(
121+
`Dojo loader version does not match the version of Dojo.
122+
Loader version = ${loaderScope.loaderVersion}.
123+
Dojo version = ${dojoVersion}.
124+
You may need to rebuild the Dojo loader.
125+
Refer to https://github.com/OpenNTF/dojo-webpack-plugin/blob/master/README.md#building-the-dojo-loader`);
126+
}
127+
return callback(err);
128+
}
129+
callback();
130+
});
131+
});
112132
});
113133

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

0 commit comments

Comments
 (0)