Skip to content

Commit 9a7803d

Browse files
committed
Update tsup config to extract error messages
1 parent be2b255 commit 9a7803d

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

errors.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"0": "The object notation for `createReducer` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createReducer",
3+
"1": "A case reducer on a non-draftable value must not return undefined",
4+
"2": ": ",
5+
"3": "prepareAction did not return an object",
6+
"4": "\"reducer\" is a required argument, and must be a function or an object of functions that can be passed to combineReducers",
7+
"5": "when using a middleware builder function, an array of middleware must be returned",
8+
"6": "each middleware provided to configureStore must be a function",
9+
"7": "\"enhancers\" field must be a callback",
10+
"8": "\"enhancers\" callback must return an array",
11+
"9": "each enhancer provided to configureStore must be a function",
12+
"10": "`name` is a required option for createSlice",
13+
"11": "The object notation for `createSlice.extraReducers` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createSlice",
14+
"12": "selectState returned undefined for an uninjected slice reducer",
15+
"13": "Please use the `create.preparedReducer` notation for prepared action creators with the `create` notation.",
16+
"14": "The slice reducer for key \"\" returned undefined when called for selector(). If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.",
17+
"15": "original must be used on state Proxy",
18+
"16": "Creating or removing a listener requires one of the known fields for matching an action",
19+
"17": "Unsubscribe not initialized",
20+
"18": ": getOriginalState can only be called synchronously",
21+
"19": "`builder.addCase` should only be called before calling `builder.addMatcher`",
22+
"20": "`builder.addCase` should only be called before calling `builder.addDefaultCase`",
23+
"21": "addCase cannot be called with two reducers for the same action type",
24+
"22": "`builder.addMatcher` should only be called before calling `builder.addDefaultCase`",
25+
"23": "`builder.addDefaultCase` can only be called once",
26+
"24": " is not a function",
27+
"25": "When using `fakeBaseQuery`, all queries & mutations must use the `queryFn` definition syntax.",
28+
"26": "Warning: Middleware for RTK-Query API at reducerPath \"\" has not been added to the store.\nYou must add the middleware for RTK-Query to function correctly!",
29+
"27": "Warning: Middleware for RTK-Query API at reducerPath \"\" has not been added to the store.\n You must add the middleware for RTK-Query to function correctly!",
30+
"28": "Cannot refetch a query that has not been started yet."
31+
}

packages/toolkit/tsup.config.ts

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { fileURLToPath } from 'url'
22
import path from 'path'
33
import fs from 'fs'
4-
import rimraf from 'rimraf'
5-
import { BuildOptions as ESBuildOptions } from 'esbuild'
4+
import type { BuildOptions as ESBuildOptions, Plugin } from 'esbuild'
65
import type { Options as TsupOptions } from 'tsup'
76
import { defineConfig } from 'tsup'
7+
import * as babel from '@babel/core'
8+
import { getBuildExtensions } from 'esbuild-extra'
89

910
import { delay } from './src/utils'
1011

@@ -93,20 +94,6 @@ const buildTargets: BuildOptions[] = [
9394
minify: true,
9495
env: 'production',
9596
},
96-
// {
97-
// format: 'umd',
98-
// name: 'umd',
99-
// target: 'es2018',
100-
// minify: false,
101-
// env: 'development',
102-
// },
103-
// {
104-
// format: 'umd',
105-
// name: 'umd.min',
106-
// target: 'es2018',
107-
// minify: true,
108-
// env: 'production',
109-
// },
11097
]
11198

11299
const entryPoints: EntryPointOptions[] = [
@@ -151,6 +138,32 @@ if (process.env.NODE_ENV === 'production') {
151138
)
152139
}
153140

141+
// Extract error strings, replace them with error codes, and write messages to a file
142+
const mangleErrorsTransform: Plugin = {
143+
name: 'mangle-errors-plugin',
144+
setup(build) {
145+
const { onTransform } = getBuildExtensions(build, 'mangle-errors-plugin')
146+
147+
onTransform({ loaders: ['ts', 'tsx'] }, async (args) => {
148+
try {
149+
const res = babel.transformSync(args.code, {
150+
parserOpts: {
151+
plugins: ['typescript', 'jsx'],
152+
},
153+
plugins: [['./scripts/mangleErrors.cjs', { minify: false }]],
154+
})!
155+
return {
156+
code: res.code!,
157+
map: res.map!,
158+
}
159+
} catch (err) {
160+
console.error('Babel mangleErrors error: ', err)
161+
return null
162+
}
163+
})
164+
},
165+
}
166+
154167
export default defineConfig((options) => {
155168
const configs = entryPoints
156169
.map((entryPointConfig) => {
@@ -190,6 +203,7 @@ export default defineConfig((options) => {
190203
minify,
191204
sourcemap: true,
192205
external: externals,
206+
esbuildPlugins: [mangleErrorsTransform],
193207
esbuildOptions(options) {
194208
// Needed to prevent auto-replacing of process.env.NODE_ENV in all builds
195209
options.platform = 'neutral'

0 commit comments

Comments
 (0)