Skip to content

Commit

Permalink
fix: do not transform internally imported files
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Apr 27, 2020
1 parent faa5267 commit 6acbbee
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

- `[jest-resolve]` Update `resolve` to a version using native `realpath`, which is faster than the default JS implementation ([#9872](https://github.com/facebook/jest/pull/9872))
- `[jest-resolve]` Pass custom cached `realpath` function to `resolve` ([#9873](https://github.com/facebook/jest/pull/9873))
- `[jest-runtime]` Do not pass files required internally through transformation when loading them

## 25.4.0

Expand Down
59 changes: 37 additions & 22 deletions packages/jest-runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ class Runtime {
return core;
}

const transformedFile = this.transformFile(modulePath, {
const transformedCode = this.transformFile(modulePath, {
isInternalModule: false,
supportsDynamicImport: true,
supportsStaticESM: true,
});

const module = new SourceTextModule(transformedFile.code, {
const module = new SourceTextModule(transformedCode, {
context,
identifier: modulePath,
importModuleDynamically: this.linkModules.bind(this),
Expand Down Expand Up @@ -471,7 +471,7 @@ class Runtime {
const manualMock =
moduleName && this._resolver.getMockModule(from, moduleName);
if (
(!options || !options.isInternalModule) &&
!options?.isInternalModule &&
!isRequireActual &&
!moduleResource &&
manualMock &&
Expand All @@ -491,7 +491,9 @@ class Runtime {

let moduleRegistry;

if (!options || !options.isInternalModule) {
if (options?.isInternalModule) {
moduleRegistry = this._internalModuleRegistry;
} else {
if (
this._moduleRegistry.get(modulePath) ||
!this._isolatedModuleRegistry
Expand All @@ -500,8 +502,6 @@ class Runtime {
} else {
moduleRegistry = this._isolatedModuleRegistry;
}
} else {
moduleRegistry = this._internalModuleRegistry;
}

const module = moduleRegistry.get(modulePath);
Expand Down Expand Up @@ -641,7 +641,7 @@ class Runtime {
moduleRegistry: ModuleRegistry,
) {
if (path.extname(modulePath) === '.json') {
const text = stripBOM(fs.readFileSync(modulePath, 'utf8'));
const text = stripBOM(this.readFile(modulePath));

const transformedFile = this._scriptTransformer.transformJson(
modulePath,
Expand Down Expand Up @@ -937,7 +937,7 @@ class Runtime {
value: this._createRequireImplementation(localModule, options),
});

const transformedFile = this.transformFile(filename, options);
const transformedCode = this.transformFile(filename, options);

let compiledFunction: ModuleWrapper | null = null;

Expand All @@ -949,7 +949,7 @@ class Runtime {
if (typeof compileFunction === 'function') {
try {
compiledFunction = compileFunction(
transformedFile.code,
transformedCode,
this.constructInjectedModuleParameters(),
{
filename,
Expand All @@ -960,10 +960,7 @@ class Runtime {
throw handlePotentialSyntaxError(e);
}
} else {
const script = this.createScriptFromCode(
transformedFile.code,
filename,
);
const script = this.createScriptFromCode(transformedCode, filename);

const runScript = script.runInContext(
vmContext,
Expand All @@ -977,7 +974,7 @@ class Runtime {
}
}
} else {
const script = this.createScriptFromCode(transformedFile.code, filename);
const script = this.createScriptFromCode(transformedCode, filename);

const runScript = this._environment.runScript<RunScriptEvalResult>(
script,
Expand Down Expand Up @@ -1030,22 +1027,28 @@ class Runtime {
this._currentlyExecutingModulePath = lastExecutingModulePath;
}

private transformFile(filename: string, options?: InternalModuleOptions) {
private transformFile(
filename: string,
options?: InternalModuleOptions,
): string {
const source = this.readFile(filename);

if (options?.isInternalModule) {
return source;
}

const transformedFile = this._scriptTransformer.transform(
filename,
this._getFullTransformationOptions(options),
this._cacheFS[filename],
source,
);

// we only care about non-internal modules
if (!options || !options.isInternalModule) {
this._fileTransforms.set(filename, transformedFile);
}
this._fileTransforms.set(filename, transformedFile);

if (transformedFile.sourceMapPath) {
this._sourceMapRegistry[filename] = transformedFile.sourceMapPath;
}
return transformedFile;
return transformedFile.code;
}

private createScriptFromCode(scriptSource: string, filename: string) {
Expand Down Expand Up @@ -1275,7 +1278,7 @@ class Runtime {
resolve.paths = (moduleName: string) =>
this._requireResolvePaths(from.filename, moduleName);

const moduleRequire = (options && options.isInternalModule
const moduleRequire = (options?.isInternalModule
? (moduleName: string) =>
this.requireInternalModule(from.filename, moduleName)
: this.requireModuleOrMock.bind(
Expand Down Expand Up @@ -1607,6 +1610,18 @@ class Runtime {
xtest: this._environment.global.xtest,
};
}

private readFile(filename: Config.Path): string {
let source = this._cacheFS[filename];

if (!source) {
source = fs.readFileSync(filename, 'utf8');

this._cacheFS[filename] = source;
}

return source;
}
}

function invariant(condition: unknown, message?: string): asserts condition {
Expand Down

0 comments on commit 6acbbee

Please sign in to comment.