diff --git a/assets/cjs_shims.js b/assets/cjs_shims.js index 425493c6..a2056370 100644 --- a/assets/cjs_shims.js +++ b/assets/cjs_shims.js @@ -1,5 +1,7 @@ +// Shim globals in cjs bundle + export const importMetaUrlShim = typeof document === 'undefined' - ? new (require('u' + 'rl').URL)('file:' + __filename).href + ? new URL('file:' + __filename).href : (document.currentScript && document.currentScript.src) || new URL('main.js', document.baseURI).href diff --git a/assets/esm_shims.js b/assets/esm_shims.js new file mode 100644 index 00000000..6749c80c --- /dev/null +++ b/assets/esm_shims.js @@ -0,0 +1,8 @@ +// Shim globals in esm bundle +import { fileURLToPath } from 'url' +import path from 'path' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = path.dirname(__filename) + +export { __dirname, __filename } diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index 5f327e84..5edddb9a 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -172,6 +172,7 @@ export async function runEsbuild( }, inject: [ format === 'cjs' ? path.join(__dirname, '../assets/cjs_shims.js') : '', + format === 'esm' ? path.join(__dirname, '../assets/esm_shims.js') : '', ...(options.inject || []), ].filter(Boolean), outdir: diff --git a/test/index.test.ts b/test/index.test.ts index f62f36f8..56dbc924 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -517,6 +517,21 @@ test(`transform import.meta.url in cjs format`, async (t) => { t.snapshot(await getFileContent('dist/input.js')) }) +test(`transform __dirname, __filename in esm format`, async (t) => { + const { getFileContent } = await run( + t.title, + { + 'input.ts': `export const a = __dirname + export const b = __filename + `, + }, + { + flags: ['--format', 'esm'], + } + ) + t.snapshot(await getFileContent('dist/input.mjs')) +}) + test('debounce promise', async (t) => { try { const equal = (a: T, b: T) => { diff --git a/test/snapshots/index.test.ts.md b/test/snapshots/index.test.ts.md index 0ce30859..50f38890 100644 --- a/test/snapshots/index.test.ts.md +++ b/test/snapshots/index.test.ts.md @@ -393,7 +393,7 @@ Generated by [AVA](https://avajs.dev). });␊ ␊ // ../../../assets/cjs_shims.js␊ - var importMetaUrlShim = typeof document === "undefined" ? new (require("url")).URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;␊ + var importMetaUrlShim = typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;␊ ␊ // input.ts␊ var input_default = importMetaUrlShim;␊ @@ -401,6 +401,25 @@ Generated by [AVA](https://avajs.dev). 0 && (module.exports = {});␊ ` +## transform __dirname, __filename in esm format + +> Snapshot 1 + + `// ../../../assets/esm_shims.js␊ + import { fileURLToPath } from "url";␊ + import path from "path";␊ + var __filename = fileURLToPath(import.meta.url);␊ + var __dirname = path.dirname(__filename);␊ + ␊ + // input.ts␊ + var a = __dirname;␊ + var b = __filename;␊ + export {␊ + a,␊ + b␊ + };␊ + ` + ## code splitting in cjs format > Snapshot 1 diff --git a/test/snapshots/index.test.ts.snap b/test/snapshots/index.test.ts.snap index 58295ad3..e592c402 100644 Binary files a/test/snapshots/index.test.ts.snap and b/test/snapshots/index.test.ts.snap differ