1
0
mirror of synced 2026-04-04 23:38:37 +08:00

chore(cli): 去除@vuepress/cli依赖

This commit is contained in:
roymondchen
2022-08-29 12:09:02 +08:00
committed by jia000
parent de8ef8dc58
commit f3c376a31f
7 changed files with 355 additions and 8 deletions

View File

@@ -0,0 +1,29 @@
import { UserConfig, UserConfigLoader } from '../types';
export const isPlainObject = <T extends Record<any, any> = Record<any, any>>(val: unknown): val is T =>
Object.prototype.toString.call(val) === '[object Object]';
/**
* Check if a module is esm with `export default`
*/
export const hasExportDefault = <T = any>(mod: unknown): mod is { default: T } =>
isPlainObject(mod) && !!mod.__esModule && Object.prototype.hasOwnProperty.call(mod, 'default');
export const loadUserConfigCjs: UserConfigLoader = async (userConfigPath) => {
const required = require(userConfigPath);
return hasExportDefault(required) ? required.default : required;
};
const loaderMap: [RegExp, UserConfigLoader][] = [[/\.(js|cjs|ts)$/, loadUserConfigCjs]];
export const loadUserConfig = async (userConfigPath?: string): Promise<Partial<UserConfig>> => {
if (!userConfigPath) return {};
for (const [condition, loader] of loaderMap) {
if (condition.test(userConfigPath)) {
return loader(userConfigPath);
}
}
return {};
};