Skip to content

Commit f402bf3

Browse files
author
chuckd
committed
Add support for cjsRequire function for loading CommonJS modules from AMD modules
1 parent 398d7fa commit f402bf3

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

NOTICE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ https://github.com/webpack/webpack/blob/v1.14.0/lib/dependencies/AMDDefineDepend
1111
https://github.com/webpack/webpack/blob/v1.14.0/lib/dependencies/ModuleDependencyTemplateAsRequireId.js
1212
https://github.com/webpack/webpack/blob/v1.14.0/lib/dependencies/AMDRequireArrayDependency.js
1313
https://github.com/webpack/webpack/blob/v1.14.0/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js
14+
https://github.com/webpack/webpack/blob/v1.14.0/lib/dependencies/CommonJsRequireDependencyParserPlugin.js
1415

1516
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:
1617
https://github.com/dojo/util/blob/1.10/build/transforms/writeDojo.js

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
# The Dojo loader
1111

12-
**dojo-webpack-plugin** uses the Dojo loader (dojo.js) at build time to resolve modules based on the properties specified in the Dojo loader config. In addition, a stripped-down build of the loader, as well as the loader config, are embedded in the packed application to support client-side execution of `require()` calls that have not been transformed by Webpack at build time (i.e. `require()` calls that reference non-stactic variables). Synchronous `require()` (e.g. `require('foo')`), which returns a reference to an already loaded module or else throws an exception, is supported, as are the `require.toAbsMid()` and `require.toUrl()` functions.
12+
**dojo-webpack-plugin** uses the Dojo loader (dojo.js) at build time to resolve modules based on the properties specified in the Dojo loader config. In addition, a stripped-down build of the loader, as well as the loader config, are embedded in the packed application to support client-side execution of `require()` calls that have not been transformed by Webpack at build time (i.e. `require()` calls that reference non-stactic variables), as well as Dojo's `require.toAbsMid()` and `require.toUrl()` functions.
13+
14+
Dojo supports a form of `require` (known as synchronous `require`) that has the same signature as CommonJS `require`. In Dojo, synchronous `require` returns a reference to an already loaded module, or else throws an exception if the module has not already been loaded and initialized. With this plugin, `require` calls matching the CommonJS/synchronous `require` signature which appear inside of AMD modules are treated as Dojo synchronous `require` calls. If you wish to load a CommonJS module from within an AMD module, you may do so using the `cjsRequire` function that is supported by the plugin.
1315

1416
This package does not include the Dojo loader. A custom build of the Dojo loader is built by Webpack based on the location of Dojo specified in the Dojo loader config. Alternatively, the location of a previously built loader may be specified using the [loader](#loader) option. See [Building the Dojo loader](#building-the-dojo-loader).
1517

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* (C) Copyright IBM Corp. 2012, 2016 All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/*
17+
MIT License http://www.opensource.org/licenses/mit-license.php
18+
Author Tobias Koppers @sokra
19+
*/
20+
"use strict";
21+
22+
const RequireHeaderDependency = require("webpack/lib/dependencies/RequireHeaderDependency");
23+
const LocalModuleDependency = require("webpack/lib/dependencies/LocalModuleDependency");
24+
const LocalModulesHelpers = require("webpack/lib/dependencies/LocalModulesHelpers");
25+
26+
module.exports = class CJSRequireDependencyParserPlugin {
27+
apply(parser) {
28+
parser.plugin("call cjsRequire", (expr) => {
29+
if(expr.arguments.length !== 1) return;
30+
let localModule;
31+
const param = parser.evaluateExpression(expr.arguments[0]);
32+
if(param.isString() && (localModule = LocalModulesHelpers.getLocalModule(parser.state, param.string))) {
33+
const dep = new LocalModuleDependency(localModule, expr.range);
34+
dep.loc = expr.loc;
35+
parser.state.current.addDependency(dep);
36+
return true;
37+
} else {
38+
const result = parser.applyPluginsBailResult("call require:commonjs:item", expr, param);
39+
if(result === undefined) {
40+
parser.applyPluginsBailResult("call require:commonjs:context", expr, param);
41+
} else {
42+
const dep = new RequireHeaderDependency(expr.callee.range);
43+
dep.loc = expr.loc;
44+
parser.state.current.addDependency(dep);
45+
}
46+
return true;
47+
}
48+
});
49+
}
50+
};

lib/DojoAMDPlugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const NullFactory = require("webpack/lib/NullFactory");
2424

2525
const DojoAMDRequireDependenciesBlockParserPlugin = require("./DojoAMDRequireDependenciesBlockParserPlugin");
2626
const DojoAMDDefineDependencyParserPlugin = require("./DojoAMDDefineDependencyParserPlugin");
27+
const CJSRequireDependencyParserPlugin = require("./CJSRequireDependencyParserPlugin");
2728
const DojoAMDMainTemplatePlugin = require("./DojoAMDMainTemplatePlugin");
2829
const DojoAMDChunkTemplatePlugin = require("./DojoAMDChunkTemplatePlugin");
2930
const DojoAMDResolverPlugin = require("./DojoAMDResolverPlugin");
@@ -131,7 +132,8 @@ module.exports = class DojoAMDPlugin {
131132
params.normalModuleFactory.plugin("parser", (parser) => {
132133
parser.apply(
133134
new DojoAMDRequireDependenciesBlockParserPlugin(this.options, reqWrapper),
134-
new DojoAMDDefineDependencyParserPlugin(this.options, reqWrapper)
135+
new DojoAMDDefineDependencyParserPlugin(this.options, reqWrapper),
136+
new CJSRequireDependencyParserPlugin()
135137
);
136138
});
137139
compiler.resolvers.normal.apply(

0 commit comments

Comments
 (0)