Skip to content

Commit

Permalink
refactor: 增强getfilemeta和babelregister能力
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Feb 25, 2021
1 parent 76ce0b8 commit c55eb0f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 8 deletions.
6 changes: 4 additions & 2 deletions packages/cli-shared/src/BabelRegister/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createDebug, lodash, winPath } from '../'
const debug = createDebug('xus:shared:BabelRegister')

export class BabelRegister {
only: Record<string, string[]> = {}
private only: Record<string, string[]> = {}

setOnlyMap({ key, value }: { key: string; value: string[] }) {
debug(`set ${key} of only map:`)
Expand All @@ -12,7 +12,7 @@ export class BabelRegister {
this.register()
}

register() {
private register() {
const only = lodash.uniq(
Object.keys(this.only)
.reduce<string[]>((memo, key) => {
Expand All @@ -38,3 +38,5 @@ export class BabelRegister {
})
}
}

export type IBabelRegister = InstanceType<typeof BabelRegister>
15 changes: 12 additions & 3 deletions packages/cli-shared/src/file/getFileMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/exports/pluginAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ 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'

type noopFn = () => any

export interface IPluginAPI extends IPluginAPIBase {
// service api
BabelRegister: IBabelRegister
// service lifycycle
onPluginReady: noopFn
onRunCmd: noopFn
Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/cli/Service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -28,6 +29,8 @@ export class CliService {
PluginManager
HookManager

BabelRegister

protected setStage(nextStage: CliServiceStage) {
this.stage = nextStage
}
Expand All @@ -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,
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const CONFIG_FILES = ['xus.config.js', 'xus.config.ts']
export const CONFIG_FILENAME = 'xus.config'
4 changes: 3 additions & 1 deletion packages/core/src/manager/PathManager.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit c55eb0f

Please sign in to comment.