diff --git a/src/import.ts b/src/import.ts index b941d68..c0d2d97 100644 --- a/src/import.ts +++ b/src/import.ts @@ -43,6 +43,7 @@ export async function importx(_specifier: string | URL, _options: strin excludeLoaders = [], listDependencies = null, ignoreImportxWarning = false, + fallbackLoaders = ['jiti'], ...otherOptions } = options @@ -89,9 +90,10 @@ export async function importx(_specifier: string | URL, _options: strin timestampLoad: -1, } - debug(`[${loader}]`, 'Importing', fullPath, 'from', parentPath) + async function run(loader: SupportedLoader) { + info.loader = loader + debug(`[${loader}]`, 'Importing', fullPath, 'from', parentPath) - async function run() { switch (loader) { case 'native': { if (cache === false && !ignoreImportxWarning) @@ -134,13 +136,30 @@ export async function importx(_specifier: string | URL, _options: strin } } - const mod = await run() - info.timestampLoad = Date.now() - const previous = _moduleInfoMap.get(mod) - if (previous) - info.previousImportInfo = previous - _moduleInfoMap.set(mod, info) - return mod + const loaders = new Set([ + loader, + ...fallbackLoaders || [], + ]) + + let error: unknown | undefined + + for (const loader of loaders) { + try { + const mod = await run(loader) + info.timestampLoad = Date.now() + const previous = _moduleInfoMap.get(mod) + if (previous) + info.previousImportInfo = previous + _moduleInfoMap.set(mod, info) + return mod + } + catch (err) { + error = err + debug(`[${loader}]`, err) + } + } + + throw error } function getLoaderFromEnv(): SupportedLoader | undefined { diff --git a/src/types.ts b/src/types.ts index e010e7c..24c085d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -61,6 +61,12 @@ export interface ImportxOptions extends FeaturesOptions { * @default 'auto' */ loader?: SupportedLoader | 'auto' + /** + * Fallback loaders used to import the module. + * + * @default ['jiti'] + */ + fallbackLoaders?: SupportedLoader[] | false /** * Options for each loader * Only the loader that is used will be applied.