Skip to content

Commit

Permalink
Fix(Cross Import): ReferenceError: __dirname is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Jun 10, 2023
1 parent fa72096 commit 42f7d2b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
10 changes: 5 additions & 5 deletions packages/cross-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cross-import",
"scripts": {
"build:cjs": "esbuild src/index.ts --bundle --outfile=dist/index.js --format=cjs --minify --sourcemap --platform=node --external:esbuild --external:fast-glob --external:jiti --external:@techor/extend --external:sucrase --external:upath",
"build:esm": "esbuild src/index.ts --bundle --outfile=dist/index.mjs --format=esm --minify --sourcemap --platform=node --external:esbuild --external:fast-glob --external:jiti --external:@techor/extend --external:sucrase --external:upath",
"build:esm": "esbuild src/index.ts --bundle --outfile=dist/index.esm.mjs --format=esm --minify --sourcemap --platform=node --external:esbuild --external:fast-glob --external:jiti --external:@techor/extend --external:sucrase --external:upath",
"build:type": "tsc --emitDeclarationOnly --preserveWatchOutput",
"build": "npm run build:cjs && npm run build:esm && npm run build:type",
"dev": "conc 'npm:build:* -- --watch'",
Expand Down Expand Up @@ -45,14 +45,14 @@
"access": "public"
},
"main": "./dist/index.js",
"jsnext:main": "./dist/index.mjs",
"esnext": "./dist/index.mjs",
"module": "./dist/index.mjs",
"jsnext:main": "./dist/index.esm.mjs",
"esnext": "./dist/index.esm.mjs",
"module": "./dist/index.esm.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs",
"import": "./dist/index.esm.mjs",
"types": "./dist/index.d.ts"
}
},
Expand Down
37 changes: 37 additions & 0 deletions packages/cross-import/src/index.esm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import path from 'path'
import fg from 'fast-glob'
import extend from '@techor/extend'
import jiti from 'jiti'
import { transform } from 'sucrase'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)

export default function crossImport(
source: string | fg.Pattern[],
options?: fg.Options
): any {
options = extend({ cwd: process.cwd() }, options)
if (!source) return
const filePath = fg.sync(source, options)[0]
if (!filePath) return
const resolvedFilePath = path.resolve(options.cwd, filePath)
if (process.env.DEBUG) {
console.log('[crossImport] resolvedFilePath:', resolvedFilePath)
}
try {
delete require.cache[resolvedFilePath]
return require(resolvedFilePath)
} catch {
return jiti(__filename, {
interopDefault: true,
transform: (options) => {
return transform(options.source, {
transforms: ['imports', 'typescript'],
})
}
})(resolvedFilePath)
}
}

export { crossImport }
7 changes: 1 addition & 6 deletions packages/cross-import/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import fg from 'fast-glob'
import extend from '@techor/extend'
import jiti from 'jiti'
import { transform } from 'sucrase'
import { fileURLToPath } from 'url'

const currentFilename = typeof __filename === 'undefined'
? fileURLToPath(import.meta.url)
: __filename

export default function crossImport(
source: string | fg.Pattern[],
Expand All @@ -25,7 +20,7 @@ export default function crossImport(
delete require.cache[resolvedFilePath]
return require(resolvedFilePath)
} catch {
return jiti(currentFilename, {
return jiti(__filename, {
interopDefault: true,
transform: (options) => {
return transform(options.source, {
Expand Down

0 comments on commit 42f7d2b

Please sign in to comment.