Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 42 additions & 16 deletions packages/vite/src/node/plugins/terser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { WorkerWithFallback } from 'artichokie'
import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '..'
import { requireResolveFromRootWithFallback } from '../utils'
import { generateCodeFrame, requireResolveFromRootWithFallback } from '../utils'

export interface TerserOptions extends TerserMinifyOptions {
/**
Expand Down Expand Up @@ -47,10 +47,16 @@
code: string,
options: TerserMinifyOptions,
) => {
Comment thread
sapphi-red marked this conversation as resolved.
// test fails when using `import`. maybe related: https://github.com/nodejs/node/issues/43205
// eslint-disable-next-line no-restricted-globals -- this function runs inside cjs
const terser = require(terserPath)
return terser.minify(code, options) as TerserMinifyOutput
try {
// test fails when using `import`. maybe related: https://github.com/nodejs/node/issues/43205
// eslint-disable-next-line no-restricted-globals -- this function runs inside cjs
const terser = require(terserPath)
Comment thread
sapphi-red marked this conversation as resolved.
Outdated
return (await terser.minify(code, options)) as TerserMinifyOutput
Comment thread
sapphi-red marked this conversation as resolved.
} catch (e) {
// convert to a plain object as additional properties of Error instances are not
// sent back to the main thread
Comment thread
bluwy marked this conversation as resolved.
throw { stack: e.stack /* stack is non-enumerable */, ...e }
}
},
{
shouldUseFake(_terserPath, _code, options) {
Expand Down Expand Up @@ -78,7 +84,7 @@
return !!environment.config.build.minify
},

async renderChunk(code, _chunk, outputOptions) {
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.
Expand All @@ -100,16 +106,36 @@
worker ||= makeWorker()

const terserPath = loadTerserPath(config.root)
const res = await worker.run(terserPath, code, {
safari10: true,
...terserOptions,
sourceMap: !!outputOptions.sourcemap,
module: outputOptions.format.startsWith('es'),
toplevel: outputOptions.format === 'cjs',
})
return {
code: res.code!,
map: res.map as any,
try {
const res = await worker.run(terserPath, code, {
safari10: true,
...terserOptions,
sourceMap: !!outputOptions.sourcemap,
module: outputOptions.format.startsWith('es'),
toplevel: outputOptions.format === 'cjs',
})
return {
code: res.code!,
map: res.map as any,
}
} catch (e) {
e.loc = {
file: chunk.fileName,
line: e.line,
column: e.col,
}
if (e.line !== undefined && e.col !== undefined) {
e.loc = {
file: chunk.fileName,
line: e.line,
column: e.col,
}
Comment thread
sapphi-red marked this conversation as resolved.
Outdated
}
if (e.pos !== undefined) {
e.frame = generateCodeFrame(code, e.pos)
}
e.pos !== undefined ? generateCodeFrame(code, e.pos) : undefined

Check failure on line 137 in packages/vite/src/node/plugins/terser.ts

View workflow job for this annotation

GitHub Actions / Lint: node-22, ubuntu-latest

Expected an assignment or function call and instead saw an expression
Comment thread
sapphi-red marked this conversation as resolved.
Outdated
throw e
}
},

Expand Down
Loading