Skip to content

Commit

Permalink
fix: update loadFreshModule to be compatible with jiti1, but reflect …
Browse files Browse the repository at this point in the history
…jiti2 usage
  • Loading branch information
lachieh committed Nov 23, 2024
1 parent 0f70a92 commit 7577a0b
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions packages/docusaurus-utils/src/moduleUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import jiti from 'jiti';
import {createJiti} from 'jiti';
import logger from '@docusaurus/logger';

/*
Expand All @@ -18,20 +18,34 @@ export async function loadFreshModule(modulePath: string): Promise<unknown> {
logger.interpolate`Invalid module path of type name=${modulePath}`,
);
}
const load = jiti(__filename, {
const jiti = createJiti(__dirname, {
// Transpilation cache, can be safely enabled
cache: true,
fsCache: true,
// Bypass Node.js runtime require cache
// Same as "import-fresh" package we used previously
requireCache: false,
moduleCache: false,
// Only take into consideration the default export
// For now we don't need named exports
// This also helps normalize return value for both CJS/ESM/TS modules
interopDefault: true,
// debug: true,
});

return load(modulePath);
const module = await jiti.import(modulePath);
if (!module) {
return undefined;
}

if (typeof module !== 'object') {
return module;
}

if ('default' in module) {
const {default: def, ...rest} = module;
return {...(def || {}), ...rest};
}

return module;
} catch (error) {
throw new Error(
logger.interpolate`Docusaurus could not load module at path path=${modulePath}\nCause: ${
Expand Down

0 comments on commit 7577a0b

Please sign in to comment.