mirror of https://github.com/webpack/webpack.git
fix: create contexts in object shorthand syntax
This commit is contained in:
parent
3dbbdcb28c
commit
6c0056c207
|
@ -141,7 +141,9 @@ class CommonJsImportsParserPlugin {
|
|||
regExp: options.unknownContextRegExp,
|
||||
mode: "sync"
|
||||
},
|
||||
expr.range
|
||||
expr.range,
|
||||
undefined,
|
||||
parser.scope.inShorthand
|
||||
);
|
||||
dep.critical =
|
||||
options.unknownContextCritical &&
|
||||
|
|
|
@ -10,11 +10,12 @@ 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;
|
||||
this.inShorthand = inShorthand;
|
||||
}
|
||||
|
||||
get type() {
|
||||
|
@ -26,6 +27,7 @@ class CommonJsRequireContextDependency extends ContextDependency {
|
|||
|
||||
write(this.range);
|
||||
write(this.valueRange);
|
||||
write(this.inShorthand);
|
||||
|
||||
super.serialize(context);
|
||||
}
|
||||
|
@ -35,6 +37,7 @@ class CommonJsRequireContextDependency extends ContextDependency {
|
|||
|
||||
this.range = read();
|
||||
this.valueRange = read();
|
||||
this.inShorthand = read();
|
||||
|
||||
super.deserialize(context);
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
const CommonJsRequireContextDependency = require("./CommonJsRequireContextDependency");
|
||||
const ContextDependency = require("./ContextDependency");
|
||||
|
||||
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
||||
|
@ -24,13 +25,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 instanceof CommonJsRequireContextDependency && dep.inShorthand) {
|
||||
moduleExports = `${dep.inShorthand}: ${moduleExports}`;
|
||||
}
|
||||
if (moduleGraph.getModule(dep)) {
|
||||
if (dep.valueRange) {
|
||||
if (Array.isArray(dep.replaces)) {
|
||||
|
|
|
@ -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");
|
||||
});
|
|
@ -0,0 +1 @@
|
|||
export const obj = {require, r: require}
|
|
@ -0,0 +1,9 @@
|
|||
/** @type {import("../../../../").Configuration} */
|
||||
module.exports = {
|
||||
module: {
|
||||
unknownContextRegExp: /^\.\//,
|
||||
unknownContextCritical: false,
|
||||
exprContextRegExp: /^\.\//,
|
||||
exprContextCritical: false
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue