-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(devkit): add loadConfigFile function for plugins to use (#21511)
- Loading branch information
Showing
8 changed files
with
94 additions
and
108 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 was deleted.
Oops, something went wrong.
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,78 @@ | ||
import { extname, join } from 'path'; | ||
import { existsSync } from 'fs'; | ||
// eslint-disable-next-line @typescript-eslint/no-restricted-imports | ||
import { workspaceRoot } from 'nx/src/devkit-exports'; | ||
// eslint-disable-next-line @typescript-eslint/no-restricted-imports | ||
import { registerTsProject } from 'nx/src/plugins/js/utils/register'; | ||
|
||
export let dynamicImport = new Function( | ||
'modulePath', | ||
'return import(modulePath);' | ||
); | ||
|
||
export async function loadConfigFile<T extends object = any>( | ||
configFilePath: string | ||
): Promise<T> { | ||
{ | ||
let module: any; | ||
|
||
if (extname(configFilePath) === '.ts') { | ||
const tsConfigPath = getRootTsConfigPath(); | ||
if (tsConfigPath) { | ||
const unregisterTsProject = registerTsProject(tsConfigPath); | ||
try { | ||
module = await load(configFilePath); | ||
} finally { | ||
unregisterTsProject(); | ||
} | ||
} else { | ||
module = await load(configFilePath); | ||
} | ||
} else { | ||
module = await load(configFilePath); | ||
} | ||
return module.default ?? module; | ||
} | ||
} | ||
|
||
export function getRootTsConfigPath(): string | null { | ||
const tsConfigFileName = getRootTsConfigFileName(); | ||
return tsConfigFileName ? join(workspaceRoot, tsConfigFileName) : null; | ||
} | ||
|
||
export function getRootTsConfigFileName(): string | null { | ||
for (const tsConfigName of ['tsconfig.base.json', 'tsconfig.json']) { | ||
const pathExists = existsSync(join(workspaceRoot, tsConfigName)); | ||
if (pathExists) { | ||
return tsConfigName; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Load the module after ensuring that the require cache is cleared. | ||
*/ | ||
async function load(path: string): Promise<any> { | ||
// Clear cache if the path is in the cache | ||
if (require.cache[path]) { | ||
for (const k of Object.keys(require.cache)) { | ||
delete require.cache[k]; | ||
} | ||
} | ||
|
||
try { | ||
// Try using `require` first, which works for CJS modules. | ||
// Modules are CJS unless it is named `.mjs` or `package.json` sets type to "module". | ||
return require(path); | ||
} catch (e: any) { | ||
if (e.code === 'ERR_REQUIRE_ESM') { | ||
// If `require` fails to load ESM, try dynamic `import()`. | ||
return await dynamicImport(`${path}?t=${Date.now()}`); | ||
} | ||
|
||
// Re-throw all other errors | ||
throw e; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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