diff --git a/packages/cli-shared/src/BabelRegister/index.ts b/packages/cli-shared/src/BabelRegister/index.ts index d385766..07eb8e1 100644 --- a/packages/cli-shared/src/BabelRegister/index.ts +++ b/packages/cli-shared/src/BabelRegister/index.ts @@ -3,7 +3,7 @@ import { createDebug, lodash, winPath } from '../' const debug = createDebug('xus:shared:BabelRegister') export class BabelRegister { - only: Record = {} + private only: Record = {} setOnlyMap({ key, value }: { key: string; value: string[] }) { debug(`set ${key} of only map:`) @@ -12,7 +12,7 @@ export class BabelRegister { this.register() } - register() { + private register() { const only = lodash.uniq( Object.keys(this.only) .reduce((memo, key) => { @@ -38,3 +38,5 @@ export class BabelRegister { }) } } + +export type IBabelRegister = InstanceType diff --git a/packages/cli-shared/src/file/getFileMeta.ts b/packages/cli-shared/src/file/getFileMeta.ts index 5641d93..20f8930 100644 --- a/packages/cli-shared/src/file/getFileMeta.ts +++ b/packages/cli-shared/src/file/getFileMeta.ts @@ -5,14 +5,23 @@ import { winPath } from '../winPath' interface IGetFileMetaOps { base: string filenameWithoutExt: string + type: 'js' | 'lib' | 'css' } const extsMap = { - js: ['.ts', '.js'] + js: ['.ts', '.js'], + lib: ['.js', 'jsx', '.ts', '.tsx', '.vue'], + css: ['.css', '.scss', '.sass', '.less'] } -export const getFileMeta = (ops: IGetFileMetaOps) => { - const exts = extsMap.js +export const getFileMeta = ( + ops: IGetFileMetaOps = { + base: process.cwd(), + filenameWithoutExt: '', + type: 'js' + } +) => { + const exts = extsMap[ops.type] for (const ext of exts) { const filename = `${ops.filenameWithoutExt}${ext}` const path = winPath(join(ops.base, filename)) diff --git a/packages/cli-shared/src/index.ts b/packages/cli-shared/src/index.ts index ab21b29..d3caf07 100644 --- a/packages/cli-shared/src/index.ts +++ b/packages/cli-shared/src/index.ts @@ -18,7 +18,7 @@ export { createEnvNameWithXusPrefix } from './env' export * from './file' export { loadModule } from './loadModule' export * from './pkg' -export { BabelRegister } from './BabelRegister' +export { BabelRegister, IBabelRegister } from './BabelRegister' // types export export type { diff --git a/packages/cli/src/exports/pluginAPI.ts b/packages/cli/src/exports/pluginAPI.ts index c2f0bdb..c2a5c3d 100644 --- a/packages/cli/src/exports/pluginAPI.ts +++ b/packages/cli/src/exports/pluginAPI.ts @@ -7,6 +7,7 @@ import type { IArgs, IRawArgs } from '@xus/core' +import type { IBabelRegister } from '@xus/cli-shared' import type { IConfig } from './create' import { IBuildLibMethods } from '@xus/plugin-build-lib' import { IBundlerRollupMethods } from '@xus/plugin-bundler-rollup' @@ -14,6 +15,8 @@ import { IBundlerRollupMethods } from '@xus/plugin-bundler-rollup' type noopFn = () => any export interface IPluginAPI extends IPluginAPIBase { + // service api + BabelRegister: IBabelRegister // service lifycycle onPluginReady: noopFn onRunCmd: noopFn diff --git a/packages/core/src/cli/Service.ts b/packages/core/src/cli/Service.ts index 3eec6a8..f9b67a5 100644 --- a/packages/core/src/cli/Service.ts +++ b/packages/core/src/cli/Service.ts @@ -1,7 +1,8 @@ // types import type { ICliServerOps, ICommand, IArgs, IRawArgs } from '../types' -import { Logger } from '@xus/cli-shared' +import { Logger, BabelRegister } from '@xus/cli-shared' import { CliServiceStage, HookTypes } from '../enums' +import { CONFIG_FILES } from '../constants' import { ConfigManager, EnvManager, @@ -28,6 +29,8 @@ export class CliService { PluginManager HookManager + BabelRegister + protected setStage(nextStage: CliServiceStage) { this.stage = nextStage } @@ -38,6 +41,7 @@ export class CliService { logger.debug(ops) // 1. init manager this.setStage(CliServiceStage.initManager) + this.BabelRegister = new BabelRegister() this.PathManager = new PathManager() this.EnvManager = new EnvManager({ service: this, @@ -64,6 +68,10 @@ export class CliService { logger.debug(this.HookManager) // 2. init config (without plugin config) + this.BabelRegister.setOnlyMap({ + key: 'xus:config', + value: CONFIG_FILES + }) this.setStage(CliServiceStage.initUserConfig) this.ConfigManager.initUserConfig() @@ -140,6 +148,10 @@ export class CliService { return new Proxy(api, { get: (target, key: string) => { + // proxy to service + if (['BabelRegister'].includes(key)) { + return (this as any)[key] + } // registerMethods if (this.pluginMethods[key]) { return this.pluginMethods[key].bind(api) diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts new file mode 100644 index 0000000..59279fa --- /dev/null +++ b/packages/core/src/constants.ts @@ -0,0 +1,2 @@ +export const CONFIG_FILES = ['xus.config.js', 'xus.config.ts'] +export const CONFIG_FILENAME = 'xus.config' diff --git a/packages/core/src/manager/PathManager.ts b/packages/core/src/manager/PathManager.ts index 33ea72b..4815d07 100644 --- a/packages/core/src/manager/PathManager.ts +++ b/packages/core/src/manager/PathManager.ts @@ -1,6 +1,7 @@ import { join, relative } from 'path' import { statSync, readdirSync } from 'fs' import { getFileMeta, isLernaPkg } from '@xus/cli-shared' +import { CONFIG_FILENAME } from '../constants' export class PathManager { get cwd(): string { @@ -14,7 +15,8 @@ export class PathManager { get userConfigPath() { const fileMeta = getFileMeta({ base: this.cwd, - filenameWithoutExt: 'xus.config' + filenameWithoutExt: CONFIG_FILENAME, + type: 'js' }) return fileMeta && fileMeta.path }