From 60bed272d55858901391a944630e453b842d2b77 Mon Sep 17 00:00:00 2001 From: Razvan Marescu Date: Tue, 25 Feb 2025 12:24:31 -0800 Subject: [PATCH] fix: ES Module compatibility for dirname Prevent "undefined in ES module scope" errors in type:module projects --- packages/shortest/src/core/compiler/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/shortest/src/core/compiler/index.ts b/packages/shortest/src/core/compiler/index.ts index 0ec4292e..0b0481b5 100644 --- a/packages/shortest/src/core/compiler/index.ts +++ b/packages/shortest/src/core/compiler/index.ts @@ -31,9 +31,10 @@ export class TestCompiler { js: ` import { fileURLToPath } from 'url'; import { dirname } from 'path'; + import { createRequire } from 'module'; + const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); - import { createRequire } from "module"; const require = createRequire(import.meta.url); `, }, @@ -70,7 +71,15 @@ export class TestCompiler { }, resolveExtensions: [".ts", ".js", ".mjs"], banner: { - js: 'import { createRequire } from "module";const require = createRequire(import.meta.url);', + js: ` + import { fileURLToPath } from 'url'; + import { dirname } from 'path'; + import { createRequire } from 'module'; + + const __filename = fileURLToPath(import.meta.url); + const __dirname = dirname(__filename); + const require = createRequire(import.meta.url); + `, }, });