Skip to content

Commit

Permalink
fix: esm loader fix (#8146)
Browse files Browse the repository at this point in the history
<!--
Thank you for your contribution.

Before making a PR, please read our contributing guidelines at

https://github.com/DevExpress/testcafe/blob/master/CONTRIBUTING.md#code-contribution

We recommend creating a *draft* PR, so that you can mark it as 'ready
for review' when you are done.
-->

## Purpose
Starting from Node version 20 Hooks like `load` run in a separate
thread, isolated from the main thread.
https://nodejs.org/docs/latest-v20.x/api/module.html#hooks.

Our current implementation relies on main thread in
`getTestFileCompilers`.
Because the `load` hook is now running in a separate thread the
`testFileCompilers` was never initialized before, and
`getTestFileCompilers` calls `initTestFileCompilers` with no parameters.
That is why the `esm` flag is lost in Node version 20 and above.

## Approach
Pass the `esm` flag from `load` hook to getTestFileCompilers` in order
to initialize the correct compilers.

The test 'Should import ESM without errors in ESM mode' also works for
this fix in node 20 and above.

## References
closes [#8132](#8132)

## Pre-Merge TODO
- [ ] Write tests for your proposed changes
- [ ] Make sure that existing tests do not fail

Co-authored-by: Bayheck <[email protected]>
  • Loading branch information
Bayheck and Bayheck authored Feb 29, 2024
1 parent 23c7811 commit 4a30f1c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/compiler/compilers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function createTestFileCompilers (compilerOptions = {}, { baseUrl, esm } = {}) {

let testFileCompilers = [];

export function getTestFileCompilers () {
export function getTestFileCompilers (esm) {
if (!testFileCompilers.length)
initTestFileCompilers();
initTestFileCompilers({}, { baseUrl: '', esm });

return testFileCompilers;
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/esm-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function load (url: string, context: Context, defaultLoad: Function
if (isNodeModulesDep || isTestcafeLibDep || !filename)
return defaultLoad(url, context, defaultLoad);

const testFilesInfo = await Compiler.createTestFileInfo(filename);
const testFilesInfo = await Compiler.createTestFileInfo(filename, true);

if (testFilesInfo?.compiler) {
const [compiledCode] = await testFilesInfo.compiler.precompile([testFilesInfo]);
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class Compiler {
return uniq(flattenDeep(getTestFileCompilers().map(compiler => compiler.getSupportedExtension())));
}

static async createTestFileInfo (filename) {
static async createTestFileInfo (filename, esm = false) {
let code = null;

try {
Expand All @@ -38,7 +38,7 @@ export default class Compiler {

code = stripBom(code).toString();

const compiler = find(getTestFileCompilers(), someCompiler => someCompiler.canCompile(code, filename));
const compiler = find(getTestFileCompilers(esm), someCompiler => someCompiler.canCompile(code, filename));

if (!compiler)
return null;
Expand Down

0 comments on commit 4a30f1c

Please sign in to comment.