diff --git a/packages/react-native-builder-bob/src/__fixtures__/project/code/$exports-input.ts b/packages/react-native-builder-bob/src/__fixtures__/project/code/$exports-input.ts index cf1b20524..e489c8d26 100644 --- a/packages/react-native-builder-bob/src/__fixtures__/project/code/$exports-input.ts +++ b/packages/react-native-builder-bob/src/__fixtures__/project/code/$exports-input.ts @@ -4,7 +4,9 @@ export * as c from "./c"; export * as d from "./d"; export * as e from "./e.story"; export * as f from "../f"; -export * as pak from ".."; +export * as pac from ".."; +export * as pak from "../"; +export * as pax from "../index"; export { a as a1 } from "./a"; export * from "./b"; diff --git a/packages/react-native-builder-bob/src/__fixtures__/project/code/$exports-output.ts b/packages/react-native-builder-bob/src/__fixtures__/project/code/$exports-output.ts index 1cc582624..d923ac3ec 100644 --- a/packages/react-native-builder-bob/src/__fixtures__/project/code/$exports-output.ts +++ b/packages/react-native-builder-bob/src/__fixtures__/project/code/$exports-output.ts @@ -4,7 +4,9 @@ export * as c from "./c.mjs"; export * as d from "./d"; export * as e from "./e.story.mjs"; export * as f from "../f.mjs"; -export * as pak from ".."; +export * as pac from "../index.mjs"; +export * as pak from "../index.mjs"; +export * as pax from "../index.mjs"; export { a as a1 } from "./a.mjs"; export * from "./b.mjs"; export type { A } from "./a"; diff --git a/packages/react-native-builder-bob/src/__fixtures__/project/code/$imports-input.ts b/packages/react-native-builder-bob/src/__fixtures__/project/code/$imports-input.ts index e860c2d1a..bc1c23d79 100644 --- a/packages/react-native-builder-bob/src/__fixtures__/project/code/$imports-input.ts +++ b/packages/react-native-builder-bob/src/__fixtures__/project/code/$imports-input.ts @@ -5,7 +5,9 @@ import c from "./c"; import d from "./d"; import e from "./e.story"; import f from "../f"; -import pak from ".."; +import pac from ".."; +import pak from "../"; +import pax from "../index"; import { a as a1 } from "./a"; import * as b1 from "./b"; diff --git a/packages/react-native-builder-bob/src/__fixtures__/project/code/$imports-output.ts b/packages/react-native-builder-bob/src/__fixtures__/project/code/$imports-output.ts index 0f38c8691..029bf9492 100644 --- a/packages/react-native-builder-bob/src/__fixtures__/project/code/$imports-output.ts +++ b/packages/react-native-builder-bob/src/__fixtures__/project/code/$imports-output.ts @@ -5,7 +5,9 @@ import c from "./c.mjs"; import d from "./d"; import e from "./e.story.mjs"; import f from "../f.mjs"; -import pak from ".."; +import pac from "../index.mjs"; +import pak from "../index.mjs"; +import pax from "../index.mjs"; import { a as a1 } from "./a.mjs"; import * as b1 from "./b.mjs"; import something, { c as c1 } from "./c.mjs"; diff --git a/packages/react-native-builder-bob/src/babel.ts b/packages/react-native-builder-bob/src/babel.ts index c7b992733..c33693dcd 100644 --- a/packages/react-native-builder-bob/src/babel.ts +++ b/packages/react-native-builder-bob/src/babel.ts @@ -12,19 +12,18 @@ type Options = { extension?: 'cjs' | 'mjs'; }; -const fileCache = new Map(); +const isFile = (filename: string): boolean => { + const exists = + fs.lstatSync(filename, { throwIfNoEntry: false })?.isFile() ?? false; -const doesFileExist = (filename: string): boolean => { - if (fileCache.has(filename)) { - return true; - } + return exists; +}; - try { - fs.accessSync(filename, fs.constants.F_OK); - return true; - } catch { - return false; - } +const isDirectory = (filename: string): boolean => { + const exists = + fs.lstatSync(filename, { throwIfNoEntry: false })?.isDirectory() ?? false; + + return exists; }; const isTypeImport = ( @@ -113,19 +112,32 @@ export default function ( // Add extension if .ts file or file with extension exists if ( - doesFileExist(`${filename}.ts`) || - doesFileExist(`${filename}.tsx`) || - doesFileExist(`${filename}.${extension}`) + isFile(`${filename}.ts`) || + isFile(`${filename}.tsx`) || + isFile(`${filename}.${extension}`) ) { node.source.value += `.${extension}`; return; } // Replace .ts extension with .js if .ts file exists - if (doesFileExist(filename)) { + if (isFile(filename)) { node.source.value = node.source.value.replace(/\.tsx?$/, `.${extension}`); return; } + + if ( + isDirectory(filename) && + (isFile(path.join(filename, 'index.ts')) || + isFile(path.join(filename, 'index.tsx')) || + isFile(path.join(filename, `index.${extension}`))) + ) { + node.source.value = node.source.value.replace( + /\/?$/, + `/index.${extension}` + ); + return; + } } return {