Skip to content

Commit

Permalink
feat: build lib 初步完成
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Feb 25, 2021
1 parent c55eb0f commit 3394e50
Show file tree
Hide file tree
Showing 39 changed files with 2,550 additions and 226 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
rules: {
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/no-non-null-assertion': 0,
'@typescript-eslint/explicit-module-boundary-types': 0
'@typescript-eslint/explicit-module-boundary-types': 0,
'@typescript-eslint/ban-ts-comment': 0
}
}
16 changes: 9 additions & 7 deletions packages/cli-shared/src/BabelRegister/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ export class BabelRegister {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('@babel/register')({
presets: [
require.resolve('@xus/babel-preset'),
{
targets: { node: 'current' },
useTransformRuntime: false,
useTypescript: true
}
[
require('@xus/babel-preset'),
{
targets: { node: 'current' },
useTransformRuntime: false,
useTypescript: true
}
]
],
ignore: [/node_modules/],
only,
extensions: ['.jsx', '.js', '.ts', '.tsx'],
extensions: ['.js', '.jsx', '.ts', '.tsx'],
babelrc: false,
cache: false
})
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-shared/src/compatESModuleRequire/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function compatESModuleRequire<
T extends { __esModule: boolean; default: any }
>(m: T): T extends { __esModule: true; default: infer U } ? U : T {
return m.__esModule ? m.default : m
return m?.__esModule ? m.default : m
}
5 changes: 1 addition & 4 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
"dependencies": {
"@xus/cli-shared": "^0.0.1",
"@xus/core": "^0.0.1",
"@xus/plugin-build-lib": "^0.0.1",
"@xus/plugin-bundler-rollup": "^0.0.1",
"@xus/preset-built-in": "^0.0.1",
"minimist": "^1.2.5"
"@xus/preset-built-in": "^0.0.1"
},
"devDependencies": {
"@types/minimist": "^1.2.1"
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/exports/create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { IPlugin as IPluginBase, IProjectConfig } from '@xus/core'
import type { ILibBuildConfig } from '@xus/preset-built-in'
import type { IPluginAPI } from './pluginAPI'

export type IConfig = IProjectConfig
export interface IConfig extends Partial<IProjectConfig> {
libBuild: Partial<ILibBuildConfig>
}

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

Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/exports/pluginAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import type {
IRawArgs
} from '@xus/core'
import type { IBabelRegister } from '@xus/cli-shared'
import { IBuildLibMethods, IBundlerRollupMethods } from '@xus/preset-built-in'
import type { IConfig } from './create'
import { IBuildLibMethods } from '@xus/plugin-build-lib'
import { IBundlerRollupMethods } from '@xus/plugin-bundler-rollup'

type noopFn = () => any

Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ export { CliService, HookTypes } from '@xus/core'
// compose plugin with core
export * from './exports'
// plugins
export * from '@xus/plugin-build-lib'
export * from '@xus/plugin-bundler-rollup'
export * from '@xus/preset-built-in'

// types
export type {
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/xus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// export types
import minimist from 'minimist'
import { yParser } from '@xus/cli-shared'
import { XUSCliService } from './Service'
// 1. init cli
// 2. get args
const rawArgs = process.argv.slice(2)
const args = minimist(rawArgs)
const args = yParser(rawArgs)
const commandName = args._[0]
const ops = {
mode: args?.mode || 'development'
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/cli/PluginAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ class PluginAPI {
ops?.fn ||
// point this to caller
function (fn: IRegisterMethodArgs) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.registerHook({
name: methodName,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
pluginName: this.pluginName,
...(typeof fn === 'function' ? { fn } : fn)
Expand Down
12 changes: 2 additions & 10 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import type { IProjectConfig } from './types'
import type { IConfigSchemaValidator } from '@xus/cli-shared'
import { createSchema, validateSchema } from '@xus/cli-shared'
import { createSchema } from '@xus/cli-shared'

export const ProjectConfigSchema = createSchema<IProjectConfig>((joi) => {
return joi.object({
return joi.object().keys({
mode: joi.string(),
plugins: joi.array(),
presets: joi.array()
})
})

export const projectConfigValidator: IConfigSchemaValidator<IProjectConfig> = (
obj,
cb
) => {
return validateSchema(obj, ProjectConfigSchema, cb)
}

export function defaultProjectConfig(
ops: Partial<IProjectConfig>
): IProjectConfig {
Expand Down
28 changes: 11 additions & 17 deletions packages/core/src/manager/ConfigManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IProjectConfig, IPluginConfig, IPackage } from '../types'
import type { ICliService } from '../cli/Service'
import { Logger, loadModule, deepmerge } from '@xus/cli-shared'
import { projectConfigValidator, defaultProjectConfig } from '../config'
import { Logger, loadModule, deepmerge, validateSchema } from '@xus/cli-shared'
import { defaultProjectConfig, ProjectConfigSchema } from '../config'

type IProjectConfigPartial = Partial<IProjectConfig>

Expand All @@ -18,7 +18,7 @@ export class ConfigManager {
private pkgJson

private pluginConfigs = new Map<string, IPluginConfig>()
private pluginValidators = new Map<string, IPluginConfig['validator']>()
private projectSchema = ProjectConfigSchema
private pluginDefaults: Record<string, any> = {}

constructor(ops: IConfigManagerOps) {
Expand Down Expand Up @@ -79,21 +79,13 @@ export class ConfigManager {
}

validConfig() {
logger.debug(`valid config `)
logger.debug(this.projectSchema)
// valid project
projectConfigValidator(this.finalConfig, (msg) => {
validateSchema(this.finalConfig, this.projectSchema, (msg) => {
logger.error(`project config invalid ${msg}`)
process.exit(1)
})

// valid plugin config
for (const [pluginName, pluginValidator] of this.pluginValidators) {
if (pluginName in this.finalConfig) {
pluginValidator!(this.finalConfig[pluginName], (msg) => {
logger.error(`project config invalid ${msg}`)
process.exit(1)
})
}
}
logger.debug(`valid config success`)
}

Expand All @@ -111,10 +103,12 @@ export class ConfigManager {
this.pluginConfigs.set(pluginName, pluginConfig)
// split plugin config
if (pluginConfig.default) {
this.pluginDefaults[pluginName] = pluginConfig.default()
this.pluginDefaults[pluginConfig.key] = pluginConfig.default()
}
if (pluginConfig.validator) {
this.pluginValidators.set(pluginName, pluginConfig.validator)
if (pluginConfig.schema) {
this.projectSchema = this.projectSchema.keys({
[pluginConfig.key]: pluginConfig.schema
})
}
}

Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/manager/HookManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class HookManager {

register(hook: IHook) {
const { name } = hook
logger.debug(`register hook ${name}`)
logger.debug(hook)
// TODO: valid name fn ??
this.hooksMap.set(name, [...(this.hooksMap.get(name) || []), hook])
}
Expand All @@ -30,6 +32,8 @@ export class HookManager {
async apply<T = void>(ops: IHookApplyOps) {
const { name, type } = ops
const hooks = this.hooksMap.get(name) || []
logger.debug(`run hook ${name} `)
logger.debug(hooks)
if (hooks.length < 1 && !builtinHooks.includes(name)) {
logger.wran(`hook ${name} is empty`)
}
Expand All @@ -38,7 +42,7 @@ export class HookManager {
// eslint-disable-next-line no-case-declarations
const eventHook = new AsyncSeriesWaterfallHook(['_'])
for (const hook of hooks) {
if (this.service.PluginManager.pluginIsEnable(hook.pluginName))
if (this.service.PluginManager.pluginIsDisable(hook.pluginName))
continue
eventHook.tapPromise(
{
Expand All @@ -58,7 +62,7 @@ export class HookManager {
// eslint-disable-next-line no-case-declarations
const addHook = new AsyncSeriesWaterfallHook<any[]>(['memo'])
for (const hook of hooks) {
if (this.service.PluginManager.pluginIsEnable(hook.pluginName))
if (this.service.PluginManager.pluginIsDisable(hook.pluginName))
continue
addHook.tapPromise(
{
Expand All @@ -78,7 +82,7 @@ export class HookManager {
// eslint-disable-next-line no-case-declarations
const serialHook = new AsyncSeriesWaterfallHook<any>(['memo'])
for (const hook of hooks) {
if (this.service.PluginManager.pluginIsEnable(hook.pluginName))
if (this.service.PluginManager.pluginIsDisable(hook.pluginName))
continue
serialHook.tapPromise(
{
Expand All @@ -97,7 +101,7 @@ export class HookManager {
// eslint-disable-next-line no-case-declarations
const parallelHook = new AsyncParallelHook(['_'])
for (const hook of hooks) {
if (this.service.PluginManager.pluginIsEnable(hook.pluginName))
if (this.service.PluginManager.pluginIsDisable(hook.pluginName))
continue
parallelHook.tapPromise(
{
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/manager/PluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,17 @@ export class PluginManager {
logger.debug(`apply plugins success`)
}

pluginIsEnable(pluginName: string) {
pluginIsDisable(pluginName: string) {
// custom skip
if (this.skipPluginNames.has(pluginName)) return false
if (this.skipPluginNames.has(pluginName)) return true

// config set to false
const pluginConfig =
this.service.ConfigManager.projectConfig[pluginName] || null
if (pluginConfig === false) return false
if (pluginConfig === false) return true

// TODO: plugin enableBy ??
return false
}

// for plugin api
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IConfigSchemaValidator } from '@xus/cli-shared'
import type { IConfigSchema } from '@xus/cli-shared'

// plugins / presets
// api type override in @xus/cli
Expand All @@ -22,8 +22,10 @@ export interface IPreset {
}

export interface IPluginConfig<T = any> {
key: string
default?: () => T
validator?: IConfigSchemaValidator<T>
// validator?: IConfigSchemaValidator<T>
schema?: IConfigSchema
// when config change how to do
// onChange
}
1 change: 1 addition & 0 deletions packages/plugin-build-lib/src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BuildLibMethods } from './enum'

export default createPlugin({
name: 'lib:build',
enforce: 'pre',
apply(api) {
// global method
api.registerMethod({
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-build-lib/src/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BuildLibMethods } from './enum'
// for register method
export interface IBuildLibMethods {
[BuildLibMethods.ModifyLibBundler]: IFastHookRegister<
(bundler: IBundlerImp) => IBundlerImp
(bundler: IBundler) => IBundler
>
[BuildLibMethods.OnLibBuildFailed]: IFastHookRegister<(e: any) => void>
[BuildLibMethods.OnLibBuildSucceed]: IFastHookRegister<
Expand Down Expand Up @@ -34,7 +34,7 @@ export interface IBundlerImp {
[key: string]: any
}

export interface IBundler extends IBundlerImp {
export interface IBundler {
new (api: IPluginAPI): IBundlerImp
}

Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-bundler-rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"dependencies": {
"@xus/cli": "^0.0.1",
"@xus/plugin-build-lib": "^0.0.1",
"rollup": "^2.38.3",
"rollup-plugin-terser": "^7.0.2"
"rollup": "^2.38.3"
}
}
9 changes: 5 additions & 4 deletions packages/plugin-bundler-rollup/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { createPlugin } from '@xus/cli'
import { Methods } from './types'
import { BundlerRollupMethods } from './types'
import RollupBundler from './rollupBundler'

export default createPlugin({
name: 'rollup:bundler',
enforce: 'pre',
name: 'bundler:rollup',
apply(api) {
api.registerMethod({
methodName: Methods.ModifyRollupConfig,
methodName: BundlerRollupMethods.ModifyRollupConfig,
throwOnExist: false
})
api.modifyLibBundler(() => RollupBundler)
}
})
Loading

0 comments on commit 3394e50

Please sign in to comment.