Skip to content

Commit 5100534

Browse files
author
Chuck Dumont
authored
Merge pull request #100 from chuckdumont/work
Fix issue with bad parser context when handling 'call define' expression
2 parents b96519b + 213a941 commit 5100534

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

lib/DojoAMDDefineDependencyParserPlugin.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,39 @@ module.exports = class DojoAMDDefineDependencyParserPlugin extends AMDDefineDepe
3333
}
3434

3535
apply(parser) {
36-
super.apply(Object.assign(Object.create(parser), {
37-
plugin: (expression, callback) => {
38-
if (expression === "call define") {
39-
// Augment base class implementation for "call define"
40-
parser.plugin(expression, this.callDefine.bind(this, callback, parser));
41-
}
42-
}
43-
}));
36+
this.subclassCallDefineHandler(parser);
4437

4538
parser.plugin("call define:amd:array", this.callDefineAmdArray.bind(this, parser));
4639

4740
parser.plugin("call define:amd:item", this.callDefineAmdItem.bind(this, parser));
4841
}
4942

43+
subclassCallDefineHandler(parser) {
44+
// "Subclasses" the 'call define' handler in the base class implementation. This is a bit
45+
// painful because the base class is not set up to support subclassing, but we manage by
46+
// replacing the parser plugin handler for the duration of the apply call in the super
47+
// class and then grabbing the 'call define' callback so that we can use it for our own
48+
// 'call define' handler.
49+
var original_plugin;
50+
if (parser.hasOwnProperty("plugin")) {
51+
original_plugin = parser.plugin;
52+
}
53+
parser.plugin = (expression, callback) => {
54+
if (expression === "call define") {
55+
// Augment base class implementation for "call define"
56+
(original_plugin || parser.__proto__.plugin).call(parser, expression, this.callDefine.bind(this, callback, parser));
57+
}
58+
};
59+
super.apply(parser);
60+
delete parser.plugin;
61+
if (original_plugin) {
62+
parser.plugin = original_plugin;
63+
}
64+
}
65+
5066
callDefine(cb, parser, expr) {
5167
parser.state.current.isAMD = true;
52-
const result = cb.call(parser, expr); // invoke base class implementation
68+
const result = cb.call(this, expr); // invoke base class implementation
5369
/* istanbul ignore if */
5470
if (!AMDDefineDependencyParserPlugin.prototype.newDefineDependency) {
5571
// This is pretty hacky. Earlier versions of webpack don't provide the newDefineDependency method allowing us

0 commit comments

Comments
 (0)