fix: create contexts in object shorthand syntax

This commit is contained in:
peterq 2021-10-21 14:52:07 +08:00
parent 3dbbdcb28c
commit 6c0056c207
6 changed files with 26 additions and 3 deletions

View File

@ -141,7 +141,9 @@ class CommonJsImportsParserPlugin {
regExp: options.unknownContextRegExp, regExp: options.unknownContextRegExp,
mode: "sync" mode: "sync"
}, },
expr.range expr.range,
undefined,
parser.scope.inShorthand
); );
dep.critical = dep.critical =
options.unknownContextCritical && options.unknownContextCritical &&

View File

@ -10,11 +10,12 @@ const ContextDependency = require("./ContextDependency");
const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall"); const ContextDependencyTemplateAsRequireCall = require("./ContextDependencyTemplateAsRequireCall");
class CommonJsRequireContextDependency extends ContextDependency { class CommonJsRequireContextDependency extends ContextDependency {
constructor(options, range, valueRange) { constructor(options, range, valueRange, inShorthand) {
super(options); super(options);
this.range = range; this.range = range;
this.valueRange = valueRange; this.valueRange = valueRange;
this.inShorthand = inShorthand;
} }
get type() { get type() {
@ -26,6 +27,7 @@ class CommonJsRequireContextDependency extends ContextDependency {
write(this.range); write(this.range);
write(this.valueRange); write(this.valueRange);
write(this.inShorthand);
super.serialize(context); super.serialize(context);
} }
@ -35,6 +37,7 @@ class CommonJsRequireContextDependency extends ContextDependency {
this.range = read(); this.range = read();
this.valueRange = read(); this.valueRange = read();
this.inShorthand = read();
super.deserialize(context); super.deserialize(context);
} }

View File

@ -5,6 +5,7 @@
"use strict"; "use strict";
const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency");
const ContextDependency = require("./ContextDependency"); const ContextDependency = require("./ContextDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
@ -24,13 +25,16 @@ class ContextDependencyTemplateAsRequireCall extends ContextDependency.Template
{ runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements } { runtimeTemplate, moduleGraph, chunkGraph, runtimeRequirements }
) { ) {
const dep = /** @type {ContextDependency} */ (dependency); const dep = /** @type {ContextDependency} */ (dependency);
const moduleExports = runtimeTemplate.moduleExports({ let moduleExports = runtimeTemplate.moduleExports({
module: moduleGraph.getModule(dep), module: moduleGraph.getModule(dep),
chunkGraph, chunkGraph,
request: dep.request, request: dep.request,
runtimeRequirements runtimeRequirements
}); });
if (dep instanceof CommonJsRequireContextDependency && dep.inShorthand) {
moduleExports = `${dep.inShorthand}: ${moduleExports}`;
}
if (moduleGraph.getModule(dep)) { if (moduleGraph.getModule(dep)) {
if (dep.valueRange) { if (dep.valueRange) {
if (Array.isArray(dep.replaces)) { if (Array.isArray(dep.replaces)) {

View File

@ -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");
});

View File

@ -0,0 +1 @@
export const obj = {require, r: require}

View File

@ -0,0 +1,9 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
module: {
unknownContextRegExp: /^\.\//,
unknownContextCritical: false,
exprContextRegExp: /^\.\//,
exprContextCritical: false
}
};