Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create contexts in object shorthand syntax #14546

Merged
merged 3 commits into from Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/dependencies/CommonJsImportsParserPlugin.js
Expand Up @@ -141,7 +141,9 @@ class CommonJsImportsParserPlugin {
regExp: options.unknownContextRegExp,
mode: "sync"
},
expr.range
expr.range,
undefined,
parser.scope.inShorthand
);
dep.critical =
options.unknownContextCritical &&
Expand Down
6 changes: 5 additions & 1 deletion lib/dependencies/CommonJsRequireContextDependency.js
Expand Up @@ -10,11 +10,13 @@ const ContextDependency = require("./ContextDependency");
const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall");

class CommonJsRequireContextDependency extends ContextDependency {
constructor(options, range, valueRange) {
constructor(options, range, valueRange, inShorthand) {
super(options);

this.range = range;
this.valueRange = valueRange;
// inShorthand must be serialized by subclasses that use it
this.inShorthand = inShorthand;
sokra marked this conversation as resolved.
Show resolved Hide resolved
}

get type() {
Expand All @@ -26,6 +28,7 @@ class CommonJsRequireContextDependency extends ContextDependency {

write(this.range);
write(this.valueRange);
write(this.inShorthand);

super.serialize(context);
}
Expand All @@ -35,6 +38,7 @@ class CommonJsRequireContextDependency extends ContextDependency {

this.range = read();
this.valueRange = read();
this.inShorthand = read();

super.deserialize(context);
}
Expand Down
1 change: 1 addition & 0 deletions lib/dependencies/ContextDependency.js
Expand Up @@ -47,6 +47,7 @@ class ContextDependency extends Dependency {
this.request = undefined;
this.range = undefined;
this.valueRange = undefined;
this.inShorthand = undefined;
// TODO refactor this
this.replaces = undefined;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/dependencies/ContextDependencyTemplateAsRequireCall.js
Expand Up @@ -24,13 +24,16 @@ class ContextDependencyTemplateAsRequireCall extends ContextDependency.Template
{ runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
) {
const dep = /** @type {ContextDependency} */ (dependency);
const moduleExports = runtimeTemplate.moduleExports({
let moduleExports = runtimeTemplate.moduleExports({
module: moduleGraph.getModule(dep),
chunkGraph,
request: dep.request,
runtimeRequirements
});

if (dep.inShorthand) {
moduleExports = `${dep.inShorthand}: ${moduleExports}`;
}
if (moduleGraph.getModule(dep)) {
if (dep.valueRange) {
if (Array.isArray(dep.replaces)) {
Expand Down
4 changes: 4 additions & 0 deletions test/configCases/parsing/issue-14545/index.js
@@ -0,0 +1,4 @@
it("should generate valid code when 'require' encounters object shorthand syntax", function() {
expect(require("./module").obj.require).toEqual(require("./module").obj.r);
expect(require("./module").obj.require).toBeTypeOf("function");
});
1 change: 1 addition & 0 deletions test/configCases/parsing/issue-14545/module.js
@@ -0,0 +1 @@
export const obj = {require, r: require}
9 changes: 9 additions & 0 deletions test/configCases/parsing/issue-14545/webpack.config.js
@@ -0,0 +1,9 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
module: {
unknownContextRegExp: /^\.\//,
unknownContextCritical: false,
exprContextRegExp: /^\.\//,
exprContextCritical: false
}
};