Skip to content

Commit 4a30f1c

Browse files
BayheckBayheck
andauthored
fix: esm loader fix (#8146)
<!-- 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]>
1 parent 23c7811 commit 4a30f1c

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

src/compiler/compilers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ function createTestFileCompilers (compilerOptions = {}, { baseUrl, esm } = {}) {
2020

2121
let testFileCompilers = [];
2222

23-
export function getTestFileCompilers () {
23+
export function getTestFileCompilers (esm) {
2424
if (!testFileCompilers.length)
25-
initTestFileCompilers();
25+
initTestFileCompilers({}, { baseUrl: '', esm });
2626

2727
return testFileCompilers;
2828
}

src/compiler/esm-loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export async function load (url: string, context: Context, defaultLoad: Function
3232
if (isNodeModulesDep || isTestcafeLibDep || !filename)
3333
return defaultLoad(url, context, defaultLoad);
3434

35-
const testFilesInfo = await Compiler.createTestFileInfo(filename);
35+
const testFilesInfo = await Compiler.createTestFileInfo(filename, true);
3636

3737
if (testFilesInfo?.compiler) {
3838
const [compiledCode] = await testFilesInfo.compiler.precompile([testFilesInfo]);

src/compiler/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class Compiler {
2626
return uniq(flattenDeep(getTestFileCompilers().map(compiler => compiler.getSupportedExtension())));
2727
}
2828

29-
static async createTestFileInfo (filename) {
29+
static async createTestFileInfo (filename, esm = false) {
3030
let code = null;
3131

3232
try {
@@ -38,7 +38,7 @@ export default class Compiler {
3838

3939
code = stripBom(code).toString();
4040

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

4343
if (!compiler)
4444
return null;

0 commit comments

Comments
 (0)