Skip to content

Commit

Permalink
feat: 添加type打包
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Mar 19, 2021
1 parent b8af744 commit 8e7d1bd
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 43 deletions.
1 change: 1 addition & 0 deletions packages/plugin-cmd-roll-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"mime": "^2.5.2",
"postcss": "^8.2.8",
"rollup": "^2.40.0",
"rollup-plugin-dts": "^3.0.1",
"semver": "^7.3.4",
"terser": "^5.6.0",
"url": "^0.11.0"
Expand Down
13 changes: 10 additions & 3 deletions packages/plugin-cmd-roll-lib/src/bundler/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ async function doBuild(ops: IRollupBuildOps) {
isWrite = true,
inputConfig,
outputConfigs,
alwaysEmptyDistDir
alwaysEmptyDistDir,
skipEmptyDistDir = false
} = ops
if (isWatch) {
logger.debug(`do watch: `)
Expand Down Expand Up @@ -56,7 +57,8 @@ async function doBuild(ops: IRollupBuildOps) {

await ensureDir(
path.join(pkgRoot, outputConfigs[0].dir!),
alwaysEmptyDistDir
alwaysEmptyDistDir,
skipEmptyDistDir
)
logger.debug(`start ${isWrite ? 'write' : 'generate'}`)
for (const output of outputConfigs) {
Expand All @@ -70,7 +72,12 @@ async function doBuild(ops: IRollupBuildOps) {
}
}

async function ensureDir(dir: string, alwaysEmptyDistDir = false) {
async function ensureDir(
dir: string,
alwaysEmptyDistDir = false,
skipEmptyDistDir = false
) {
if (skipEmptyDistDir) return
if (fs.existsSync(dir) && fs.statSync(dir).isDirectory()) {
const files = fs.readdirSync(dir)
if (files.length > 0) {
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-cmd-roll-lib/src/bundler/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { rollupBundler } from './bundler'
export { rollTypes, TypesDir } from './rollTypes'
export * from './types'
29 changes: 29 additions & 0 deletions packages/plugin-cmd-roll-lib/src/bundler/rollTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { IPluginAPI } from '@xus/cli-types'
import type { IRollupBuildOps } from '.'
import { runCmd, removeDirOrFile } from '@xus/cli-shared'
import { generateTypeOps } from '../resolveConfig'
import { rollupBundler } from '.'

export const TypesDir = 'xus_type'
export async function rollTypes(
pkgRoot: string,
buildOps: IRollupBuildOps,
api: IPluginAPI
) {
const res = await runCmd(
'npx',
['tsc', '--emitDeclarationOnly', '--outDir', TypesDir],
{
start: 'generate types start',
succeed: 'generate types succeed',
failed: 'generate types failed'
}
)
if (!res) return
const ops = generateTypeOps(pkgRoot, buildOps, api)
if (ops) {
api.logger.debug(`[rollup types] with `, ops)
await rollupBundler(ops)
await removeDirOrFile([api.getPathBasedOnCtx(TypesDir)])
}
}
1 change: 1 addition & 0 deletions packages/plugin-cmd-roll-lib/src/bundler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface IRollupBuildOps {
isWrite: boolean
pkgRoot: string
alwaysEmptyDistDir: boolean
skipEmptyDistDir?: boolean
}

export type IRollupBuildFn = (ops: IRollupBuildOps) => Promise<void>
18 changes: 5 additions & 13 deletions packages/plugin-cmd-roll-lib/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createPlugin } from '@xus/cli-types'
import { chalk, runCmd } from '@xus/cli-shared'
import { basename } from 'path'
import { rollupBundler } from './bundler'
import { rollupBundler, rollTypes } from './bundler'
import { resolveConfig, generateBuildOps } from './resolveConfig'
import { modifyConfig } from './modify'
// config
Expand Down Expand Up @@ -58,18 +58,10 @@ export default createPlugin({
// to rollup
api.logger.info(chalk.yellow(`running for ${basename(pkg)}`))
await rollupBundler(buildOps)
// if (resolvedConfig.rollTypes) {
// api.logger.debug(`roll types `)
// await runCmd(
// 'npx',
// ['tsc', '--emitDeclarationOnly', '--outDir', 'xus_type'],
// {
// start: 'generate types start',
// succeed: 'generate types succeed',
// failed: 'generate types failed'
// }
// )
// }
if (resolvedConfig.rollTypes) {
api.logger.debug(`rollup types `)
rollTypes(pkg, buildOps, api)
}
process.chdir(saveCwd)
}

Expand Down
92 changes: 65 additions & 27 deletions packages/plugin-cmd-roll-lib/src/resolveConfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { IArgs, IPluginAPI } from '@xus/cli-types'
import { IRollupBuildOps } from '../bundler'
import { IRollLibConfig, IResolvedConfig } from '../types'
import { isLernaPkg, orderBy, getFileMeta } from '@xus/cli-shared'
import { relative, join, isAbsolute, extname } from 'path'
import { relative, join, isAbsolute, extname, basename } from 'path'
import { TypesDir } from '../bundler'
import dts from 'rollup-plugin-dts'

export function resolveConfig(
args: IArgs,
Expand Down Expand Up @@ -103,17 +105,11 @@ export async function generateBuildOps(
inputOps.preserveEntrySignatures = 'strict'
// input to absolute
if (!inputOps.input && !resolvedConfig.entry) {
const fileMeta =
getFileMeta({
base: api.cwd,
filenameWithoutExt: 'index',
type: 'lib'
}) ||
getFileMeta({
base: api.getPathBasedOnCtx('src'),
filenameWithoutExt: 'index',
type: 'lib'
})
const fileMeta = getFileMeta({
base: api.getPathBasedOnCtx('src'),
filenameWithoutExt: 'index',
type: 'lib'
})
const entry = fileMeta?.path
if (!entry) {
throw new Error(`[generate buildOps] lib-build should have a entry`)
Expand Down Expand Up @@ -168,18 +164,60 @@ export async function generateBuildOps(
}
}

// const tsRE = /\.tsx?$/
// export function generateTypeOps(
// pkgRoot: string,
// buildOps: IRollupBuildOps,
// api: IPluginAPI
// ) {
// const entry = buildOps.inputConfig.input
// const input = ``
// if (entry) {
// if (typeof entry === 'string' && tsRE.test(entry)) {
// const ext = extname(entry)
// // input = `${entry.slice(0, -ext.length)}.d.ts`.replace()
// }
// }
// }
const tsRE = /\.tsx?$/
export function generateTypeOps(
pkgRoot: string,
buildOps: IRollupBuildOps,
api: IPluginAPI
): IRollupBuildOps | null {
const entry = buildOps.inputConfig.input
let inputs: Record<string, string> = {}
if (entry) {
if (typeof entry === 'string' && tsRE.test(entry)) {
const ext = extname(entry)
const bn = basename(entry)
inputs[bn.slice(0, -ext.length)] = entry
}
if (!Array.isArray(entry) && typeof entry !== 'string') {
inputs = {
...inputs,
...entry
}
}
const tempRoot = join(pkgRoot, TypesDir)
Object.keys(inputs).forEach((key) => {
const i = inputs[key]
let r = relative(pkgRoot, i)
if (tsRE.test(r)) {
if (r.startsWith('src')) {
r = relative('src', r)
}
const ext = extname(r)
inputs[key] = join(tempRoot, `${r.slice(0, -ext.length)}.d.ts`)
} else {
delete inputs[key]
}
})
api.logger.debug(`[generate types options] types entry: `)
api.logger.debug(inputs)
return {
inputConfig: {
input: inputs,
plugins: [dts()]
},
outputConfigs: [
{
dir: buildOps.outputConfigs?.[0]?.dir || 'dist',
format: 'es',
entryFileNames: `[name].d.ts`
}
],
isWatch: buildOps.isWatch,
isWrite: true,
pkgRoot,
alwaysEmptyDistDir: false,
skipEmptyDistDir: true
}
}
return null
}

0 comments on commit 8e7d1bd

Please sign in to comment.