Skip to content

Commit

Permalink
feat: 新增rollup types
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Mar 5, 2021
1 parent 7106a86 commit a885398
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 46 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@
"hooks": {
"commit-msg": "npx xus commit-lint"
}
}
},
"browserslist": [
"current node"
]
}
4 changes: 2 additions & 2 deletions packages/plugin-build-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"dependencies": {
"@xus/cli": "^0.1.5",
"@xus/rollup-chain": "^0.2.2",
"@xus/rollup-chain": "^0.2.3",
"rollup": "^2.40.0"
}
}
}
36 changes: 20 additions & 16 deletions packages/plugin-build-lib/src/bundler/rollupBundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ export class RollupBundler implements IBundlerImp {
logger.debug(`build ops `)
logger.debug(ops)
const { targets, ...rest } = ops
const islerna = isLernaPkg(this.api.cwd)
this.memoPkgs = null
for (const target of targets as ILibBuildTargets[]) {
logger.info(
chalk.yellow(
`[${rest.watch ? 'Watch' : 'Rollup'}] ${target} bundler start \n`
)
)
if (isLernaPkg(this.api.cwd)) {
if (islerna) {
this.api.setCliEnv('Lerna_Root', this.api.cwd)
await this.doBuildForLerna({
target,
Expand Down Expand Up @@ -93,27 +95,29 @@ export class RollupBundler implements IBundlerImp {
return {}
}

private memoPkgs: string[] | null = null
private async doBuildForLerna(ops: IDoBuildOps) {
// lerna pkg
logger.debug(`do lerna pkgs build `)
const order = ops?.order
const pointPkg = ops?.pointPkg
const filterPkgs = this.api
.getLernaPkgs()
.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 = orderBy(fp, order || []).map((o) => join(dirname, o))
if (!this.memoPkgs) {
const order = ops?.order
const pointPkg = ops?.pointPkg
const filterPkgs = this.api
.getLernaPkgs()
.filter((pkg) => (pointPkg ? pointPkg.includes(basename(pkg)) : true))
// to order
if (!order) {
this.memoPkgs = filterPkgs
} else {
const fp = filterPkgs.map((p) => basename(p))
const dirname = join(this.api.cwd, 'packages')
this.memoPkgs = orderBy(fp, order || []).map((o) => join(dirname, o))
}
logger.debug(this.memoPkgs)
}
logger.debug(pkgs)

const saveCwd = this.api.cwd
for (const pkg of pkgs) {
for (const pkg of this.memoPkgs) {
process.chdir(pkg)
await this.doBuild(ops)
}
Expand Down
7 changes: 4 additions & 3 deletions packages/plugin-build-lib/src/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ export interface IBundler {
}

// for esm tagret
export type ILibBuildTargets = 'esm' | 'cjs' | 'browser' | 'modern'
type ITargets = 'esm' | 'cjs' | 'browser' | 'modern'
export type ILibBuildTargets = ITargets | 'rollTypes'

export type IModifyRollupConfigCtx = {
[key in ILibBuildTargets]: boolean
}

export interface ILibBuildConfig {
// lib build target: esm cjs broeser modern
targets: ILibBuildTargets[]
targets: ITargets[]
/**
* point pkg name
* package/core
Expand All @@ -65,7 +66,7 @@ export interface ILibBuildConfig {
// to custom rollup config
rollupChain?: (
rollupChain: IRollupChain,
ctx: IModifyRollupConfigCtx
ctx: Omit<IModifyRollupConfigCtx, 'rollTypes'>
) => IRollupChain
/**
* lib packing is orderly when lerna mode
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-build-lib/src/plugin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export const getModifyConfigCtx = (target: ILibBuildTargets) => {
esm: false,
cjs: false,
browser: false,
modern: false
modern: false,
rollTypes: false
}
ctx[target] = true
return ctx
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-cmd-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@
"@rollup/plugin-url": "^6.0.0",
"@svgr/rollup": "^5.5.0",
"@vue/compiler-sfc": "^3.0.6",
"@xus/babel-preset": "^0.1.9",
"@xus/babel-preset": "^0.1.11",
"@xus/cli": "^0.1.5",
"@xus/rollup-plugin-vue2": "^1.0.0",
"autoprefixer": "^10.2.4",
"postcss": "^8.2.6",
"rollup-plugin-dts": "^2.0.1",
"rollup-plugin-postcss": "^4.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.30.0",
"rollup-plugin-vue": "^6.0.0-beta.10",
"semver": "^7.3.4",
"vue-template-compiler": "^2.6.12"
},
"devDependencies": {
Expand All @@ -53,4 +53,4 @@
"peerDependencies": {
"core-js": "^3.9.0"
}
}
}
3 changes: 2 additions & 1 deletion packages/plugin-cmd-lib/src/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export enum BuiltInRollupPlugin {
Svgr = 'svgr',
Url = 'url',
Json = 'json',
Terser = 'terser'
Terser = 'terser',
Dts = 'dts'
}

export const ExternalMatchBabelReg = new RegExp(/^@babel\/runtime/)
30 changes: 21 additions & 9 deletions packages/plugin-cmd-lib/src/modify.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { IRollupChain, IModifyRollupConfigCtx, IPluginAPI } from '@xus/cli'
import type { IPkg } from './types'
import { getFileMeta, lookUpFile, resolve } from '@xus/cli'
import semver from 'semver'
import { join, extname } from 'path'
import { getFileMeta, lookUpFile, resolve, semver } from '@xus/cli'
import { join, extname, dirname } from 'path'
import { BuiltInRollupPlugin, ExternalMatchBabelReg } from './enum'
import getBabelConfig from './getBabelConfig'
// rollup plugin
Expand All @@ -20,12 +19,15 @@ import typescript2 from 'rollup-plugin-typescript2'
import babel from '@rollup/plugin-babel'
import vue2 from '@xus/rollup-plugin-vue2'
import vue3 from 'rollup-plugin-vue'
import dts from 'rollup-plugin-dts'
// css
import postcss from 'rollup-plugin-postcss'
import autoprefixer from 'autoprefixer'

const extensions = ['.js', '.jsx', '.ts', '.tsx', '.vue']

let isCheckedTypes = false

export const defaultInput = (
rc: IRollupChain,
_: IModifyRollupConfigCtx,
Expand Down Expand Up @@ -91,6 +93,11 @@ export const ensureOutput = (
rc.output
.file(outFile.replace(/.js$/, getExt('modern', isProd)))
.format('es')
} else if (ctx.rollTypes) {
rc.clear()
rc.input(join(dirname(outFile), 'index.d.ts'))
rc.plugin(BuiltInRollupPlugin.Dts).use(dts)
rc.output.file(join(dirname(outFile), 'index.d.ts')).format('es')
}

api.logger.debug(rc.output.get('file'))
Expand All @@ -108,13 +115,14 @@ export const ensureCommonPlugin = (
rc.plugin(BuiltInRollupPlugin.NodeResolve).use(nodeResolve, [
{
mainFields: ['jsnext:main', 'module', 'main'],
preferBuiltins: true,
extensions
}
])
if (ctx.browser || ctx.modern) {
// need rollup a full pkg
rc.plugin(BuiltInRollupPlugin.Commonjs)
.use(commonjs)
.use(commonjs, [{ sourceMap: false }])
.after(BuiltInRollupPlugin.NodeResolve)
}

Expand All @@ -141,7 +149,6 @@ export const ensureCorePlugin = (
// auto sniff vue
let vueVersion: null | number = null
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Vue = require(resolve.sync('vue', {
basedir: api.getCliEnv('Lerna_Root') || api.cwd
}))
Expand Down Expand Up @@ -203,25 +210,30 @@ export const ensureCorePlugin = (
.use(typescript2, [
{
cwd: api.cwd,
check: !isCheckedTypes,
clean: true,
cacheRoot: `${api.cwd}/.rpts2_cache`,
tsconfig: tsconfigPath,
tsconfigDefaults: {
compilerOptions: {
declaration: true,
...vueTsx
}
},
tsconfigOverride: {
compilerOptions: {
target: 'esnext'
target: 'esnext',
module: 'esnext',
// sourceMap: !isCheckedTypes,
declaration: !isCheckedTypes,
declarationMap: !isCheckedTypes
}
},
check: true
}
}
])
// tsc --> babel
.before(BuiltInRollupPlugin.Babel)

isCheckedTypes = true
}

// terser
Expand Down
18 changes: 12 additions & 6 deletions packages/plugin-cmd-lib/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default createPlugin({
options: {
'--pkg': 'point pkg dir name',
'--targets': 'point build target esm|cjs|browser|modern',
'--watch': 'watch mode'
'--watch': 'watch mode',
'--rollTypes': 'to rollup types files'
}
},
(args) => {
Expand All @@ -30,13 +31,16 @@ export default createPlugin({

const config = api.projectConfig.libBuild
// handle of args
let targets = config?.targets || DefaultTargets
let targets = (config?.targets || DefaultTargets) as ILibBuildTargets[]
if (args?.targets) {
targets = (args?.targets as string)
.split(',')
.filter((target) =>
DefaultTargets.includes(target as ILibBuildTargets)
)
DefaultTargets.includes(target)
) as ILibBuildTargets[]
}
if (args?.rollTypes) {
targets.push('rollTypes')
}

api.logger.debug(`build targets `)
Expand Down Expand Up @@ -83,8 +87,10 @@ export default createPlugin({
})

// on success
api.onLibBuildSucceed(() => {
api.logger.success(`lib build succeed!!!`)
api.onLibBuildSucceed((ops) => {
if (!ops?.watch) {
api.logger.success(`lib build succeed!!!`)
}
})
// on failed
api.onLibBuildFailed((e) => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"declaration": true,
"skipLibCheck": true,
"esModuleInterop": true,
"moduleResolution": "node",
"lib": ["esnext", "dom"]
},
"exclude": ["**/__test__/**/*"]
Expand Down
42 changes: 38 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3304,6 +3304,31 @@
resolved "https://registry.yarnpkg.com/@vue/shared/-/shared-3.0.6.tgz#d65576430fc4ad383dc7c829118798e5169178d4"
integrity sha512-c37C60HpelUZIx+SNZVEINSxkFzQYhIXFg5AynnIA4QDBmY4iSPoACfGSwSUTCTKImukPeCgY2oqRJVP3R1Mnw==

"@xus/babel-preset@^0.1.11":
version "0.1.11"
resolved "https://registry.yarnpkg.com/@xus/babel-preset/-/babel-preset-0.1.11.tgz#35acf74b8ed5f4ca2386dc8a439f8e6d28cac836"
integrity sha512-/M1K663BfkgYiMvSPlTq+gte0iWTBkZiQRyeSNSYBfUbfA/0Fmr+yUsVMFXmRhDWf9h0e7zfMYlVQ5gx3q5Oqg==
dependencies:
"@babel/core" "^7.12.10"
"@babel/plugin-proposal-class-properties" "7.12.1"
"@babel/plugin-proposal-decorators" "7.12.1"
"@babel/plugin-proposal-nullish-coalescing-operator" "7.12.1"
"@babel/plugin-proposal-optional-chaining" "7.12.1"
"@babel/plugin-syntax-dynamic-import" "^7.8.3"
"@babel/plugin-transform-react-display-name" "7.12.1"
"@babel/plugin-transform-runtime" "^7.12.1"
"@babel/preset-env" "^7.12.1"
"@babel/preset-react" "^7.12.1"
"@babel/preset-typescript" "^7.12.1"
"@babel/runtime-corejs3" "^7.12.5"
"@vue/babel-helper-vue-jsx-merge-props" "^1.2.1"
"@vue/babel-plugin-jsx" "^1.0.2"
"@vue/babel-preset-jsx" "^1.2.4"
babel-plugin-dynamic-import-node "^2.3.3"
babel-plugin-transform-react-remove-prop-types "0.4.24"
core-js "^3.8.1"
semver "^7.3.4"

"@xus/babel-preset@^0.1.9":
version "0.1.9"
resolved "https://registry.yarnpkg.com/@xus/babel-preset/-/babel-preset-0.1.9.tgz#20a6a8f0d811d3027efb1e1baab1adff2a303db0"
Expand Down Expand Up @@ -3357,10 +3382,10 @@
stylelint-no-unsupported-browser-features "^4.1.4"
stylelint-order "^4.1.0"

"@xus/rollup-chain@^0.2.2":
version "0.2.2"
resolved "https://registry.yarnpkg.com/@xus/rollup-chain/-/rollup-chain-0.2.2.tgz#cddc844a239a0146221c2c302b7a86983194a4fc"
integrity sha512-SxuDyHm4xQ06A/9H7THptgTk05dfoV1VmJFVwE9t6rRqUkmjdOSzpcKQ0sy66p2BJODHuAPC118CR/qdaAsCGw==
"@xus/rollup-chain@^0.2.3":
version "0.2.3"
resolved "https://registry.yarnpkg.com/@xus/rollup-chain/-/rollup-chain-0.2.3.tgz#abd3b69ea6d065a84c5bd7c04750615eb6c4002f"
integrity sha512-sNhUrlPN7XMFTNFSG7+1cnVAb/ZFICZdEMuzSUdlzNgy+VUIfkUZC/5e/+GKcyTOGiOnYIxRbxj9w3Zq+rCM6Q==

"@xus/rollup-plugin-vue2@^1.0.0":
version "1.0.0"
Expand Down Expand Up @@ -11601,6 +11626,15 @@ rimraf@^3.0.0, rimraf@^3.0.2:
dependencies:
glob "^7.1.3"

rollup-plugin-dts@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-dts/-/rollup-plugin-dts-2.0.1.tgz#333f50a637e199a073d490b198746f3c6bd07701"
integrity sha512-y38NSXIY37YExCumbGBTL5dXg7pL7XD+Kbe98iEHWFN9yiKJf7t4kKBOkml5ylUDjQIXBnNClGDeRktc1T5dmA==
dependencies:
magic-string "^0.25.7"
optionalDependencies:
"@babel/code-frame" "^7.10.4"

rollup-plugin-postcss@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-postcss/-/rollup-plugin-postcss-4.0.0.tgz#2131fb6db0d5dce01a37235e4f6ad4523c681cea"
Expand Down

0 comments on commit a885398

Please sign in to comment.