Skip to content

Commit

Permalink
Fix(Cross Import): Errors should not be caught
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Oct 31, 2023
1 parent dc36b15 commit aba5e25
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 51 deletions.
16 changes: 5 additions & 11 deletions packages/cross-import/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "cross-import",
"scripts": {
"build:cjs": "esbuild src/index.ts --bundle --outfile=dist/index.js --format=cjs --minify --sourcemap --platform=node",
"build:cjs": "esbuild src/index.ts --outfile=dist/index.js --format=cjs --minify --sourcemap --platform=node",
"build:type": "tsc --emitDeclarationOnly --preserveWatchOutput",
"build": "pnpm run build:cjs && pnpm run build:type",
"dev": "pnpm run \"/^build:.*/\" --watch",
Expand Down Expand Up @@ -44,18 +44,12 @@
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"exports": "./dist/index.js",
"files": [
"dist"
],
"devDependencies": {
"sucrase": "^3.32.0",
"jiti": "^1.18.2"
"dependencies": {
"jiti": "^1.21.0",
"sucrase": "^3.34.0"
}
}
37 changes: 15 additions & 22 deletions packages/cross-import/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,38 @@ import { transform } from 'sucrase'

export default function crossImport(modulePath: string): any {
if (!modulePath) return
if (process.env.DEBUG) {
console.log('[DEBUG] [Cross Import] modulePath:', modulePath)
}
/** try to delete cache first */
try {
if (require.cache[modulePath]) {
delete require.cache[modulePath]
if (process.env.DEBUG) {
console.log('[DEBUG] [Cross Import] delete cache')
console.log('[cross-import] cache deleted')
}
}
} catch { /* empty */ }

try {
if (process.env.DEBUG) {
console.log('[DEBUG] [Cross Import] require')
console.log('[cross-import] require:', modulePath)
}
return require(modulePath)
} catch (error) {
if (process.env.DEBUG) {
console.log('[DEBUG] [Cross Import] JITI')
console.error(error)
console.log('[cross-import] fall back to jiti:', modulePath)
}
try {
return jiti(__filename, {
interopDefault: true,
cache: false,
debug: !!process.env.DEBUG,
transform: (options) => {
if (process.env.DEBUG) {
console.log('[DEBUG] [Cross Import] JITI transform')
}
return transform(options.source, {
transforms: ['imports', 'typescript'],
})
return jiti(__filename, {
interopDefault: true,
cache: false,
debug: !!process.env.DEBUG,
transform: (options) => {
if (process.env.DEBUG) {
console.log('[cross-import] jiti transform')
}
})(modulePath)
} catch (error) {
throw new Error(error)
}
return transform(options.source, {
transforms: ['imports', 'typescript'],
})
}
})(modulePath)
}
}
4 changes: 2 additions & 2 deletions packages/explore-config/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "explore-config",
"scripts": {
"build:cjs": "esbuild src/index.ts --bundle --outfile=dist/index.js --format=cjs --minify --sourcemap --platform=node --external:esbuild --external:@techor/glob --external:@techor/log --external:@techor/extend --external:cross-import --external:upath",
"build:esm": "esbuild src/index.ts --bundle --outfile=dist/index.mjs --format=esm --minify --sourcemap --platform=node --external:esbuild --external:@techor/glob --external:@techor/log --external:@techor/extend --external:cross-import --external:upath",
"build:cjs": "esbuild src/index.ts --outfile=dist/index.js --format=cjs --minify --sourcemap --platform=node",
"build:esm": "esbuild src/index.ts --outfile=dist/index.mjs --format=esm --minify --sourcemap --platform=node",
"build:type": "tsc --emitDeclarationOnly --preserveWatchOutput",
"build": "pnpm run build:cjs && pnpm run build:esm && pnpm run build:type",
"dev": "pnpm run \"/^build:.*/\" --watch",
Expand Down
38 changes: 22 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aba5e25

Please sign in to comment.