Repository: vitejs/vite
File: packages/vite/src/node/plugins/terser.ts (TypeScript)

Code before:

    async renderChunk(code, _chunk, outputOptions) {
      // This plugin is included for any non-false value of config.build.minify,
      // so that normal chunks can use the preferred minifier, and legacy chunks
      // can use terser.
      if (
        config.build.minify !== 'terser' &&
        // @ts-expect-error injected by @vitejs/plugin-legacy
        !outputOptions.__vite_force_terser__
      ) {
        return null
      }

      // Lazy load worker.
      worker ||= makeWorker()

      const terserPath = loadTerserPath(config.root)
      const res = await worker.run(terserPath, code, {
        safari10: true,
        ...terserOptions,
        format: {
          ...terserOptions.format,
          // For ES lib mode, preserve comments to keep pure annotations for tree-shaking
          preserve_annotations:
            config.build.lib && outputOptions.format === 'es',
        },
        sourceMap: !!outputOptions.sourcemap,
        module: outputOptions.format.startsWith('es'),
        toplevel: outputOptions.format === 'cjs',
      })
      return {
        code: res.code!,
        map: res.map as any,
      }
    },

    closeBundle() {
      worker?.stop()
    },
  }
}


Review comment (suggestion):
I think we should also respect the user's `preserve_annotations` config if they're not building a library in ES output. E.g. if they enable `preserve_annotations` explicitly for other things, this would've always force it to `false`.

Code after:

    applyToEnvironment(environment) {
      // We also need the plugin even if minify isn't 'terser' as we force
      // terser in plugin-legacy
      return !!environment.config.build.minify
    },

    async renderChunk(code, chunk, outputOptions) {
      // This plugin is included for any non-false value of config.build.minify,
      // so that normal chunks can use the preferred minifier, and legacy chunks
      // can use terser.
      if (
        config.build.minify !== 'terser' &&
        // @ts-expect-error injected by @vitejs/plugin-legacy
        !outputOptions.__vite_force_terser__
      ) {
        return null
      }

      // Lazy load worker.
      worker ||= makeWorker()

      const terserPath = pathToFileURL(loadTerserPath(config.root)).href
      try {
        const res = await worker.run(terserPath, code, {
          safari10: true,
          ...terserOptions,
          format: {
            ...terserOptions.format,
            // For ES lib mode, preserve comments to keep pure annotations for tree-shaking
            preserve_annotations:
              config.build.lib && outputOptions.format === 'es'
                ? true
                : terserOptions.format?.preserve_annotations,
          },
          sourceMap: !!outputOptions.sourcemap,
          module: outputOptions.format.startsWith('es'),
          toplevel: outputOptions.format === 'cjs',
        })
        return {
          code: res.code!,
          map: res.map as any,
        }
      } catch (e) {
        if (e.line !== undefined && e.col !== undefined) {
          e.loc = {
            file: chunk.fileName,
            line: e.line,
            column: e.col,
          }
        }