(config: ResolvedConfig)
| 36 | } |
| 37 | |
| 38 | export function terserPlugin(config: ResolvedConfig): Plugin { |
| 39 | const { maxWorkers, ...terserOptions } = config.build.terserOptions |
| 40 | |
| 41 | const makeWorker = () => |
| 42 | new WorkerWithFallback( |
| 43 | () => |
| 44 | async ( |
| 45 | terserPath: string, |
| 46 | code: string, |
| 47 | options: TerserMinifyOptions, |
| 48 | ) => { |
| 49 | const terser: typeof import('terser') = await import(terserPath) |
| 50 | try { |
| 51 | return (await terser.minify(code, options)) as TerserMinifyOutput |
| 52 | } catch (e) { |
| 53 | // convert to a plain object as additional properties of Error instances are not |
| 54 | // sent back to the main thread |
| 55 | throw { stack: e.stack /* stack is non-enumerable */, ...e } |
| 56 | } |
| 57 | }, |
| 58 | { |
| 59 | shouldUseFake(_terserPath, _code, options) { |
| 60 | return !!( |
| 61 | (typeof options.mangle === 'object' && |
| 62 | (options.mangle.nth_identifier?.get || |
| 63 | (typeof options.mangle.properties === 'object' && |
| 64 | options.mangle.properties.nth_identifier?.get))) || |
| 65 | typeof options.format?.comments === 'function' || |
| 66 | typeof options.output?.comments === 'function' || |
| 67 | options.nameCache |
| 68 | ) |
| 69 | }, |
| 70 | max: maxWorkers, |
| 71 | }, |
| 72 | ) |
| 73 | |
| 74 | let worker: ReturnType<typeof makeWorker> |
| 75 | |
| 76 | return { |
| 77 | name: 'vite:terser', |
| 78 | |
| 79 | applyToEnvironment(environment) { |
| 80 | // We also need the plugin even if minify isn't 'terser' as we force |
| 81 | // terser in plugin-legacy |
| 82 | return !!environment.config.build.minify |
| 83 | }, |
| 84 | |
| 85 | async renderChunk(code, chunk, outputOptions) { |
| 86 | // This plugin is included for any non-false value of config.build.minify, |
| 87 | // so that normal chunks can use the preferred minifier, and legacy chunks |
| 88 | // can use terser. |
| 89 | if ( |
| 90 | config.build.minify !== 'terser' && |
| 91 | !this.environment.config.isOutputOptionsForLegacyChunks?.(outputOptions) |
| 92 | ) { |
| 93 | return null |
| 94 | } |
| 95 |
no outgoing calls
no test coverage detected