Skip to content

Commit

Permalink
fix: don't add extensions if .native.ts etc. exist
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Jul 4, 2024
1 parent 176444e commit 135f681
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions packages/react-native-builder-bob/src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ const isDirectory = (filename: string): boolean => {
return exists;
};

const isModule = (filename: string, ext: string): boolean => {
const exts = ['ts', 'tsx', ext];

// Metro won't resolve these extensions if explicit extension is provided
// So we can't add extension to these files
const additional = ['native', 'android', 'ios', 'web'];

return exts.some(
(ext) =>
isFile(`${filename}.${ext}`) &&
additional.every((add) => !isFile(`${filename}.${add}.${ext}`))
);
};

const isTypeImport = (
node: ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration
) =>
Expand Down Expand Up @@ -110,27 +124,22 @@ export default function (
node.source.value
);

// Add extension if .ts file or file with extension exists
if (
isFile(`${filename}.ts`) ||
isFile(`${filename}.tsx`) ||
isFile(`${filename}.${extension}`)
) {
node.source.value += `.${extension}`;
// Replace .ts extension with .js if file with extension is explicitly imported
if (isFile(filename)) {
node.source.value = node.source.value.replace(/\.tsx?$/, `.${extension}`);
return;
}

// Replace .ts extension with .js if .ts file exists
if (isFile(filename)) {
node.source.value = node.source.value.replace(/\.tsx?$/, `.${extension}`);
// Add extension if .ts file or file with extension exists
if (isModule(filename, extension)) {
node.source.value += `.${extension}`;
return;
}

// Expand folder imports to index and add extension
if (
isDirectory(filename) &&
(isFile(path.join(filename, 'index.ts')) ||
isFile(path.join(filename, 'index.tsx')) ||
isFile(path.join(filename, `index.${extension}`)))
isModule(path.join(filename, 'index'), extension)
) {
node.source.value = node.source.value.replace(
/\/?$/,
Expand Down

0 comments on commit 135f681

Please sign in to comment.