Skip to content

Commit

Permalink
refactor: 重构lib-build改为esbuild处理
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Mar 15, 2021
1 parent 3b3e9b2 commit 435bcad
Show file tree
Hide file tree
Showing 69 changed files with 1,221 additions and 1,121 deletions.
3 changes: 2 additions & 1 deletion packages/cli-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"homepage": "https://github.com/xus-code/bundle-tools/tree/master/packages/cli-utils#readme",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
Expand Down Expand Up @@ -53,4 +54,4 @@
"@types/rimraf": "^3.0.0",
"@types/yargs-parser": "^20.2.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# `cli`
# `@xus/cli-types`

> TODO: description
## Usage

```
const cli = require('cli');
const cliUtils = require('@xus/cli-types');
// TODO: DEMONSTRATE API
```
36 changes: 36 additions & 0 deletions packages/cli-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@xus/cli-types",
"version": "0.1.0",
"description": "cli utils",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"keywords": [
"utils"
],
"author": "guo.xu <xuguo@outlook.it>",
"homepage": "https://github.com/xus-code/bundle-tools/tree/master/packages/cli-utils#readme",
"license": "MIT",
"files": [
"dist"
],
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/xus-code/cli.git"
},
"bugs": {
"url": "https://github.com/xus-code/cli/issues"
},
"scripts": {
"build:types": "tsc"
},
"dependencies": {
"@xus/cli-shared": "^0.1.4",
"@xus/core": "^0.1.5",
"@xus/rollup-chain": "^0.2.6",
"esbuild": "^0.9.2",
"webpack-chain": "^6.5.1"
}
}
26 changes: 26 additions & 0 deletions packages/cli-types/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import webpackChain from 'webpack-chain'
// empty
import { IPlugin, IPreset, IConfig } from './types'

type IReturnArg<T extends any> = (args: T) => T

type PartialDeep<T extends Record<string, any>> = {
[K in keyof T]?: T[K] extends Record<string, unknown>
? PartialDeep<T[K]>
: T[K]
}

export const defineConfig: IReturnArg<PartialDeep<IConfig>> = (config) => config

export const createPlugin: IReturnArg<IPlugin> = (plugin) => plugin

export const createPreset: IReturnArg<IPreset> = (preset) => preset

// types
export * from './types'

export type { IArgs, IRawArgs, ICommand } from '@xus/core'

export type { IRollupChain, IRollupChainConfig } from '@xus/rollup-chain'

export type IWebpackChain = webpackChain
124 changes: 124 additions & 0 deletions packages/cli-types/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import {
IPlugin as IPluginBase,
IFastHookRegister,
IArgs,
IRawArgs,
IPluginAPI as IPluginAPIBase,
IPathManager,
IConfigManager,
IEnvManager,
IPluginManager,
IProjectConfig
} from '@xus/core'
import type { IBabelRegister, IRunCmdMessage } from '@xus/cli-shared'
// preset api
import type { IRollupChain, IRollupChainConfig } from '@xus/rollup-chain'
import webpackChain from 'webpack-chain'
import { TransformOptions } from 'esbuild'

// Hooks Types
// type IEvent = any
// type ISerial = any
// type IParallel = any
// type IAdd = any

// IPluginAPI
type noopFn = () => any

export interface IPluginAPI extends IPluginAPIBase {
// service api
BabelRegister: IBabelRegister
// service lifycycle
onSetuped: (config: IConfig) => void
onRunCmd: noopFn
getCmdArgs: () => { args: IArgs; rawArgs: IRawArgs }
modifyProjectConfig: (config: IConfig) => void

// path manager
cwd: IPathManager['cwd']
cwdPkg: IPathManager['cwdPkg']
userConfigPath: IPathManager['userConfigPath']
getPathBasedOnCtx: IPathManager['getPathBasedOnCtx']
getLernaPkgs: IPathManager['getLernaPkgs']

// env manager
mode: IEnvManager['mode']
babelEnv: IEnvManager['babelEnv']
getEnv: IEnvManager['getEnv']
setEnv: IEnvManager['setEnv']
getCliEnv: IEnvManager['getCliEnv']
setCliEnv: IEnvManager['setCliEnv']

// config manager
projectConfig: IConfig
cwdPkgJson: IConfigManager['cwdPkgJson']
loadConfig: IConfigManager['loadConfig']

// plugin manager
skipPlugin: IPluginManager['skipPlugin']

// rollup
modifyRollupConfig: IFastHookRegister<(rc: IRollupChain) => IRollupChain>
getRollupConfig: () => Promise<IRollupChainConfig>
// webpack
modifyWebpackConfig: IFastHookRegister<(wc: webpackChain) => webpackChain>
getWebpackConfig: () => Promise<any>
}

