Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: shim __dirname, __filename in esm bundle
  • Loading branch information
egoist committed Nov 29, 2021
1 parent b0c3677 commit d0870dd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion 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
8 changes: 8 additions & 0 deletions 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 }
1 change: 1 addition & 0 deletions src/esbuild/index.ts
Expand Up @@ -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:
Expand Down
15 changes: 15 additions & 0 deletions test/index.test.ts
Expand Up @@ -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 = <T>(a: T, b: T) => {
Expand Down
21 changes: 20 additions & 1 deletion test/snapshots/index.test.ts.md
Expand Up @@ -393,14 +393,33 @@ 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;␊
// Annotate the CommonJS export names for ESM import in node:␊
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
Expand Down
Binary file modified test/snapshots/index.test.ts.snap
Binary file not shown.

0 comments on commit d0870dd

Please sign in to comment.