Skip to content

Commit

Permalink
fix: add support for index files to babel plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jul 2, 2024
1 parent e33e892 commit 693a27b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
42 changes: 27 additions & 15 deletions packages/react-native-builder-bob/src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ type Options = {
extension?: 'cjs' | 'mjs';
};

const fileCache = new Map<string, string>();
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 = (
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 693a27b

Please sign in to comment.