Skip to content

Commit f0b37d3

Browse files
committed
Fix mangleErrorsPlugin not preserving different error types
- Resolved an issue where `mangleErrorsPlugin` did not preserve the original error constructor type. Previously, if a `TypeError` or any other custom error was thrown, the plugin would incorrectly transform it into a generic `Error`.
1 parent 0418326 commit f0b37d3

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

packages/toolkit/scripts/mangleErrors.mts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,15 @@ export const mangleErrorsPlugin = (
9797
changeInArray = false
9898
},
9999
visitor: {
100-
ThrowStatement(path, file) {
100+
ThrowStatement(path) {
101+
if (
102+
!('arguments' in path.node.argument) ||
103+
!t.isNewExpression(path.node.argument)
104+
) {
105+
return
106+
}
101107
const args = path.node.argument.arguments
102-
const minify = file.opts.minify
108+
const { minify } = options
103109

104110
if (args && args[0]) {
105111
// Skip running this logic when certain types come up:
@@ -112,11 +118,14 @@ export const mangleErrorsPlugin = (
112118
path.node.argument.arguments[0].type === 'CallExpression' ||
113119
path.node.argument.arguments[0].type === 'ObjectExpression' ||
114120
path.node.argument.arguments[0].type === 'MemberExpression' ||
115-
path.node.argument.arguments[0]?.callee?.name === 'HandledError'
121+
!t.isExpression(path.node.argument.arguments[0]) ||
122+
!t.isIdentifier(path.node.argument.callee)
116123
) {
117124
return
118125
}
119126

127+
const errorName = path.node.argument.callee.name
128+
120129
const errorMsgLiteral = evalToString(path.node.argument.arguments[0])
121130

122131
if (errorMsgLiteral.includes('Super expression')) {
@@ -149,13 +158,13 @@ export const mangleErrorsPlugin = (
149158
if (minify) {
150159
path.replaceWith(
151160
t.throwStatement(
152-
t.newExpression(t.identifier('Error'), [prodMessage]),
161+
t.newExpression(t.identifier(errorName), [prodMessage]),
153162
),
154163
)
155164
} else {
156165
path.replaceWith(
157166
t.throwStatement(
158-
t.newExpression(t.identifier('Error'), [
167+
t.newExpression(t.identifier(errorName), [
159168
t.conditionalExpression(
160169
t.binaryExpression(
161170
'===',
@@ -181,4 +190,4 @@ export const mangleErrorsPlugin = (
181190
}
182191
}
183192

184-
export default mangleErrorsPlugin
193+
export default mangleErrorsPlugin

packages/toolkit/tsup.config.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import path from 'node:path'
66
import { fileURLToPath } from 'node:url'
77
import type { Options as TsupOptions } from 'tsup'
88
import { defineConfig } from 'tsup'
9+
import type { MangleErrorsPluginOptions } from './scripts/mangleErrors.mjs'
10+
import { mangleErrorsPlugin } from './scripts/mangleErrors.mjs'
911

1012
// No __dirname under Node ESM
1113
const __filename = fileURLToPath(import.meta.url)
@@ -129,18 +131,28 @@ if (process.env.NODE_ENV === 'production') {
129131

130132
// Extract error strings, replace them with error codes, and write messages to a file
131133
const mangleErrorsTransform: Plugin = {
132-
name: 'mangle-errors-plugin',
134+
name: mangleErrorsPlugin.name,
133135
setup(build) {
134-
const { onTransform } = getBuildExtensions(build, 'mangle-errors-plugin')
136+
const { onTransform } = getBuildExtensions(build, mangleErrorsPlugin.name)
135137

136138
onTransform({ loaders: ['ts', 'tsx'] }, async (args) => {
137139
try {
138-
const res = babel.transformSync(args.code, {
140+
const res = await babel.transformAsync(args.code, {
139141
parserOpts: {
140142
plugins: ['typescript', 'jsx'],
141143
},
142-
plugins: [['./scripts/mangleErrors.cjs', { minify: false }]],
143-
})!
144+
plugins: [
145+
[
146+
mangleErrorsPlugin,
147+
{ minify: false } satisfies MangleErrorsPluginOptions,
148+
],
149+
],
150+
})
151+
152+
if (res == null) {
153+
throw new Error('Babel transformAsync returned null')
154+
}
155+
144156
return {
145157
code: res.code!,
146158
map: res.map!,

0 commit comments

Comments
 (0)