Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckdumont committed Oct 2, 2017
2 parents 7a2711a + 879a0dd commit f0b4416
Show file tree
Hide file tree
Showing 16 changed files with 2,600 additions and 40 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node/**
node_modules/**
test/js/**
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules/
/test/js/
3 changes: 3 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ https://github.com/webpack/webpack/blob/v1.14.0/lib/dependencies/ModuleDependenc
https://github.com/webpack/webpack/blob/v1.14.0/lib/dependencies/AMDRequireArrayDependency.js
https://github.com/webpack/webpack/blob/v1.14.0/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js
https://github.com/webpack/webpack/blob/v1.14.0/lib/dependencies/CommonJsRequireDependencyParserPlugin.js
https://github.com/webpack/webpack/blob/v3.6.0/test/ConfigTestCases.test.js (test only)
https://github.com/webpack/webpack/blob/v3.6.0/test/checkArrayExpectation.js (test only)
https://github.com/webpack/webpack/blob/v3.6.0/lib/node/NodeMainTemplatePlugin.js (test only)

The product includes third party code that has been modified and adapted from the following file, originating in the Dojo Util project, and licensed under the Dojo License - http://dojotoolkit.org/license:
https://github.com/dojo/util/blob/1.10/build/transforms/writeDojo.js
44 changes: 37 additions & 7 deletions lib/DojoAMDMainTemplatePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

const util = require('util');
const ConcatSource = require("webpack-sources").ConcatSource;

module.exports = class DojoAMDMainTemplatePlugin {
constructor(options) {
Expand Down Expand Up @@ -102,8 +103,11 @@ module.exports = class DojoAMDMainTemplatePlugin {
}

function dojoModuleFromWebpackModule(webpackModule) {
var dojoModule = Object.create(webpackModule);
dojoModule.id = req.absMidsById[webpackModule.id];
var dojoModule = {};
Object.keys(webpackModule).forEach(key => {
dojoModule[key] = webpackModule[key];
});
dojoModule.i = req.absMidsById[webpackModule.i] || webpackModule.i;
return dojoModule;
}

Expand All @@ -125,6 +129,31 @@ module.exports = class DojoAMDMainTemplatePlugin {
throw new Error('Unsupported require call');
}
}

// For unit testing only
compilation.mainTemplate.plugin("require", function(source) {
if (compilation.options.target === "async-node") {
// When running unit tests in node, the scoped require function is node's CommonJs
// require. This code, together with the 'render' plugin below, defines a scoped
// Dojo require variable for each AMD module so that referencing 'require' from
// within the module will yield the Dojo function.
source = source.replace(/__webpack_require__\);/g, "__webpack_require__, req);");
}
return source;
});

// For unit testing only
compilation.moduleTemplate.plugin("render", function(source, module) {
var result = source;
if (module.isAMD && compilation.options.target === "async-node") {
// Define a module scoped 'require' variable for AMD modules that yields the
// the Dojo require function.
result = new ConcatSource();
result.add("var require = arguments[3];");
result.add(source);
}
return result;
});

compilation.mainTemplate.plugin("bootstrap", function(source, chunk) {
const buf = [];
Expand Down Expand Up @@ -179,16 +208,17 @@ module.exports = class DojoAMDMainTemplatePlugin {
buf.push("(function(){ // Ensure this refers to global scope");
buf.push(this.indent("this.require = req;"));
if(chunk.chunks.length > 0) {
buf.push(this.indent("this[" + JSON.stringify(this.outputOptions.jsonpFunction) + "].registerAbsMids = registerAbsMids;"));
var jsonpFn = JSON.stringify(this.outputOptions.jsonpFunction);
if (compilation.options.target === "async-node") {
// For unit tests, the jsonp function is not defined by default
buf.push(this.indent(`this[${jsonpFn}] = this[${jsonpFn}] || {}`));
}
buf.push(this.indent(`this[${jsonpFn}].registerAbsMids = registerAbsMids;`));
}
buf.push("})();");
return this.asString(buf);
});

compilation.mainTemplate.plugin("module-obj", function(source) {
return source.replace("i: moduleId,", "i: req.absMidsById[moduleId] || moduleId,");
});

compilation.mainTemplate.plugin("require-extensions", function(source) {
const buf = [];
buf.push(source);
Expand Down

0 comments on commit f0b4416

Please sign in to comment.