Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 4x 17x 6x 19x 19x 19x 19x 17x 2x 1x 18x 17x 4x 4x 4x 13x 14x 2x 14x 16x | import { createJiti } from 'jiti';
import type jiti from 'jiti';
import { transform, Options } from 'sucrase';
type Jiti = ReturnType<typeof jiti>;
type JITIOptions = Jiti['options'];
let jitiInstance: ReturnType<typeof jiti> | null = null;
function lazyJiti(option: JITIOptions = {}, transformOpt = {} as Options) {
return (
jitiInstance ??
(jitiInstance = createJiti(__filename, {
interopDefault: true,
...option,
transform: (opts) => {
return transform(opts.source, {
transforms: ['typescript', 'imports'],
...transformOpt,
});
},
}))
);
}
export interface LoadConfOption {
jiti?: boolean;
jitiOptions?: JITIOptions;
transformOption?: Options;
}
export async function loadConf<T>(path: string, option: LoadConfOption = {}): Promise<T> {
const { jiti = true, jitiOptions, transformOption } = option;
let config = await (async function () {
try {
if (jiti) {
return path ? await lazyJiti(jitiOptions, transformOption).import(path) : {};
} else {
return path ? require(path) : {};
}
} catch {
return await lazyJiti(jitiOptions, transformOption).import(path);
}
})();
// Ensure both default export and named exports are handled
if (config.default) {
if (typeof config.default === 'function') {
const defaultFunc = config.default;
Object.assign(defaultFunc, config);
return defaultFunc;
} else {
config = { ...config.default, ...config };
}
}
// ⚠️ 🚨 For some reason, the CI test default does not exist on the Windows platform.
// To ensure platform consistency, default has been removed.
if (path && path.endsWith('.cjs')) {
delete config.default;
}
return config;
}
export async function jsLoader<T>(filepath: string, content: string, option: LoadConfOption = {}): Promise<T> {
return await loadConf<T>(filepath, option);
}
|