-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dadbb2b
commit 9d2cf8a
Showing
13 changed files
with
164 additions
and
75 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
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,19 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
export const findUpFile = ( | ||
filePath: string, | ||
fileName: string | string[], | ||
levels = 10, | ||
): string | null => { | ||
const files = fs.readdirSync(filePath); | ||
if (levels === 0) { | ||
return null; | ||
} | ||
const arFiles: string[] = Array.isArray(fileName) ? fileName : [fileName]; | ||
const pckg = files.find(file => arFiles.includes(file)); | ||
if (pckg) { | ||
return path.resolve(filePath, pckg); | ||
} | ||
return findUpFile(path.resolve(filePath, '..'), fileName, levels - 1); | ||
}; |
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,35 @@ | ||
import path from 'path'; | ||
import * as ts from 'typescript'; | ||
|
||
export const dynamicRequire = (filePath: string): any => { | ||
const fileParts = filePath.split('.'); | ||
const ext = fileParts.pop()?.toLowerCase() || 'js'; | ||
if (['ts', 'tsx'].indexOf(ext) !== -1) { | ||
const configPath = ts.findConfigFile( | ||
path.dirname(filePath), | ||
ts.sys.fileExists, | ||
); | ||
const config = configPath | ||
? ts.readConfigFile(configPath, ts.sys.readFile) | ||
: { | ||
config: { | ||
compilerOptions: { | ||
allowJs: true, | ||
jsx: ts.JsxEmit.ReactJSX, | ||
module: ts.ModuleKind.CommonJS, | ||
}, | ||
}, | ||
}; | ||
|
||
const program = ts.createProgram([filePath], config.config); | ||
program.emit(); | ||
const jsFilePath = `${fileParts.join('.')}.js`; | ||
try { | ||
return require('esm')(module)(jsFilePath); | ||
} finally { | ||
//fs.unlinkSync(tmpFilePath); | ||
} | ||
} | ||
|
||
return require('esm')(module)(filePath); | ||
}; |
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
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
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