// IConfig
export interface IConfig extends IProjectConfig {
libBuild: {
libName: string
// transform: 'esbuild' | 'babel'
minify: false | 'esbuild' | 'terser'
target: 'esnext' | TransformOptions['target']
formats: ('esm' | 'cjs' | 'iife' | 'umd')[]
rollTypes: boolean
sourcemap: boolean
alwaysEmptyDistDir: boolean

// lerna mode
pkgsOrder: string[]

// insider
rollupChain: (rc: IRollupChain) => IRollupChain
}
lint: {
eslint:
| boolean
| {
include?: string
ext?: string[]
}
stylelint:
| boolean
| {
include?: string[]
}
}
changelog: {
filename: string
mainTemplate: string
headerPartial: string
commitPartial: string
}
release: {
// before hooks for run lint test build...
beforeRelease: {
bin: string
args: string[]
message: IRunCmdMessage
}[]
// in lenra mode to ensure pkg publish order
order: string[]
branch: string
}
}

// IPlugin
export type IPlugin = IPluginBase<(api: IPluginAPI) => void>

// IPreset
export interface IPreset {
plugins: IPlugin[]
}
File renamed without changes.
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
"url": "https://github.com/xus-code/bundle-tools/issues"
},
"dependencies": {
"@xus/cli-types": "^0.1.0",
"@xus/cli-shared": "^0.1.4",
"@xus/core": "^0.1.5",
"@xus/preset-built-in": "^0.1.7"
}
}
}
32 changes: 0 additions & 32 deletions packages/cli/src/exports/create.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/cli/src/exports/index.ts

This file was deleted.

54 changes: 0 additions & 54 deletions packages/cli/src/exports/pluginAPI.ts

This file was deleted.

12 changes: 1 addition & 11 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
export * from '@xus/cli-shared'
export { CliService, HookTypes } from '@xus/core'
// compose plugin with core
export * from './exports'
// plugins
export * from '@xus/preset-built-in'

// types
export type {
IArgs,
IRawArgs,
ICommand,
IHook,
IFastHookRegister
} from '@xus/core'
export * from '@xus/cli-types'
21 changes: 17 additions & 4 deletions packages/core/src/cli/Service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// types
import type { ICliServerOps, ICommand, IArgs, IRawArgs } from '../types'
import type {
ICliServerOps,
ICommand,
IArgs,
IRawArgs,
IProjectConfig
} from '../types'
import { Logger, BabelRegister } from '@xus/cli-shared'
import { CliServiceStage, HookTypes } from '../enums'
import { CONFIG_FILES } from '../constants'
Expand Down Expand Up @@ -102,7 +108,7 @@ export class CliService {
this.PluginManager.applyPlugins()
// plugin register hooks ready
await this.HookManager.apply({
name: 'pluginsReady',
name: 'onSetuped',
type: HookTypes.event,
args: this.ConfigManager.projectConfig
})
Expand All @@ -111,9 +117,16 @@ export class CliService {
getPluginAPI(ops: { pluginName: string }) {
const api = new PluginAPI({ ...ops, service: this })
// register service lifycycle
;['onConfigReady', 'onPluginReady', 'onRunCmd'].forEach((methodName) => {
;['onSetuped', 'onRunCmd'].forEach((methodName) => {
api.registerMethod({ methodName, throwOnExist: false })
})
api.registerMethod({
methodName: 'modifyProjectConfig',
throwOnExist: false,
fn: (config: IProjectConfig) => {
this.ConfigManager.modifyProjectConfig(config)
}
})
// register cmd args getter
api.registerMethod({
methodName: 'getCmdArgs',
Expand Down Expand Up @@ -201,7 +214,7 @@ export class CliService {
// 4. emit a start event
this.setStage(CliServiceStage.runCmd)
await this.HookManager.apply({
name: 'runCmd',
name: 'onRunCmd',
type: HookTypes.event
})
// for api
Expand Down

0 comments on commit 435bcad

Please sign in to comment.