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

Code before:
      // 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
      }

      // Do not minify ES lib output since that would remove pure annotations
      // and break tree-shaking, unless the user explicitly enables terser.
      if (
        config.build.lib &&
        outputOptions.format === 'es' &&
        config.build.minify !== 'terser'
      ) {
        return null
      }

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

      const terserPath = loadTerserPath(config.root)

      // For ES lib mode, preserve comments to maintain pure annotations and tree-shaking
      const isEsLibMode = config.build.lib && outputOptions.format === 'es'
      const preserveComments = isEsLibMode
        ? /^!/ // Preserve comments starting with ! (license comments)
        : terserOptions.output?.comments

      const res = await worker.run(terserPath, code, {
        safari10: true,
        ...terserOptions,
        output: {
          ...terserOptions.output,
          comments: preserveComments,
        },
        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):
```suggestion
      // For ES lib mode, preserve comments to maintain pure annotations for tree-shaking
```

Code after:
      // 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,
          }
        }
        if (e.pos !== undefined) {
          e.frame = generateCodeFrame(code, e.pos)
        }