-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow tsConfigRaw to be passed so that decorators can be enabled.
- Loading branch information
Drew Loomer
committed
Jun 28, 2023
1 parent
daa5847
commit 8c04a59
Showing
3 changed files
with
68 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,91 @@ | ||
import { extname } from 'path' | ||
import { extname } from "path"; | ||
|
||
import { Config } from '@jest/types' | ||
import { TransformOptions as JestTransformOptions, Transformer } from '@jest/transform' | ||
import { Format, Loader, TransformOptions, transformSync } from 'esbuild' | ||
import { Config } from "@jest/types"; | ||
import { | ||
TransformOptions as JestTransformOptions, | ||
Transformer, | ||
} from "@jest/transform"; | ||
import { Format, Loader, TransformOptions, transformSync } from "esbuild"; | ||
|
||
import { Options } from './options' | ||
import { getExt, loaders } from './utils' | ||
import { Options } from "./options"; | ||
import { getExt, loaders } from "./utils"; | ||
|
||
const createTransformer = (options?: Options) => ({ | ||
process(content: string, | ||
filename: string, | ||
config: Config.ProjectConfig, | ||
process( | ||
content: string, | ||
filename: string, | ||
config: Config.ProjectConfig, | ||
opts?: JestTransformOptions | ||
) { | ||
const sources = { code: content } | ||
const ext = getExt(filename), extName = extname(filename).slice(1) | ||
const sources = { code: content }; | ||
const ext = getExt(filename), | ||
extName = extname(filename).slice(1); | ||
|
||
const enableSourcemaps = options?.sourcemap || false | ||
const loader = (options?.loaders && options?.loaders[ext] | ||
const enableSourcemaps = options?.sourcemap || false; | ||
const loader = (options?.loaders && options?.loaders[ext] | ||
? options.loaders[ext] | ||
: loaders.includes(extName) ? extName: 'text' | ||
) as Loader | ||
const sourcemaps: Partial<TransformOptions> = enableSourcemaps | ||
? { sourcemap: true, sourcesContent: false, sourcefile: filename } | ||
: {} | ||
: loaders.includes(extName) | ||
? extName | ||
: "text") as Loader; | ||
const sourcemaps: Partial<TransformOptions> = enableSourcemaps | ||
? { sourcemap: true, sourcesContent: false, sourcefile: filename } | ||
: {}; | ||
|
||
/// this logic or code from | ||
const tsconfigRaw = options?.tsconfigRaw | ||
? { tsconfigRaw: options.tsconfigRaw } | ||
: {}; | ||
|
||
/// this logic or code from | ||
/// https://github.com/threepointone/esjest-transform/blob/main/src/index.js | ||
/// this will support the jest.mock | ||
/// https://github.com/aelbore/esbuild-jest/issues/12 | ||
/// TODO: transform the jest.mock to a function using babel traverse/parse then hoist it | ||
if (sources.code.indexOf("ock(") >= 0 || opts?.instrument) { | ||
const source = require('./transformer').babelTransform({ | ||
const source = require("./transformer").babelTransform({ | ||
sourceText: content, | ||
sourcePath: filename, | ||
config, | ||
options: opts | ||
}) | ||
sources.code = source | ||
options: opts, | ||
}); | ||
sources.code = source; | ||
} | ||
|
||
const result = transformSync(sources.code, { | ||
loader, | ||
format: options?.format as Format || 'cjs', | ||
target: options?.target || 'es2018', | ||
...(options?.jsxFactory ? { jsxFactory: options.jsxFactory }: {}), | ||
...(options?.jsxFragment ? { jsxFragment: options.jsxFragment }: {}), | ||
...sourcemaps | ||
}) | ||
|
||
format: (options?.format as Format) || "cjs", | ||
target: options?.target || "es2018", | ||
...(options?.jsxFactory ? { jsxFactory: options.jsxFactory } : {}), | ||
...(options?.jsxFragment ? { jsxFragment: options.jsxFragment } : {}), | ||
...sourcemaps, | ||
...tsconfigRaw, | ||
}); | ||
|
||
let { map, code } = result; | ||
if (enableSourcemaps) { | ||
map = { | ||
...JSON.parse(result.map), | ||
sourcesContent: null, | ||
} | ||
}; | ||
|
||
// Append the inline sourcemap manually to ensure the "sourcesContent" | ||
// is null. Otherwise, breakpoints won't pause within the actual source. | ||
code = code + '\n//# sourceMappingURL=data:application/json;base64,' + Buffer.from(JSON.stringify(map)).toString('base64') | ||
code = | ||
code + | ||
"\n//# sourceMappingURL=data:application/json;base64," + | ||
Buffer.from(JSON.stringify(map)).toString("base64"); | ||
} else { | ||
map = null | ||
map = null; | ||
} | ||
|
||
return { code, map } | ||
} | ||
}) | ||
|
||
const transformer: Pick<Transformer, 'canInstrument' | 'createTransformer'> = { | ||
return { code, map }; | ||
}, | ||
}); | ||
|
||
const transformer: Pick<Transformer, "canInstrument" | "createTransformer"> = { | ||
canInstrument: true, | ||
createTransformer | ||
} | ||
createTransformer, | ||
}; | ||
|
||
export * from './options' | ||
export * from "./options"; | ||
|
||
export default transformer | ||
export default transformer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { Loader } from 'esbuild' | ||
import type { Loader, TransformOptions } from "esbuild"; | ||
|
||
export interface Options { | ||
jsxFactory?: string | ||
jsxFragment?: string | ||
sourcemap?: boolean | 'inline' | 'external' | ||
jsxFactory?: string; | ||
jsxFragment?: string; | ||
sourcemap?: boolean | "inline" | "external"; | ||
loaders?: { | ||
[ext: string]: Loader | ||
}, | ||
target?: string | ||
format?: string | ||
} | ||
[ext: string]: Loader; | ||
}; | ||
target?: string; | ||
format?: string; | ||
tsconfigRaw?: TransformOptions["tsconfigRaw"]; | ||
} |