chore(cli): 去除@vuepress/cli依赖
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { allowTs, info } from '@vuepress/cli';
|
||||
import { cac } from 'cac';
|
||||
import chalk from 'chalk';
|
||||
|
||||
import { allowTs } from './utils/allowTs';
|
||||
import { scripts } from './commands';
|
||||
import { UserConfig } from './types';
|
||||
|
||||
@@ -37,8 +37,5 @@ export const cli = (defaultAppConfig: UserConfig): void => {
|
||||
// register `dev` command
|
||||
program.command('entry', 'Start development server').action(wrapCommand(scripts(defaultAppConfig)));
|
||||
|
||||
// register `info` command
|
||||
program.command('info', 'Display environment information').action(wrapCommand(info));
|
||||
|
||||
program.parse(process.argv);
|
||||
};
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import path from 'path';
|
||||
|
||||
import { loadUserConfig } from '@vuepress/cli';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
import App from '../Core';
|
||||
import { UserConfig } from '../types';
|
||||
import { loadUserConfig } from '../utils/loadUserConfig';
|
||||
|
||||
export const scripts = (defaultAppConfig: UserConfig) => {
|
||||
const entry = async (): Promise<void> => {
|
||||
|
||||
@@ -61,3 +61,5 @@ export interface UserConfig {
|
||||
onInit?: (app: Core) => ModuleMainFilePath | Promise<ModuleMainFilePath>;
|
||||
onPrepare?: (app: Core) => void;
|
||||
}
|
||||
|
||||
export type UserConfigLoader = (userConfigPath: string) => Promise<UserConfig>;
|
||||
|
||||
23
packages/cli/src/utils/allowTs.ts
Normal file
23
packages/cli/src/utils/allowTs.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { transformSync } from 'esbuild';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
/**
|
||||
* Transform a ts file to cjs code
|
||||
*/
|
||||
export const transformTsFileToCodeSync = (filename: string): string =>
|
||||
transformSync(fs.readFileSync(filename).toString(), {
|
||||
format: 'cjs',
|
||||
loader: 'ts',
|
||||
sourcefile: filename,
|
||||
sourcemap: 'inline',
|
||||
target: 'node14',
|
||||
}).code;
|
||||
|
||||
/**
|
||||
* Globally allow ts files to be loaded via `require()`
|
||||
*/
|
||||
export const allowTs = (): void => {
|
||||
require.extensions['.ts'] = (m: any, filename) => {
|
||||
m._compile(transformTsFileToCodeSync(filename), filename);
|
||||
};
|
||||
};
|
||||
29
packages/cli/src/utils/loadUserConfig.ts
Normal file
29
packages/cli/src/utils/loadUserConfig.ts
Normal 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 {};
|
||||
};
|
||||
Reference in New Issue
Block a user