Skip to content

Commit

Permalink
Update tsup config to extract error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Sep 24, 2023
1 parent be2b255 commit 9a7803d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
31 changes: 31 additions & 0 deletions errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"0": "The object notation for `createReducer` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createReducer",
"1": "A case reducer on a non-draftable value must not return undefined",
"2": ": ",
"3": "prepareAction did not return an object",
"4": "\"reducer\" is a required argument, and must be a function or an object of functions that can be passed to combineReducers",
"5": "when using a middleware builder function, an array of middleware must be returned",
"6": "each middleware provided to configureStore must be a function",
"7": "\"enhancers\" field must be a callback",
"8": "\"enhancers\" callback must return an array",
"9": "each enhancer provided to configureStore must be a function",
"10": "`name` is a required option for createSlice",
"11": "The object notation for `createSlice.extraReducers` has been removed. Please use the 'builder callback' notation instead: https://redux-toolkit.js.org/api/createSlice",
"12": "selectState returned undefined for an uninjected slice reducer",
"13": "Please use the `create.preparedReducer` notation for prepared action creators with the `create` notation.",
"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.",
"15": "original must be used on state Proxy",
"16": "Creating or removing a listener requires one of the known fields for matching an action",
"17": "Unsubscribe not initialized",
"18": ": getOriginalState can only be called synchronously",
"19": "`builder.addCase` should only be called before calling `builder.addMatcher`",
"20": "`builder.addCase` should only be called before calling `builder.addDefaultCase`",
"21": "addCase cannot be called with two reducers for the same action type",
"22": "`builder.addMatcher` should only be called before calling `builder.addDefaultCase`",
"23": "`builder.addDefaultCase` can only be called once",
"24": " is not a function",
"25": "When using `fakeBaseQuery`, all queries & mutations must use the `queryFn` definition syntax.",
"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!",
"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!",
"28": "Cannot refetch a query that has not been started yet."
}
46 changes: 30 additions & 16 deletions packages/toolkit/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { fileURLToPath } from 'url'
import path from 'path'
import fs from 'fs'
import rimraf from 'rimraf'
import { BuildOptions as ESBuildOptions } from 'esbuild'
import type { BuildOptions as ESBuildOptions, Plugin } from 'esbuild'
import type { Options as TsupOptions } from 'tsup'
import { defineConfig } from 'tsup'
import * as babel from '@babel/core'
import { getBuildExtensions } from 'esbuild-extra'

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

Expand Down Expand Up @@ -93,20 +94,6 @@ const buildTargets: BuildOptions[] = [
minify: true,
env: 'production',
},
// {
// format: 'umd',
// name: 'umd',
// target: 'es2018',
// minify: false,
// env: 'development',
// },
// {
// format: 'umd',
// name: 'umd.min',
// target: 'es2018',
// minify: true,
// env: 'production',
// },
]

const entryPoints: EntryPointOptions[] = [
Expand Down Expand Up @@ -151,6 +138,32 @@ if (process.env.NODE_ENV === 'production') {
)
}

// Extract error strings, replace them with error codes, and write messages to a file
const mangleErrorsTransform: Plugin = {
name: 'mangle-errors-plugin',
setup(build) {
const { onTransform } = getBuildExtensions(build, 'mangle-errors-plugin')

onTransform({ loaders: ['ts', 'tsx'] }, async (args) => {
try {
const res = babel.transformSync(args.code, {
parserOpts: {
plugins: ['typescript', 'jsx'],
},
plugins: [['./scripts/mangleErrors.cjs', { minify: false }]],
})!
return {
code: res.code!,
map: res.map!,
}
} catch (err) {
console.error('Babel mangleErrors error: ', err)
return null
}
})
},
}

export default defineConfig((options) => {
const configs = entryPoints
.map((entryPointConfig) => {
Expand Down Expand Up @@ -190,6 +203,7 @@ export default defineConfig((options) => {
minify,
sourcemap: true,
external: externals,
esbuildPlugins: [mangleErrorsTransform],
esbuildOptions(options) {
// Needed to prevent auto-replacing of process.env.NODE_ENV in all builds
options.platform = 'neutral'
Expand Down

0 comments on commit 9a7803d

Please sign in to comment.