Skip to content

Commit a679a64

Browse files
authored
fix: fallback terser to main thread when nameCache is provided (#20750)
1 parent a67bb5f commit a679a64

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

packages/vite/src/node/__tests__/plugins/terser.spec.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ describe('terser', () => {
2727
},
2828
load(id) {
2929
if (id === '\0entry.js') {
30-
return `const foo = 1;console.log(foo)`
30+
return `
31+
const foo = 1;
32+
console.log(foo);
33+
const bar = { hello: 1, ["world"]: 2 };
34+
console.log(bar.hello + bar["world"]);
35+
`
3136
}
3237
},
3338
},
@@ -52,4 +57,21 @@ describe('terser', () => {
5257
})
5358
expect(resultCode).toContain('prefix_')
5459
})
60+
61+
test('nameCache', async () => {
62+
const nameCache = {}
63+
64+
await run({
65+
compress: false,
66+
mangle: {
67+
properties: {
68+
keep_quoted: true,
69+
},
70+
},
71+
nameCache,
72+
})
73+
74+
expect(nameCache).toHaveProperty('props.props.$hello')
75+
expect(nameCache).not.toHaveProperty('props.props.$world')
76+
})
5577
})

packages/vite/src/node/plugins/terser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
6666
(typeof options.mangle.properties === 'object' &&
6767
options.mangle.properties.nth_identifier?.get))) ||
6868
typeof options.format?.comments === 'function' ||
69-
typeof options.output?.comments === 'function'
69+
typeof options.output?.comments === 'function' ||
70+
options.nameCache
7071
)
7172
},
7273
max: maxWorkers,

0 commit comments

Comments
 (0)