Skip to content

Commit

Permalink
fix: fallback terser to main thread when function options are used (#…
Browse files Browse the repository at this point in the history
…18987)

Co-authored-by: Wout Mertens <[email protected]>
  • Loading branch information
sapphi-red and wmertens authored Dec 17, 2024
1 parent d88d000 commit 12b612d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"sirv": "^3.0.0",
"source-map-support": "^0.5.21",
"strip-literal": "^2.1.1",
"terser": "^5.37.0",
"tinyglobby": "^0.2.10",
"tsconfck": "^3.1.4",
"tslib": "^2.8.1",
Expand Down
55 changes: 55 additions & 0 deletions packages/vite/src/node/__tests__/plugins/terser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, test } from 'vitest'
import { build } from 'vite'
import type { RollupOutput } from 'rollup'
import type { TerserOptions } from '../../plugins/terser'

const __dirname = resolve(fileURLToPath(import.meta.url), '..')

describe('terser', () => {
const run = async (terserOptions: TerserOptions) => {
const result = (await build({
root: resolve(__dirname, '../packages/build-project'),
logLevel: 'silent',
build: {
write: false,
minify: 'terser',
terserOptions,
},
plugins: [
{
name: 'test',
resolveId(id) {
if (id === 'entry.js') {
return '\0' + id
}
},
load(id) {
if (id === '\0entry.js') {
return `const foo = 1;console.log(foo)`
}
},
},
],
})) as RollupOutput
return result.output[0].code
}

test('basic', async () => {
await run({})
})

test('nth', async () => {
const resultCode = await run({
mangle: {
nth_identifier: {
get: (n) => {
return 'prefix_' + n.toString()
},
},
},
})
expect(resultCode).toContain('prefix_')
})
})
14 changes: 12 additions & 2 deletions packages/vite/src/node/plugins/terser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Terser } from 'dep-types/terser'
import { Worker } from 'artichokie'
import { WorkerWithFallback } from 'artichokie'
import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '..'
import { requireResolveFromRootWithFallback } from '../utils'
Expand Down Expand Up @@ -37,7 +37,7 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
const { maxWorkers, ...terserOptions } = config.build.terserOptions

const makeWorker = () =>
new Worker(
new WorkerWithFallback(
() =>
async (
terserPath: string,
Expand All @@ -50,6 +50,16 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
return terser.minify(code, options) as Terser.MinifyOutput
},
{
shouldUseFake(_terserPath, _code, options) {
return !!(
(typeof options.mangle === 'object' &&
(options.mangle.nth_identifier?.get ||
(typeof options.mangle.properties === 'object' &&
options.mangle.properties.nth_identifier?.get))) ||
typeof options.format?.comments === 'function' ||
typeof options.output?.comments === 'function'
)
},
max: maxWorkers,
},
)
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 12b612d

Please sign in to comment.