forked from BuilderIO/mitosis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* publish * add missing cases * cleanup override file logic * fix react imports * check all extensions * fix override filename logic * move * remove logs * remove
- Loading branch information
Showing
3 changed files
with
91 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Target } from '@builder.io/mitosis'; | ||
import { pathExists, readFile } from 'fs-extra'; | ||
|
||
const getOverrideFilenames = ({ | ||
filename, | ||
target, | ||
}: { | ||
filename: string; | ||
target: Target; | ||
}): string[] => { | ||
switch (target) { | ||
case 'alpine': | ||
case 'angular': | ||
case 'customElement': | ||
case 'html': | ||
case 'liquid': | ||
case 'lit': | ||
case 'marko': | ||
case 'mitosis': | ||
case 'stencil': | ||
case 'svelte': | ||
case 'swift': | ||
case 'template': | ||
case 'webcomponent': | ||
case 'vue': | ||
case 'vue2': | ||
case 'vue3': | ||
return [filename]; | ||
|
||
// For all JSX targets, we want to be flexible and allow any possible extension | ||
case 'react': | ||
case 'reactNative': | ||
case 'rsc': | ||
case 'preact': | ||
case 'solid': | ||
case 'qwik': { | ||
// strip 'tsx', 'ts', 'jsx', 'js' from filename | ||
const filenameStrippedFromExtensions = filename.replace(/(.jsx?|.tsx?)/, ''); | ||
|
||
const EXTENSIONS = ['.tsx', '.ts', '.jsx', '.js']; | ||
const filePaths: string[] = EXTENSIONS.map((ext) => filenameStrippedFromExtensions + ext); | ||
|
||
return filePaths; | ||
} | ||
|
||
default: | ||
return [filename]; | ||
} | ||
}; | ||
export const getOverrideFile = async ({ | ||
path, | ||
filename, | ||
target, | ||
}: { | ||
path: string; | ||
filename: string; | ||
target: Target; | ||
}): Promise<string | null> => { | ||
const filePaths = getOverrideFilenames({ filename, target }).map((filename) => | ||
[path, filename].join('/'), | ||
); | ||
|
||
const foundFilePath = ( | ||
await Promise.all( | ||
filePaths.map(async (filePath) => ({ filePath, exists: await pathExists(filePath) })), | ||
) | ||
).find(({ exists }) => exists); | ||
|
||
if (foundFilePath) { | ||
return readFile(foundFilePath.filePath, 'utf8'); | ||
} else { | ||
return null; | ||
} | ||
}; |