Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fallback terser to main thread when function options are used #18987

Merged

Conversation

sapphi-red
Copy link
Member

Description

This PR makes terser to run in the main thread when function options are used.
I also tested out calling functions on the main thread side from the workers, but it was slower than simply running in the main thread.

the way I tested and the results

I ran the script below with each implementation.

import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { build } from 'vite'

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

const result = (await build({
  root: resolve(__dirname, '../../../../../../playground/resolve'), // or css
  logLevel: 'silent',
  build: {
    write: false,
    minify: 'terser',
    terserOptions: {
      mangle: {
        nth_identifier: {
          get: (n) => {
            return 'prefix_' + n.toString()
          },
        },
      },
    },
  },
}))

The results was:

  • resolve playground
    • call main thread: 2.76s
    • fallback: 2.48s
  • css playground
    • call main thread: 2.76s
    • fallback: 2.66s

It is expected that "call main thread" is slower for the resolve playground because it only has a single chunk. css playground generates multiple chunks but it's still slower than "fallback".

We can improve this by detecting if the passed function is serializable (i.e. does not use variables outside the function) and serializing the function in that case. But I think we can first merge this PR as it's better than nothing.

fixes #17409
refs #17589

Co-authored-by: Wout Mertens <Wout.Mertens@gmail.com>
@sapphi-red sapphi-red added the p3-minor-bug An edge case that only affects very specific usage (priority) label Dec 17, 2024
@patak-dev patak-dev merged commit 12b612d into vitejs:main Dec 17, 2024
16 checks passed
@sapphi-red sapphi-red deleted the fix/minify-terser-fallback-to-main-thread branch December 18, 2024 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error using build.terserOptions mangle nth_identifier
2 participants