Skip to content

Commit 20258de

Browse files
authoredDec 7, 2021
fix: make esbuild as optional peer dependency (#3129)
1 parent fc1c137 commit 20258de

File tree

9 files changed

+485
-42
lines changed

9 files changed

+485
-42
lines changed
 

‎e2e/__tests__/ast-transformers.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ describe('hoist-jest', () => {
6363
})
6464

6565
describe('transformer-in-ts', () => {
66-
const TRANSFORMER_IN_TS_DIR_NAME = `${AST_TRANSFORMERS_DIR_NAME}/transformer-in-ts`
66+
const DIR = path.join(__dirname, '..', AST_TRANSFORMERS_DIR_NAME, 'transformer-in-ts')
6767

68-
test(`successfully runs the tests inside ${TRANSFORMER_IN_TS_DIR_NAME}`, () => {
69-
const { json } = runWithJson(TRANSFORMER_IN_TS_DIR_NAME)
68+
beforeAll(() => {
69+
runNpmInstall(DIR)
70+
})
71+
72+
test(`successfully runs the tests inside ${DIR}`, () => {
73+
const { json } = runWithJson(DIR)
7074

7175
expect(json.success).toBe(true)
7276
})

‎e2e/ast-transformers/transformer-in-ts/package-lock.json

+372
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎e2e/ast-transformers/transformer-in-ts/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"dependencies": {
3+
"esbuild": "~0.14.2"
4+
},
25
"jest": {
36
"globals": {
47
"ts-jest": {

‎package-lock.json

+56-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
"homepage": "https://kulshekhar.github.io/ts-jest",
5353
"dependencies": {
5454
"bs-logger": "0.x",
55-
"esbuild": "~0.14.0",
5655
"fast-json-stable-stringify": "2.x",
5756
"jest-util": "^27.0.0",
5857
"json5": "2.x",
@@ -65,18 +64,22 @@
6564
"@babel/core": ">=7.0.0-beta.0 <8",
6665
"@types/jest": "^27.0.0",
6766
"babel-jest": ">=27.0.0 <28",
67+
"esbuild": "~0.14.0",
6868
"jest": "^27.0.0",
6969
"typescript": ">=3.8 <5.0"
7070
},
7171
"peerDependenciesMeta": {
72-
"babel-jest": {
73-
"optional": true
74-
},
7572
"@babel/core": {
7673
"optional": true
7774
},
7875
"@types/jest": {
7976
"optional": true
77+
},
78+
"babel-jest": {
79+
"optional": true
80+
},
81+
"esbuild": {
82+
"optional": true
8083
}
8184
},
8285
"husky": {
@@ -111,6 +114,7 @@
111114
"@typescript-eslint/parser": "^5.3.0",
112115
"conventional-changelog-cli": "2.x",
113116
"cross-spawn": "latest",
117+
"esbuild": "~0.14.2",
114118
"eslint": "8.4.0",
115119
"eslint-config-prettier": "latest",
116120
"eslint-plugin-import": "latest",

‎src/config/config-set.ts

+29-17
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import Module from 'module'
1313
import { dirname, extname, isAbsolute, join, normalize, resolve } from 'path'
1414

1515
import { LogContexts, Logger } from 'bs-logger'
16-
import { transformSync } from 'esbuild'
1716
import { globsToMatcher } from 'jest-util'
1817
import json5 from 'json5'
19-
import type { CompilerOptions, Diagnostic, FormatDiagnosticsHost, ParsedCommandLine } from 'typescript'
18+
import type * as ts from 'typescript'
2019

2120
import { DEFAULT_JEST_TEST_MATCH, JS_JSX_EXTENSIONS } from '../constants'
2221
import type { RawCompilerOptions } from '../raw-compiler-options'
@@ -31,7 +30,8 @@ import type {
3130
TsJestGlobalOptions,
3231
TTypeScript,
3332
} from '../types'
34-
import { stringify, rootLogger } from '../utils'
33+
import { TsCompilerInstance } from '../types'
34+
import { rootLogger, stringify } from '../utils'
3535
import { backportJestConfig } from '../utils/backports'
3636
import { importer } from '../utils/importer'
3737
import { Errors, ImportReasons, interpolate } from '../utils/messages'
@@ -119,7 +119,7 @@ export class ConfigSet {
119119
cacheSuffix!: string
120120
tsCacheDir: string | undefined
121121
// eslint-disable-next-line @typescript-eslint/no-explicit-any
122-
parsedTsConfig!: ParsedCommandLine | Record<string, any>
122+
parsedTsConfig!: ts.ParsedCommandLine | Record<string, any>
123123
resolvedTransformers: TsJestAstTransformer = {
124124
before: [],
125125
after: [],
@@ -161,7 +161,7 @@ export class ConfigSet {
161161
/**
162162
* @internal
163163
*/
164-
private readonly _overriddenCompilerOptions: Partial<CompilerOptions> = {
164+
private readonly _overriddenCompilerOptions: Partial<ts.CompilerOptions> = {
165165
inlineSourceMap: false,
166166
// we don't want to create declaration files
167167
declaration: false,
@@ -316,13 +316,22 @@ export class ConfigSet {
316316
const { astTransformers } = options
317317
if (astTransformers) {
318318
const resolveTransformerFunc = (transformerPath: string) => {
319-
let transformerFunc
319+
let transformerFunc: {
320+
version: number
321+
name: string
322+
factory: (
323+
compilerInstance: TsCompilerInstance,
324+
options?: Record<string, unknown>,
325+
) => (ctx: ts.TransformationContext) => ts.Transformer<ts.SourceFile>
326+
}
320327
if (extname(transformerPath) === '.ts') {
321-
const compiledTransformer = transformSync(readFileSync(transformerPath, 'utf-8'), {
322-
loader: 'ts',
323-
format: 'cjs',
324-
target: 'es2015',
325-
}).code
328+
const compiledTransformer = importer
329+
.esBuild(ImportReasons.EsBuild)
330+
.transformSync(readFileSync(transformerPath, 'utf-8'), {
331+
loader: 'ts',
332+
format: 'cjs',
333+
target: 'es2015',
334+
}).code
326335
transformerFunc = requireFromString(compiledTransformer, transformerPath.replace('.ts', '.js'))
327336
} else {
328337
transformerFunc = require(transformerPath)
@@ -423,8 +432,11 @@ export class ConfigSet {
423432
/**
424433
* @internal
425434
*/
426-
private _getAndResolveTsConfig(compilerOptions?: RawCompilerOptions, resolvedConfigFile?: string): ParsedCommandLine {
427-
const result = this._resolveTsConfig(compilerOptions, resolvedConfigFile) as ParsedCommandLine
435+
private _getAndResolveTsConfig(
436+
compilerOptions?: RawCompilerOptions,
437+
resolvedConfigFile?: string,
438+
): ts.ParsedCommandLine {
439+
const result = this._resolveTsConfig(compilerOptions, resolvedConfigFile) as ts.ParsedCommandLine
428440
const { _overriddenCompilerOptions: forcedOptions } = this
429441
const finalOptions = result.options
430442
// Target ES2015 output by default (instead of ES3).
@@ -519,7 +531,7 @@ export class ConfigSet {
519531
// eslint-disable-next-line @typescript-eslint/no-explicit-any
520532
protected _resolveTsConfig(compilerOptions?: RawCompilerOptions, resolvedConfigFile?: string): Record<string, any>
521533
// eslint-disable-next-line no-dupe-class-members
522-
protected _resolveTsConfig(compilerOptions?: RawCompilerOptions, resolvedConfigFile?: string): ParsedCommandLine {
534+
protected _resolveTsConfig(compilerOptions?: RawCompilerOptions, resolvedConfigFile?: string): ts.ParsedCommandLine {
523535
let config = { compilerOptions: Object.create(null) }
524536
let basePath = normalizeSlashes(this.rootDir)
525537
const ts = this.compilerModule
@@ -559,7 +571,7 @@ export class ConfigSet {
559571
return this._stringifyContentRegExp ? this._stringifyContentRegExp.test(filePath) : false
560572
}
561573

562-
raiseDiagnostics(diagnostics: Diagnostic[], filePath?: string, logger?: Logger): void {
574+
raiseDiagnostics(diagnostics: ts.Diagnostic[], filePath?: string, logger?: Logger): void {
563575
const { ignoreCodes } = this._diagnostics
564576
const { DiagnosticCategory } = this.compilerModule
565577
const filteredDiagnostics =
@@ -594,12 +606,12 @@ export class ConfigSet {
594606
/**
595607
* @internal
596608
*/
597-
private _createTsError(diagnostics: readonly Diagnostic[]): TSError {
609+
private _createTsError(diagnostics: readonly ts.Diagnostic[]): TSError {
598610
const formatDiagnostics = this._diagnostics.pretty
599611
? this.compilerModule.formatDiagnosticsWithColorAndContext
600612
: this.compilerModule.formatDiagnostics
601613
/* istanbul ignore next (not possible to cover) */
602-
const diagnosticHost: FormatDiagnosticsHost = {
614+
const diagnosticHost: ts.FormatDiagnosticsHost = {
603615
getNewLine: () => '\n',
604616
getCurrentDirectory: () => this.cwd,
605617
getCanonicalFileName: (path: string) => path,

‎src/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Transformer, TransformOptions } from '@jest/transform'
22
import type { Config } from '@jest/types'
33
import type * as _babel from 'babel__core'
4+
import type * as _esbuild from 'esbuild'
45
import type * as _ts from 'typescript'
56

67
import type { ConfigSet } from './config'
@@ -26,6 +27,7 @@ declare module '@jest/types' {
2627
*/
2728
export type TBabelCore = typeof _babel
2829
export type TTypeScript = typeof _ts
30+
export type TEsBuild = typeof _esbuild
2931
/**
3032
* @internal
3133
*/

‎src/utils/importer.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { TBabelCore, TBabelJest, TTypeScript } from '../types'
2+
import type { TEsBuild } from '../types'
23

34
import { rootLogger } from './logger'
45
import { Memoize } from './memoize'
@@ -59,6 +60,10 @@ export class Importer {
5960
return this._import(why, which)
6061
}
6162

63+
esBuild(why: ImportReasons): TEsBuild {
64+
return this._import(why, 'esbuild')
65+
}
66+
6267
@Memoize((...args: string[]) => args.join(':'))
6368
tryThese(moduleName: string, ...fallbacks: string[]): RequireResult<true> | undefined {
6469
let name: string
@@ -119,12 +124,12 @@ export class Importer {
119124
return unpatched
120125
}
121126

122-
protected _import(
127+
protected _import<T>(
123128
why: string,
124129
moduleName: string,
125130
{ alternatives = [], installTip = moduleName }: ImportOptions = {},
126131
// eslint-disable-next-line @typescript-eslint/no-explicit-any
127-
): any {
132+
): T {
128133
// try to load any of the alternative after trying main one
129134
const res = this.tryThese(moduleName, ...alternatives)
130135
// if we could load one, return it

‎src/utils/messages.ts

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const enum Deprecations {
4747
export const enum ImportReasons {
4848
TsJest = 'Using "ts-jest" requires this package to be installed.',
4949
BabelJest = 'Using "babel-jest" requires this package to be installed.',
50+
EsBuild = 'Using "esbuild" requires this package to be installed.',
5051
}
5152

5253
/**

0 commit comments

Comments
 (0)
Please sign in to comment.