Skip to content

Commit 8dfd4a1

Browse files
committed
Fix TS related issues in mangleErrors.mts
1 parent 33881a8 commit 8dfd4a1

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

packages/toolkit/scripts/mangleErrors.mts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
1-
const fs = require('fs')
1+
import type { Node, PluginObj, PluginPass } from '@babel/core'
22
import * as helperModuleImports from '@babel/helper-module-imports'
33
import * as fs from 'node:fs'
4+
import * as path from 'node:path'
5+
6+
type Babel = typeof import('@babel/core')
7+
8+
/**
9+
* Represents the options for the {@linkcode mangleErrorsPlugin}.
10+
*
11+
* @internal
12+
*/
13+
export interface MangleErrorsPluginOptions {
14+
/**
15+
* Whether to minify the error messages or not.
16+
* If `true`, the error messages will be replaced with an index
17+
* that maps object lookup.
18+
*/
19+
minify: boolean
20+
}
421

522
/**
623
* Converts an AST type into a JavaScript string so that it can be added to the error message lookup.
724
*
825
* Adapted from React (https://github.com/facebook/react/blob/master/scripts/shared/evalToString.js) with some
926
* adjustments.
1027
*/
11-
const evalToString = (ast) => {
28+
const evalToString = (
29+
ast: Node | { type: 'Literal'; value: string },
30+
): string => {
1231
switch (ast.type) {
1332
case 'StringLiteral':
1433
case 'Literal': // ESLint
@@ -55,7 +74,10 @@ const evalToString = (ast) => {
5574
* throw new Error(process.env.NODE_ENV === 'production' ? 0 : "This is my error message.");
5675
* throw new Error(process.env.NODE_ENV === 'production' ? 1 : "This is a second error message.");
5776
*/
58-
export const mangleErrorsPlugin = (babel) => {
77+
export const mangleErrorsPlugin = (
78+
babel: Babel,
79+
options: MangleErrorsPluginOptions,
80+
): PluginObj<PluginPass & MangleErrorsPluginOptions> => {
5981
const t = babel.types
6082
// When the plugin starts up, we'll load in the existing file. This allows us to continually add to it so that the
6183
// indexes do not change between builds.
@@ -65,7 +87,7 @@ export const mangleErrorsPlugin = (babel) => {
6587
if (fs.existsSync(errorsPath)) {
6688
errorsFiles = fs.readFileSync(errorsPath).toString()
6789
}
68-
let errors = Object.values(JSON.parse(errorsFiles || '{}'))
90+
const errors = Object.values(JSON.parse(errorsFiles || '{}'))
6991
// This variable allows us to skip writing back to the file if the errors array hasn't changed
7092
let changeInArray = false
7193

0 commit comments

Comments
 (0)