-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: pass ESM options to transformers #9597
Merged
+76
−17
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -90,6 +90,8 @@ export default class ScriptTransformer { | |
fileData: string, | ||
filename: Config.Path, | ||
instrument: boolean, | ||
supportsDynamicImport: boolean, | ||
supportsStaticESM: boolean, | ||
): string { | ||
const configString = this._cache.configString; | ||
const transformer = this._getTransformer(filename); | ||
|
@@ -101,8 +103,8 @@ export default class ScriptTransformer { | |
config: this._config, | ||
instrument, | ||
rootDir: this._config.rootDir, | ||
supportsDynamicImport: false, | ||
supportsStaticESM: false, | ||
supportsDynamicImport, | ||
supportsStaticESM, | ||
}), | ||
) | ||
.update(CACHE_VERSION) | ||
|
@@ -122,13 +124,21 @@ export default class ScriptTransformer { | |
filename: Config.Path, | ||
content: string, | ||
instrument: boolean, | ||
supportsDynamicImport: boolean, | ||
supportsStaticESM: boolean, | ||
): Config.Path { | ||
const baseCacheDir = HasteMap.getCacheFilePath( | ||
this._config.cacheDirectory, | ||
'jest-transform-cache-' + this._config.name, | ||
VERSION, | ||
); | ||
const cacheKey = this._getCacheKey(content, filename, instrument); | ||
const cacheKey = this._getCacheKey( | ||
content, | ||
filename, | ||
instrument, | ||
supportsDynamicImport, | ||
supportsStaticESM, | ||
); | ||
// Create sub folders based on the cacheKey to avoid creating one | ||
// directory with many files. | ||
const cacheDir = path.join(baseCacheDir, cacheKey[0] + cacheKey[1]); | ||
|
@@ -193,13 +203,19 @@ export default class ScriptTransformer { | |
return transform; | ||
} | ||
|
||
private _instrumentFile(filename: Config.Path, content: string): string { | ||
private _instrumentFile( | ||
filename: Config.Path, | ||
content: string, | ||
supportsDynamicImport: boolean, | ||
supportsStaticESM: boolean, | ||
): string { | ||
const result = babelTransform(content, { | ||
auxiliaryCommentBefore: ' istanbul ignore next ', | ||
babelrc: false, | ||
caller: { | ||
name: '@jest/transform', | ||
supportsStaticESM: false, | ||
supportsDynamicImport, | ||
supportsStaticESM, | ||
}, | ||
configFile: false, | ||
filename, | ||
|
@@ -243,14 +259,23 @@ export default class ScriptTransformer { | |
this._getTransformer(filepath); | ||
} | ||
|
||
// TODO: replace third argument with TransformOptions in Jest 26 | ||
transformSource( | ||
filepath: Config.Path, | ||
content: string, | ||
instrument: boolean, | ||
supportsDynamicImport = false, | ||
supportsStaticESM = false, | ||
): TransformResult { | ||
const filename = this._getRealPath(filepath); | ||
const transform = this._getTransformer(filename); | ||
const cacheFilePath = this._getFileCachePath(filename, content, instrument); | ||
const cacheFilePath = this._getFileCachePath( | ||
filename, | ||
content, | ||
instrument, | ||
supportsDynamicImport, | ||
supportsStaticESM, | ||
); | ||
let sourceMapPath: Config.Path | null = cacheFilePath + '.map'; | ||
// Ignore cache if `config.cache` is set (--no-cache) | ||
let code = this._config.cache ? readCodeCacheFile(cacheFilePath) : null; | ||
|
@@ -287,8 +312,8 @@ export default class ScriptTransformer { | |
if (transform && shouldCallTransform) { | ||
const processed = transform.process(content, filename, this._config, { | ||
instrument, | ||
supportsDynamicImport: false, | ||
supportsStaticESM: false, | ||
supportsDynamicImport, | ||
supportsStaticESM, | ||
}); | ||
|
||
if (typeof processed === 'string') { | ||
|
@@ -323,7 +348,12 @@ export default class ScriptTransformer { | |
} | ||
|
||
if (!transformWillInstrument && instrument) { | ||
code = this._instrumentFile(filename, transformed.code); | ||
code = this._instrumentFile( | ||
filename, | ||
transformed.code, | ||
supportsDynamicImport, | ||
supportsStaticESM, | ||
); | ||
} else { | ||
code = transformed.code; | ||
} | ||
|
@@ -350,12 +380,16 @@ export default class ScriptTransformer { | |
|
||
private _transformAndBuildScript( | ||
filename: Config.Path, | ||
options: Options | null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems to have been a bug, it was never passed |
||
options: Options, | ||
instrument: boolean, | ||
fileSource?: string, | ||
): TransformResult { | ||
const isInternalModule = !!(options && options.isInternalModule); | ||
const isCoreModule = !!(options && options.isCoreModule); | ||
const { | ||
isCoreModule, | ||
isInternalModule, | ||
supportsDynamicImport, | ||
supportsStaticESM, | ||
} = options; | ||
const content = stripShebang( | ||
fileSource || fs.readFileSync(filename, 'utf8'), | ||
); | ||
|
@@ -375,6 +409,8 @@ export default class ScriptTransformer { | |
filename, | ||
content, | ||
instrument, | ||
supportsDynamicImport, | ||
supportsStaticESM, | ||
); | ||
|
||
code = transformedSource.code; | ||
|
@@ -431,8 +467,12 @@ export default class ScriptTransformer { | |
options: Options, | ||
fileSource: string, | ||
): string { | ||
const isInternalModule = options.isInternalModule; | ||
const isCoreModule = options.isCoreModule; | ||
const { | ||
isCoreModule, | ||
isInternalModule, | ||
supportsDynamicImport, | ||
supportsStaticESM, | ||
} = options; | ||
const willTransform = | ||
!isInternalModule && !isCoreModule && this.shouldTransform(filename); | ||
|
||
|
@@ -441,6 +481,8 @@ export default class ScriptTransformer { | |
filename, | ||
fileSource, | ||
false, | ||
supportsDynamicImport, | ||
supportsStaticESM, | ||
); | ||
return transformedJsonSource; | ||
} | ||
|
@@ -469,7 +511,11 @@ export default class ScriptTransformer { | |
(code, filename) => { | ||
try { | ||
transforming = true; | ||
return this.transformSource(filename, code, false).code || code; | ||
return ( | ||
// we might wanna do `supportsDynamicImport` at some point | ||
this.transformSource(filename, code, false, false, false).code || | ||
code | ||
); | ||
} finally { | ||
transforming = false; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made them non-optional for all private methods and default
false
for the public onesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should ideally use options objects, as 2-3 boolean flag inputs are quite confusing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally agree, but IIRC functions with more arguments are optimized better than the ones with object arguments, and this is kinda hot path
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
potentially we could add separate functions? the es module path can be async (or rather, always is) as well, so that might tie nicely into #9504. Then we could ditch the
supportsDynamicImport
option since that works for both esm and cjs (and can be async) and just vary on the static version