diff --git a/README.md b/README.md index 3d84a34..1579c7c 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/buildDojo/transforms/writeDojo.js b/buildDojo/transforms/writeDojo.js index 9a3d470..5865952 100644 --- a/buildDojo/transforms/writeDojo.js +++ b/buildDojo/transforms/writeDojo.js @@ -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* @@ -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* diff --git a/lib/DojoAMDMainTemplatePlugin.js b/lib/DojoAMDMainTemplatePlugin.js index 57efb1a..5d28b01 100644 --- a/lib/DojoAMDMainTemplatePlugin.js +++ b/lib/DojoAMDMainTemplatePlugin.js @@ -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"); @@ -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(""); diff --git a/lib/DojoAMDPlugin.js b/lib/DojoAMDPlugin.js index c3c084d..cc52f34 100644 --- a/lib/DojoAMDPlugin.js +++ b/lib/DojoAMDPlugin.js @@ -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. @@ -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); }); @@ -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", () => {