Skip to content

Commit

Permalink
feat: 更换babelRegister为esbuildRegister
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Mar 15, 2021
1 parent 71ea0ab commit 03a1e94
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 66 deletions.
1 change: 1 addition & 0 deletions packages/cli-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"debug": "^4.3.1",
"deepmerge": "^4.2.2",
"enquirer": "^2.3.6",
"esbuild-register": "^2.2.0",
"execa": "^5.0.0",
"fast-glob": "^3.2.5",
"joi": "^17.4.0",
Expand Down
44 changes: 0 additions & 44 deletions packages/cli-shared/src/BabelRegister/index.ts

This file was deleted.

34 changes: 34 additions & 0 deletions packages/cli-shared/src/EsbuildRegister/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createDebug, lodash, winPath } from '..'
import { register } from 'esbuild-register/dist/node'
const debug = createDebug('xus:shared:EsbuildRegister')
export class EsbuildRegister {
private only: Record<string, string[]> = {}

setOnlyMap({ key, value }: { key: string; value: string[] }) {
debug(`set ${key} of only map:`)
debug(value)
this.only[key] = value
this.register()
}

private register() {
const only = lodash.uniq(
Object.keys(this.only)
.reduce<string[]>((memo, key) => {
return memo.concat(this.only[key])
}, [])
.map(winPath)
)
only.forEach((file) => {
register({
sourcefile: file,
sourcemap: false,
extensions: ['.js', '.jsx', '.ts', '.tsx'],
format: 'cjs',
target: 'es2019'
})
})
}
}

export type IEsbuildRegister = InstanceType<typeof EsbuildRegister>
2 changes: 1 addition & 1 deletion packages/cli-shared/src/file/getFileMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface IGetFileMetaOps {

const extsMap = {
js: ['.ts', '.js'],
lib: ['.js', 'jsx', '.ts', '.tsx', '.vue'],
lib: ['.js', 'jsx', '.ts', '.tsx'],
css: ['.css', '.scss', '.sass', '.less']
}

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 @@ -20,7 +20,7 @@ export { createEnvNameWithXusPrefix } from './env'
export * from './file'
export { loadModule } from './loadModule'
export * from './pkg'
export { BabelRegister, IBabelRegister } from './BabelRegister'
export { EsbuildRegister, IEsbuildRegister } from './EsbuildRegister'
export * from './rimraf'
export * from './runCmd'
export * from './orderBy'
Expand Down
4 changes: 2 additions & 2 deletions packages/cli-types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
IPluginManager,
IProjectConfig
} from '@xus/core'
import type { IBabelRegister, IRunCmdMessage } from '@xus/cli-shared'
import type { IEsbuildRegister, IRunCmdMessage } from '@xus/cli-shared'
// preset api
import type { IRollupChain, IRollupChainConfig } from '@xus/rollup-chain'
import webpackChain from 'webpack-chain'
Expand All @@ -27,7 +27,7 @@ type noopFn = () => any

export interface IPluginAPI extends IPluginAPIBase {
// service api
BabelRegister: IBabelRegister
EsbuildRegister: IEsbuildRegister
// service lifycycle
onSetuped: IFastHookRegister<(config: IConfig) => void>
onRunCmd: noopFn
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/cli/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
IRawArgs,
IProjectConfig
} from '../types'
import { Logger, BabelRegister } from '@xus/cli-shared'
import { Logger, EsbuildRegister } from '@xus/cli-shared'
import { CliServiceStage, HookTypes } from '../enums'
import { CONFIG_FILES } from '../constants'
import {
Expand Down Expand Up @@ -35,7 +35,7 @@ export class CliService {
PluginManager
HookManager

BabelRegister
EsbuildRegister

protected setStage(nextStage: CliServiceStage) {
this.stage = nextStage
Expand All @@ -47,7 +47,7 @@ export class CliService {
logger.debug(ops)
// 1. init manager
this.setStage(CliServiceStage.initManager)
this.BabelRegister = new BabelRegister()
this.EsbuildRegister = new EsbuildRegister()
this.PathManager = new PathManager()
this.EnvManager = new EnvManager({
service: this,
Expand All @@ -74,7 +74,7 @@ export class CliService {
logger.debug(this.HookManager)

// 2. init config (without plugin config)
this.BabelRegister.setOnlyMap({
this.EsbuildRegister.setOnlyMap({
key: 'xus:config',
value: CONFIG_FILES
})
Expand Down Expand Up @@ -162,7 +162,7 @@ export class CliService {
return new Proxy(api, {
get: (target, key: string) => {
// proxy to service
if (['BabelRegister'].includes(key)) {
if (['EsbuildRegister'].includes(key)) {
return (this as any)[key]
}
// registerMethods
Expand Down
11 changes: 6 additions & 5 deletions packages/core/src/manager/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ export class ConfigManager {
private loadUserConfig() {
// 1. load userConfig
const path = this.service.PathManager.userConfigPath
const userConfig = path
? loadModule<IProjectConfig>(path, (err) => {
logger.error(`user config load failed, ${err}`)
})
: {}
const userConfig =
path === ' '
? loadModule<IProjectConfig>(path, (err) => {
logger.error(`user config load failed, ${err}`)
})
: {}

logger.debug(`user config: `)
logger.debug(userConfig)
Expand Down
15 changes: 7 additions & 8 deletions packages/core/src/manager/PathManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { join, relative } from 'path'
import { statSync, readdirSync } from 'fs'
import { getFileMeta, isLernaPkg } from '@xus/cli-shared'
import { CONFIG_FILENAME } from '../constants'
import { isLernaPkg, lookUpFile } from '@xus/cli-shared'
import { CONFIG_FILES } from '../constants'

export class PathManager {
private configPath!: string
get cwd(): string {
return process.cwd()
}
Expand All @@ -13,12 +14,10 @@ export class PathManager {
}

get userConfigPath() {
const fileMeta = getFileMeta({
base: this.cwd,
filenameWithoutExt: CONFIG_FILENAME,
type: 'js'
})
return fileMeta && fileMeta.path
if (!this.configPath) {
this.configPath = lookUpFile(this.cwd, CONFIG_FILES, true) || ' '
}
return this.configPath
}

// for plugins
Expand Down

0 comments on commit 03a1e94

Please sign in to comment.