Skip to content

Commit a8827b7

Browse files
authored
fix: Fix custom ESM loader for tests within vscode extension (#345)
1 parent 9f0f2d1 commit a8827b7

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

build/mocha-esm-loader.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let unittestsModule;
1010
// Get absolute paths for imports in the loader
1111
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1212
const projectRoot = path.join(__dirname, '..');
13+
const projectOutUrl = pathToFileURL(path.join(projectRoot, 'out')).href + '/';
1314
const vscodeMockPath = pathToFileURL(path.join(projectRoot, 'out/test/vscode-mock.js')).href;
1415
const telemetryMockPath = pathToFileURL(path.join(projectRoot, 'out/test/mocks/vsc/telemetryReporter.js')).href;
1516

@@ -421,9 +422,9 @@ export async function load(url, context, nextLoad) {
421422
// If URL parsing fails, fall through to other handlers
422423
}
423424

424-
// For .js files in the out/ directory, inject CJS globals shims
425+
// For .js files in the project's out/ directory, inject CJS globals shims
425426
// This is needed because source files use native __dirname, __filename, require which don't exist in ESM
426-
if (url.startsWith('file://') && url.includes('/out/') && url.endsWith('.js')) {
427+
if (url.startsWith(projectOutUrl) && url.endsWith('.js')) {
427428
const filePath = fileURLToPath(url);
428429
const fs = await import('node:fs/promises');
429430
const source = await fs.readFile(filePath, 'utf8');

0 commit comments

Comments
 (0)