Skip to content

Commit 58fb035

Browse files
committed
fix: sub define key should't be renamed when it's a defined variable
1 parent 644f1d1 commit 58fb035

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

lib/DefinePlugin.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const RuntimeGlobals = require("./RuntimeGlobals");
1414
const WebpackError = require("./WebpackError");
1515
const ConstDependency = require("./dependencies/ConstDependency");
1616
const BasicEvaluatedExpression = require("./javascript/BasicEvaluatedExpression");
17-
17+
const { VariableInfo } = require("./javascript/JavascriptParser");
1818
const {
1919
evaluateToString,
2020
toConstantDependency
@@ -455,10 +455,16 @@ class DefinePlugin {
455455
*/
456456
const applyDefineKey = (prefix, key) => {
457457
const splittedKey = key.split(".");
458+
const firstKey = splittedKey[0];
458459
for (const [i, _] of splittedKey.slice(1).entries()) {
459460
const fullKey = prefix + splittedKey.slice(0, i + 1).join(".");
460461
parser.hooks.canRename.for(fullKey).tap(PLUGIN_NAME, () => {
461462
addValueDependency(key);
463+
if (
464+
parser.scope.definitions.get(firstKey) instanceof VariableInfo
465+
) {
466+
return false;
467+
}
462468
return true;
463469
});
464470
}

lib/javascript/JavascriptParser.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5004,3 +5004,4 @@ module.exports.ALLOWED_MEMBER_TYPES_EXPRESSION =
50045004
module.exports.ALLOWED_MEMBER_TYPES_CALL_EXPRESSION =
50055005
ALLOWED_MEMBER_TYPES_CALL_EXPRESSION;
50065006
module.exports.getImportAttributes = getImportAttributes;
5007+
module.exports.VariableInfo = VariableInfo;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
export default {
3+
bar: "test"
4+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
import foo from './foo.js';
3+
4+
function works1() {
5+
return foo.bar;
6+
}
7+
8+
function works2() {
9+
const v = foo.bar;
10+
return v;
11+
}
12+
13+
function works3() {
14+
const v = foo.bar.baz;
15+
return v;
16+
}
17+
18+
it("should compile and run", () => {
19+
expect(works1()).toBe("test");
20+
expect(works2()).toBe("test");
21+
expect(works3()).toBe("test");
22+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var DefinePlugin = require("../../../../").DefinePlugin;
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
plugins: [
6+
new DefinePlugin({
7+
"foo.bar.baz": '"test"'
8+
})
9+
]
10+
};

types.d.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,7 +4546,7 @@ declare interface ExportSpec {
45464546
*/
45474547
hidden?: boolean;
45484548
}
4549-
type ExportedVariableInfo = string | ScopeInfo | VariableInfo;
4549+
type ExportedVariableInfo = string | VariableInfo | ScopeInfo;
45504550
declare abstract class ExportsInfo {
45514551
get ownedExports(): Iterable<ExportInfo>;
45524552
get orderedOwnedExports(): Iterable<ExportInfo>;
@@ -6810,7 +6810,7 @@ declare class JavascriptParser extends Parser {
68106810
| undefined
68116811
| ((
68126812
arg0: string,
6813-
arg1: string | ScopeInfo | VariableInfo,
6813+
arg1: string | VariableInfo | ScopeInfo,
68146814
arg2: () => string[]
68156815
) => any),
68166816
defined: undefined | ((arg0: string) => any),
@@ -7143,6 +7143,7 @@ declare class JavascriptParser extends Parser {
71437143
| ExportAllDeclarationJavascriptParser
71447144
| ImportExpressionJavascriptParser
71457145
) => undefined | ImportAttributes;
7146+
static VariableInfo: typeof VariableInfo;
71467147
}
71477148

71487149
/**
@@ -13918,7 +13919,7 @@ declare interface RuntimeValueOptions {
1391813919
* to create the range of the _parent node_.
1391913920
*/
1392013921
declare interface ScopeInfo {
13921-
definitions: StackedMap<string, ScopeInfo | VariableInfo>;
13922+
definitions: StackedMap<string, VariableInfo | ScopeInfo>;
1392213923
topLevelScope: boolean | "arrow";
1392313924
inShorthand: string | boolean;
1392413925
inTaggedTemplateTag: boolean;
@@ -15249,7 +15250,12 @@ type UsageStateType = 0 | 1 | 2 | 3 | 4;
1524915250
type UsedName = string | false | string[];
1525015251
type Value = string | number | boolean | RegExp;
1525115252
type ValueCacheVersion = string | Set<string>;
15252-
declare abstract class VariableInfo {
15253+
declare class VariableInfo {
15254+
constructor(
15255+
declaredScope: ScopeInfo,
15256+
freeName?: string | true,
15257+
tagInfo?: TagInfo
15258+
);
1525315259
declaredScope: ScopeInfo;
1525415260
freeName?: string | true;
1525515261
tagInfo?: TagInfo;

0 commit comments

Comments
 (0)