Skip to content

Commit

Permalink
feat: 添加watch模式
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Mar 1, 2021
1 parent 10ae089 commit 66f4f46
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 49 deletions.
15 changes: 6 additions & 9 deletions packages/plugin-build-lib/src/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,26 @@ export interface IBuildLibMethods {
>
[BuildLibMethods.OnLibBuildFailed]: IFastHookRegister<(e: any) => void>
[BuildLibMethods.OnLibBuildSucceed]: IFastHookRegister<
(stats: ILibBuildRes) => void
(stats: ILibBuildStats) => void
>
[BuildLibMethods.RunLibBuild]: (ops: Partial<ILibBuildOps>) => void
}

// for bundler imp
export interface ILibBuildStats {
info: string
}

export type ILibBuildRes = {
[key in ILibBuildTargets]?: ILibBuildStats
info?: string
watch: boolean
}

export interface ILibBuildOps {
targets: ILibBuildTargets[]
watch: boolean
pointPkg: string
[key: string]: any
pointPkg?: string[]
order?: string[]
}

export interface IBundlerImp {
build: (ops: ILibBuildOps) => Promise<ILibBuildRes>
build: (ops: ILibBuildOps) => Promise<ILibBuildStats>
[key: string]: any
}

Expand Down
85 changes: 62 additions & 23 deletions packages/plugin-bundler-rollup/src/rollupBundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { IPluginAPI, IBundlerImp, ILibBuildTargets } from '@xus/cli'
import type { IRollupChain } from './rollupChian'
import type { IRollupConfig } from './rollupChian/lib/types'
import type { IDoBuildOps } from './types'
import { relative } from 'path'
import { rollup } from 'rollup'
import { basename, join } from 'path'
import { rollup, watch } from 'rollup'
import { Logger, HookTypes, isLernaPkg, chalk } from '@xus/cli'
import { getModifyConfigCtx } from './utils'
import { BundlerRollupMethods } from './types'
Expand Down Expand Up @@ -40,15 +40,18 @@ class RollupBundler implements IBundlerImp {
async build(
ops = {
targets: ['esm', 'cjs', 'browser'],
watch: false,
pointPkg: ''
watch: false
}
) {
logger.debug(`build ops `)
logger.debug(ops)
const { targets, ...rest } = ops
for (const target of targets as ILibBuildTargets[]) {
logger.info(chalk.yellow(`build ${target} bundler start \n`))
logger.info(
chalk.yellow(
`[${rest.watch ? 'Watch' : 'Rollup'}] ${target} bundler start \n`
)
)
if (isLernaPkg(this.api.cwd)) {
this.api.setCliEnv('Lerna_Root', this.api.cwd)
await this.doBuildForLerna({
Expand All @@ -62,11 +65,13 @@ class RollupBundler implements IBundlerImp {
})
}
}
return {}
return {
watch: ops.watch
}
}

private async doBuild(ops: IDoBuildOps) {
const { target } = ops
const { target, watch } = ops
logger.debug(`get rollup config: `)
const config = await this.getConfig(target)
logger.debug(config)
Expand All @@ -80,13 +85,16 @@ class RollupBundler implements IBundlerImp {
const { input, ...restConfig } = config
if (Array.isArray(input)) {
for (const i of input) {
await this.rollup({
input: i,
...restConfig
})
await this.rollup(
{
input: i,
...restConfig
},
watch
)
}
} else {
await this.rollup(config)
await this.rollup(config, watch)
}
} else {
logger.wran(`build target ${target} has no config to rollup`)
Expand All @@ -97,10 +105,20 @@ class RollupBundler implements IBundlerImp {
private async doBuildForLerna(ops: IDoBuildOps) {
// lerna pkg
logger.debug(`do lerna pkgs build `)
const pkgRE = ops?.pointPkg ? new RegExp(ops.pointPkg) : false
const pkgs = this.api
const order = ops?.order
const pointPkg = ops?.pointPkg
const filterPkgs = this.api
.getLernaPkgs()
.filter((pkg) => (pkgRE ? pkgRE.test(relative(this.api.cwd, pkg)) : true))
.filter((pkg) => (pointPkg ? pointPkg.includes(basename(pkg)) : true))
// to order
let pkgs: string[] = []
if (!order) {
pkgs = filterPkgs
} else {
const fp = filterPkgs.map((p) => basename(p))
const dirname = join(this.api.cwd, 'packages')
pkgs = order.filter((o) => fp.includes(o)).map((o) => join(dirname, o))
}
logger.debug(pkgs)

const saveCwd = this.api.cwd
Expand All @@ -115,14 +133,35 @@ class RollupBundler implements IBundlerImp {
return {}
}

private async rollup(config: IRollupConfig) {
const { input, output, ...buildConfig } = config
logger.info(chalk.green(`Rollup ${input}... -> ${output.file}...`))
const bundler = await rollup({
input,
...buildConfig
})
await bundler.write(output)
private async rollup(config: IRollupConfig, isWatch = false) {
if (isWatch) {
const watcher = watch([
{
...config,
watch: config?.watch ? config.watch : {}
}
])
watcher.on('event', (event: any) => {
if (event.error) {
this.api.logger.error(event.error)
} else if (event.code === 'START') {
this.api.logger.log(`Rebuild since file changed`)
}
})
process.once('SIGINT', () => {
watcher.close()
})
} else {
const { input, output, ...buildConfig } = config
logger.info(chalk.green(`[write] ${input} -> ${output.file}`))
const bundler = await rollup({
input,
...buildConfig,
watch: false
})
await bundler.write(output)
await bundler.close()
}
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/plugin-bundler-rollup/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { ILibBuildTargets, IFastHookRegister } from '@xus/cli'
import type {
ILibBuildTargets,
IFastHookRegister,
ILibBuildOps
} from '@xus/cli'
import type { IRollupChain } from './rollupChian'

// for plugin
Expand All @@ -16,9 +20,6 @@ export type IBundlerRollupMethods = {
>
}

// for bundler
export interface IDoBuildOps {
export type IDoBuildOps = {
target: ILibBuildTargets
watch: boolean
pointPkg: string
}
} & Omit<ILibBuildOps, 'targets'>
10 changes: 4 additions & 6 deletions packages/plugin-cmd-lib/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import type { IConfigSchemaValidator } from '@xus/cli'
import type { ILibBuildConfig } from './types'
import { createSchema, validateSchema } from '@xus/cli'
import { createSchema } from '@xus/cli'

export const libBuildSchema = createSchema<ILibBuildConfig>((joi) =>
joi.object({
targets: joi.array(),
pkg: joi.string(),
pointPkgs: joi.array(),
pkgOrder: joi.array(),
rollupChain: joi.function()
})
)

export const defaultLibBuildConfig: () => ILibBuildConfig = () => {
return {
targets: ['esm', 'cjs', 'browser', 'modern'],
pkg: '',
rollupChain: (rc) => rc
targets: ['esm', 'cjs', 'browser']
}
}
10 changes: 7 additions & 3 deletions packages/plugin-cmd-lib/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default createPlugin({
desc: 'bunlde js ts react for lib mode, based on rollup',
usage: 'xus lib',
options: {
'--pkg': 'point pkg dir name with RegExp',
'--pkg': 'point pkg dir name',
'--targets': 'point build target esm|cjs|browser|modern',
'--watch': 'watch mode'
}
Expand Down Expand Up @@ -73,10 +73,14 @@ export default createPlugin({

// run
api.logger.debug(`run lib build `)
const pointPkgs = args?.pkg
? (args?.pkg as string).split(',')
: undefined
api.runLibBuild({
targets: targets as ILibBuildTargets[],
pointPkg: config?.pkg || args?.pkg || '',
watch: !!args?.watch
pointPkg: config?.pointPkgs || pointPkgs || undefined,
watch: !!args?.watch,
order: config?.pkgOrder
})

// on success
Expand Down
17 changes: 15 additions & 2 deletions packages/plugin-cmd-lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@ import type {
} from '@xus/cli'

export interface ILibBuildConfig {
// lib build target: esm cjs broeser modern
targets: ILibBuildTargets[]
pkg: string
rollupChain: (
/**
* point pkg name
* package/core
* pkg: ['core']
*/
pointPkgs?: string[]
// to custom rollup config
rollupChain?: (
rollupChain: IRollupChain,
ctx: IModifyRollupConfigCtx
) => IRollupChain
/**
* lib packing is orderly when lerna mode
* like ['shared', 'core']
* shared should be roll before core
*/
pkgOrder?: string[]
}

export type IPkg = {
Expand Down

0 comments on commit 66f4f46

Please sign in to comment.