Skip to content

Commit 12b612d

Browse files
sapphi-redwmertens
andauthoredDec 17, 2024··
fix: fallback terser to main thread when function options are used (#18987)
Co-authored-by: Wout Mertens <Wout.Mertens@gmail.com>
1 parent d88d000 commit 12b612d

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed
 

‎packages/vite/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
"sirv": "^3.0.0",
146146
"source-map-support": "^0.5.21",
147147
"strip-literal": "^2.1.1",
148+
"terser": "^5.37.0",
148149
"tinyglobby": "^0.2.10",
149150
"tsconfck": "^3.1.4",
150151
"tslib": "^2.8.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { resolve } from 'node:path'
2+
import { fileURLToPath } from 'node:url'
3+
import { describe, expect, test } from 'vitest'
4+
import { build } from 'vite'
5+
import type { RollupOutput } from 'rollup'
6+
import type { TerserOptions } from '../../plugins/terser'
7+
8+
const __dirname = resolve(fileURLToPath(import.meta.url), '..')
9+
10+
describe('terser', () => {
11+
const run = async (terserOptions: TerserOptions) => {
12+
const result = (await build({
13+
root: resolve(__dirname, '../packages/build-project'),
14+
logLevel: 'silent',
15+
build: {
16+
write: false,
17+
minify: 'terser',
18+
terserOptions,
19+
},
20+
plugins: [
21+
{
22+
name: 'test',
23+
resolveId(id) {
24+
if (id === 'entry.js') {
25+
return '\0' + id
26+
}
27+
},
28+
load(id) {
29+
if (id === '\0entry.js') {
30+
return `const foo = 1;console.log(foo)`
31+
}
32+
},
33+
},
34+
],
35+
})) as RollupOutput
36+
return result.output[0].code
37+
}
38+
39+
test('basic', async () => {
40+
await run({})
41+
})
42+
43+
test('nth', async () => {
44+
const resultCode = await run({
45+
mangle: {
46+
nth_identifier: {
47+
get: (n) => {
48+
return 'prefix_' + n.toString()
49+
},
50+
},
51+
},
52+
})
53+
expect(resultCode).toContain('prefix_')
54+
})
55+
})

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Terser } from 'dep-types/terser'
2-
import { Worker } from 'artichokie'
2+
import { WorkerWithFallback } from 'artichokie'
33
import type { Plugin } from '../plugin'
44
import type { ResolvedConfig } from '..'
55
import { requireResolveFromRootWithFallback } from '../utils'
@@ -37,7 +37,7 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
3737
const { maxWorkers, ...terserOptions } = config.build.terserOptions
3838

3939
const makeWorker = () =>
40-
new Worker(
40+
new WorkerWithFallback(
4141
() =>
4242
async (
4343
terserPath: string,
@@ -50,6 +50,16 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
5050
return terser.minify(code, options) as Terser.MinifyOutput
5151
},
5252
{
53+
shouldUseFake(_terserPath, _code, options) {
54+
return !!(
55+
(typeof options.mangle === 'object' &&
56+
(options.mangle.nth_identifier?.get ||
57+
(typeof options.mangle.properties === 'object' &&
58+
options.mangle.properties.nth_identifier?.get))) ||
59+
typeof options.format?.comments === 'function' ||
60+
typeof options.output?.comments === 'function'
61+
)
62+
},
5363
max: maxWorkers,
5464
},
5565
)

‎pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.