Skip to content

Commit

Permalink
perf(esbuild): don't keep names during minification (#19095)
Browse files Browse the repository at this point in the history
* perf: Don't keep names during minification

Saves 18KB. For error classes, names actually matter, so we do preserve
them manually.

* Fix build

* Update internal snapshots

* fix null types

* Update packages/client/src/runtime/query.ts

Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>

* Correcy internals import

---------

Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>
  • Loading branch information
SevInf and Jolg42 committed May 8, 2023
1 parent 01626da commit 5a0f107
Show file tree
Hide file tree
Showing 37 changed files with 151 additions and 10 deletions.
1 change: 0 additions & 1 deletion helpers/compile/build.ts
Expand Up @@ -25,7 +25,6 @@ export type BuildOptions = esbuild.BuildOptions & {
const DEFAULT_BUILD_OPTIONS = {
platform: 'node',
target: 'ES2020',
keepNames: true,
logLevel: 'error',
tsconfig: 'tsconfig.build.json',
incremental: process.env.WATCH === 'true',
Expand Down
11 changes: 9 additions & 2 deletions packages/client/src/generation/generateClient.ts
@@ -1,6 +1,13 @@
import { BinaryType, overwriteFile } from '@prisma/fetch-engine'
import type { BinaryPaths, DataSource, DMMF, GeneratorConfig } from '@prisma/generator-helper'
import { assertNever, ClientEngineType, getClientEngineType, getEngineVersion, Platform } from '@prisma/internals'
import {
assertNever,
ClientEngineType,
getClientEngineType,
getEngineVersion,
Platform,
setClassName,
} from '@prisma/internals'
import fs from 'fs'
import { ensureDir } from 'fs-extra'
import { bold, dim, green, red } from 'kleur/colors'
Expand All @@ -27,10 +34,10 @@ type OutputDeclaration = {
export class DenylistError extends Error {
constructor(message: string) {
super(message)
this.name = 'DenylistError'
this.stack = undefined
}
}
setClassName(DenylistError, 'DenylistError')

export interface GenerateClientOptions {
projectRoot?: string
Expand Down
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
import { setRetryable } from './utils/setRetryable'
Expand All @@ -15,3 +17,4 @@ export class BadRequestError extends DataProxyAPIError {
if (code) this.code = code
}
}
setClassName(BadRequestError, 'BadRequestError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { RequestResponse } from '../utils/request'
import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
Expand All @@ -17,3 +19,4 @@ export class HealthcheckTimeoutError extends DataProxyAPIError {
this.logs = logs
}
}
setClassName(HealthcheckTimeoutError, 'HealthcheckTimeoutError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { RequestResponse } from '../utils/request'
import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
Expand All @@ -17,3 +19,4 @@ export class EngineStartupError extends DataProxyAPIError {
this.logs = logs
}
}
setClassName(EngineStartupError, 'EngineStartupError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { RequestResponse } from '../utils/request'
import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
Expand All @@ -15,3 +17,4 @@ export class EngineVersionNotSupportedError extends DataProxyAPIError {
super('Engine version is not supported', setRetryable(info, false))
}
}
setClassName(EngineVersionNotSupportedError, 'EngineVersionNotSupportedError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { DataProxyErrorInfo } from './DataProxyError'
import { DataProxyError } from './DataProxyError'
import { setRetryable } from './utils/setRetryable'
Expand All @@ -12,3 +14,4 @@ export class ForcedRetryError extends DataProxyError {
super('This request must be retried', setRetryable(info, true))
}
}
setClassName(ForcedRetryError, 'ForcedRetryError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
import { setRetryable } from './utils/setRetryable'
Expand All @@ -14,3 +16,4 @@ export class GatewayTimeoutError extends DataProxyAPIError {
super(message, setRetryable(info, false))
}
}
setClassName(GatewayTimeoutError, 'GatewayTimeoutError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { RequestResponse } from '../utils/request'
import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
Expand All @@ -17,3 +19,4 @@ export class InteractiveTransactionError extends DataProxyAPIError {
super(message, setRetryable(info, false))
}
}
setClassName(InteractiveTransactionError, 'InteractiveTransactionError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { DataProxyErrorInfo } from './DataProxyError'
import { DataProxyError } from './DataProxyError'
import { setRetryable } from './utils/setRetryable'
Expand All @@ -11,3 +13,4 @@ export class InvalidDatasourceError extends DataProxyError {
super(message, setRetryable(info, false))
}
}
setClassName(InvalidDatasourceError, 'InvalidDatasourceError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { RequestResponse } from '../utils/request'
import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
Expand All @@ -22,3 +24,4 @@ export class InvalidRequestError extends DataProxyAPIError {
super(message, setRetryable(info, false))
}
}
setClassName(InvalidRequestError, 'InvalidRequestError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { DataProxyErrorInfo } from './DataProxyError'
import { DataProxyError } from './DataProxyError'
import { setRetryable } from './utils/setRetryable'
Expand All @@ -12,3 +14,4 @@ export class RequestError extends DataProxyError {
super(`Cannot fetch data from service:\n${message}`, setRetryable(info, true))
}
}
setClassName(RequestError, 'RequestError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { RequestResponse } from '../utils/request'
import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
Expand All @@ -17,3 +19,4 @@ export class NotFoundError extends DataProxyAPIError {
super(message, setRetryable(info, false))
}
}
setClassName(NotFoundError, 'NotFoundError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { DataProxyErrorInfo } from './DataProxyError'
import { DataProxyError } from './DataProxyError'
import { setRetryable } from './utils/setRetryable'
Expand All @@ -12,3 +14,4 @@ export class NotImplementedYetError extends DataProxyError {
super(message, setRetryable(info, false))
}
}
setClassName(NotImplementedYetError, 'NotImplementedYetError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
import { setRetryable } from './utils/setRetryable'
Expand All @@ -12,3 +14,4 @@ export class SchemaMissingError extends DataProxyAPIError {
super('Schema needs to be uploaded', setRetryable(info, true))
}
}
setClassName(SchemaMissingError, 'SchemaMissingError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
import { setRetryable } from './utils/setRetryable'
Expand All @@ -16,3 +18,4 @@ export class ServerError extends DataProxyAPIError {
this.logs = logs
}
}
setClassName(ServerError, 'ServerError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
import { setRetryable } from './utils/setRetryable'
Expand All @@ -14,3 +16,4 @@ export class UnauthorizedError extends DataProxyAPIError {
super(message, setRetryable(info, false))
}
}
setClassName(UnauthorizedError, 'UnauthorizedError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import type { DataProxyAPIErrorInfo } from './DataProxyAPIError'
import { DataProxyAPIError } from './DataProxyAPIError'
import { setRetryable } from './utils/setRetryable'
Expand All @@ -14,3 +16,4 @@ export class UsageExceededError extends DataProxyAPIError {
super(message, setRetryable(info, true))
}
}
setClassName(UsageExceededError, 'UsageExceededError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

export class PrismaClientInitializationError extends Error {
clientVersion: string
errorCode?: string
Expand All @@ -13,3 +15,5 @@ export class PrismaClientInitializationError extends Error {
return 'PrismaClientInitializationError'
}
}

setClassName(PrismaClientInitializationError, 'PrismaClientInitializationError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import { ErrorWithBatchIndex } from './ErrorWithBatchIndex'

type KnownErrorParams = {
Expand Down Expand Up @@ -29,3 +31,5 @@ export class PrismaClientKnownRequestError extends Error implements ErrorWithBat
return 'PrismaClientKnownRequestError'
}
}

setClassName(PrismaClientKnownRequestError, 'PrismaClientKnownRequestError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import { PrismaClientRustErrorArgs } from '../engines/common/types/PrismaClientRustErrorArgs'
import { getBacktrace, isPanic } from '../engines/common/utils/log'

Expand All @@ -18,10 +20,12 @@ export class PrismaClientRustError extends Error {
}

get [Symbol.toStringTag]() {
return 'PrismaClientRustPanicError'
return 'PrismaClientRustError'
}

public isPanic(): boolean {
return this._isPanic
}
}

setClassName(PrismaClientRustError, 'PrismaClientRustError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

export class PrismaClientRustPanicError extends Error {
clientVersion: string

Expand All @@ -10,3 +12,5 @@ export class PrismaClientRustPanicError extends Error {
return 'PrismaClientRustPanicError'
}
}

setClassName(PrismaClientRustPanicError, 'PrismaClientRustPanicError')
@@ -1,3 +1,5 @@
import { setClassName } from '@prisma/internals'

import { ErrorWithBatchIndex } from './ErrorWithBatchIndex'

type UnknownErrorParams = {
Expand All @@ -11,6 +13,7 @@ export class PrismaClientUnknownRequestError extends Error implements ErrorWithB

constructor(message: string, { clientVersion, batchRequestIdx }: UnknownErrorParams) {
super(message)
this.name = 'PrismaClientUnknownRequestError'

this.clientVersion = clientVersion
Object.defineProperty(this, 'batchRequestIdx', {
Expand All @@ -23,3 +26,5 @@ export class PrismaClientUnknownRequestError extends Error implements ErrorWithB
return 'PrismaClientUnknownRequestError'
}
}

setClassName(PrismaClientUnknownRequestError, 'PrismaClientUnknownRequestError')
17 changes: 17 additions & 0 deletions packages/client/src/runtime/object-enums.ts
Expand Up @@ -49,10 +49,13 @@ class NullTypesEnumValue extends ObjectEnumValue {
}

class DbNull extends NullTypesEnumValue {}
setClassName(DbNull, 'DbNull')

class JsonNull extends NullTypesEnumValue {}
setClassName(JsonNull, 'JsonNull')

class AnyNull extends NullTypesEnumValue {}
setClassName(AnyNull, 'AnyNull')

export const objectEnumValues = {
classes: {
Expand All @@ -66,3 +69,17 @@ export const objectEnumValues = {
AnyNull: new AnyNull(secret),
},
}

/**
* See helper in @internals package. Can not be used here
* because importing internal breaks browser build.
*
* @param classObject
* @param name
*/
export function setClassName(classObject: Function, name: string) {
Object.defineProperty(classObject, 'name', {
value: name,
configurable: true,
})
}
4 changes: 4 additions & 0 deletions packages/client/src/runtime/query.ts
@@ -1,3 +1,4 @@
import { setClassName } from '@prisma/internals'
import Decimal from 'decimal.js'
import indent from 'indent-string'
import { bold, dim, green, red, white } from 'kleur/colors'
Expand Down Expand Up @@ -453,14 +454,17 @@ export class PrismaClientValidationError extends Error {
return 'PrismaClientValidationError'
}
}
setClassName(PrismaClientValidationError, 'PrismaClientValidationError')
export class PrismaClientConstructorValidationError extends Error {
constructor(message: string) {
super(message + `\nRead more at https://pris.ly/d/client-constructor`)
this.name = 'PrismaClientConstructorValidationError'
}
get [Symbol.toStringTag]() {
return 'PrismaClientConstructorValidationError'
}
}
setClassName(PrismaClientConstructorValidationError, 'PrismaClientConstructorValidationError')

export interface FieldArgs {
name: string
Expand Down
3 changes: 2 additions & 1 deletion packages/client/src/runtime/utils/rejectOnNotFound.ts
@@ -1,4 +1,4 @@
import { isError } from '@prisma/internals'
import { isError, setClassName } from '@prisma/internals'

import { PrismaClientKnownRequestError } from '../core/errors/PrismaClientKnownRequestError'
import { Action } from '../core/types/JsApi'
Expand All @@ -21,6 +21,7 @@ export class NotFoundError extends PrismaClientKnownRequestError {
this.name = 'NotFoundError'
}
}
setClassName(NotFoundError, 'NotFoundError')

/**
* Gets the configured rejection action
Expand Down
4 changes: 2 additions & 2 deletions packages/internals/src/__tests__/handlePanic.test.ts
Expand Up @@ -105,9 +105,9 @@ describe('handlePanic', () => {
})
} catch (error) {
error.schemaPath = 'Some Schema Path'
expect(error).toMatchInlineSnapshot(`[Error: Some error message!]`)
expect(error).toMatchInlineSnapshot(`[RustPanic: Some error message!]`)
expect(JSON.stringify(error)).toMatchInlineSnapshot(
`"{"__typename":"RustPanic","rustStack":"","area":"LIFT_CLI","schemaPath":"Some Schema Path"}"`,
`"{"__typename":"RustPanic","name":"RustPanic","rustStack":"","area":"LIFT_CLI","schemaPath":"Some Schema Path"}"`,
)
}
})
Expand Down
1 change: 1 addition & 0 deletions packages/internals/src/cli/Help.ts
Expand Up @@ -14,6 +14,7 @@ export function unknownCommand(helpTemplate: string, cmd: string): HelpError {
export class HelpError extends Error {
constructor(msg: string) {
super(msg)
this.name = 'HelpError'
// setPrototypeOf is needed for custom errors to work
Object.setPrototypeOf(this, HelpError.prototype)
}
Expand Down
1 change: 1 addition & 0 deletions packages/internals/src/engine-commands/getConfig.ts
Expand Up @@ -47,6 +47,7 @@ ${detailsHeader} ${message}`
[Context: getConfig]`

super(addVersionDetailsToErrorMessage(errorMessageWithContext))
this.name = 'GetConfigError'
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/internals/src/engine-commands/getDmmf.ts
Expand Up @@ -48,6 +48,7 @@ ${detailsHeader} ${message}`
[Context: getDmmf]`

super(addVersionDetailsToErrorMessage(errorMessageWithContext))
this.name = 'GetDmmfError'
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/internals/src/engine-commands/validate.ts
Expand Up @@ -34,6 +34,7 @@ ${detailsHeader} ${message}`
[Context: validate]`

super(addVersionDetailsToErrorMessage(errorMessageWithContext))
this.name = 'ValidateError'
}
}

Expand Down

0 comments on commit 5a0f107

Please sign in to comment.