@@ -6,7 +6,7 @@ import type {
6
6
import { WorkerWithFallback } from 'artichokie'
7
7
import type { Plugin } from '../plugin'
8
8
import type { ResolvedConfig } from '..'
9
- import { requireResolveFromRootWithFallback } from '../utils'
9
+ import { generateCodeFrame , requireResolveFromRootWithFallback } from '../utils'
10
10
11
11
export interface TerserOptions extends TerserMinifyOptions {
12
12
/**
@@ -50,7 +50,13 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
50
50
) => {
51
51
const terser : typeof import ( 'terser' ) = ( await import ( terserPath ) )
52
52
. default
53
- return terser . minify ( code , options ) as TerserMinifyOutput
53
+ try {
54
+ return ( await terser . minify ( code , options ) ) as TerserMinifyOutput
55
+ } catch ( e ) {
56
+ // convert to a plain object as additional properties of Error instances are not
57
+ // sent back to the main thread
58
+ throw { stack : e . stack /* stack is non-enumerable */ , ...e }
59
+ }
54
60
} ,
55
61
{
56
62
shouldUseFake ( _terserPath , _code , options ) {
@@ -78,7 +84,7 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
78
84
return ! ! environment . config . build . minify
79
85
} ,
80
86
81
- async renderChunk ( code , _chunk , outputOptions ) {
87
+ async renderChunk ( code , chunk , outputOptions ) {
82
88
// This plugin is included for any non-false value of config.build.minify,
83
89
// so that normal chunks can use the preferred minifier, and legacy chunks
84
90
// can use terser.
@@ -100,16 +106,30 @@ export function terserPlugin(config: ResolvedConfig): Plugin {
100
106
worker ||= makeWorker ( )
101
107
102
108
const terserPath = pathToFileURL ( loadTerserPath ( config . root ) ) . href
103
- const res = await worker . run ( terserPath , code , {
104
- safari10 : true ,
105
- ...terserOptions ,
106
- sourceMap : ! ! outputOptions . sourcemap ,
107
- module : outputOptions . format . startsWith ( 'es' ) ,
108
- toplevel : outputOptions . format === 'cjs' ,
109
- } )
110
- return {
111
- code : res . code ! ,
112
- map : res . map as any ,
109
+ try {
110
+ const res = await worker . run ( terserPath , code , {
111
+ safari10 : true ,
112
+ ...terserOptions ,
113
+ sourceMap : ! ! outputOptions . sourcemap ,
114
+ module : outputOptions . format . startsWith ( 'es' ) ,
115
+ toplevel : outputOptions . format === 'cjs' ,
116
+ } )
117
+ return {
118
+ code : res . code ! ,
119
+ map : res . map as any ,
120
+ }
121
+ } catch ( e ) {
122
+ if ( e . line !== undefined && e . col !== undefined ) {
123
+ e . loc = {
124
+ file : chunk . fileName ,
125
+ line : e . line ,
126
+ column : e . col ,
127
+ }
128
+ }
129
+ if ( e . pos !== undefined ) {
130
+ e . frame = generateCodeFrame ( code , e . pos )
131
+ }
132
+ throw e
113
133
}
114
134
} ,
115
135
0 commit comments