Skip to content

Commit

Permalink
perf: 优化打包产出
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Feb 26, 2021
1 parent 3394e50 commit a69a346
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 328 deletions.
3 changes: 0 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,5 @@
"@xus/cli-shared": "^0.0.1",
"@xus/core": "^0.0.1",
"@xus/preset-built-in": "^0.0.1"
},
"devDependencies": {
"@types/minimist": "^1.2.1"
}
}
15 changes: 7 additions & 8 deletions packages/plugin-bundler-rollup/src/rollupBundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ class RollupBundler implements IBundlerImp {
pointPkg: ''
}
) {
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`))
if (isLernaPkg(this.api.cwd)) {
return await this.doBuildForLerna({
await this.doBuildForLerna({
target,
...rest
})
} else {
return await this.doBuild({
await this.doBuild({
target,
...rest
})
Expand All @@ -72,7 +75,6 @@ class RollupBundler implements IBundlerImp {
if (config) {
// 2. validate config
this.validConfig(config)
logger.info(chalk.yellow(`build ${target} bundler start \n`))
// real build
const { input, ...restConfig } = config
if (Array.isArray(input)) {
Expand Down Expand Up @@ -114,15 +116,12 @@ class RollupBundler implements IBundlerImp {

private async rollup(config: IRollupConfig) {
const { input, output, ...buildConfig } = config
logger.info(`Rollup ${input}...`)
logger.info(chalk.green(`Rollup ${input}... -> ${output.file}...`))
const bundler = await rollup({
input,
...buildConfig
})
if (output) {
logger.info(`write ${output.file}...`)
await bundler.write(output)
}
await bundler.write(output)
}
}

Expand Down
24 changes: 23 additions & 1 deletion packages/plugin-bundler-rollup/src/rollupChian/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,23 @@ class Output<T = any> extends ChainedMap<T> {
globals!: IChainedMapSet<OutputOptions['globals'], this>
name!: IChainedMapSet<string, this>
paths!: IChainedMapSet<OutputOptions['paths'], this>
esModule!: IChainedMapSet<boolean, this>
compact!: IChainedMapSet<boolean, this>
sourcemap!: IChainedMapSet<boolean, this>
sourcemapFile!: IChainedMapSet<OutputOptions['sourcemapFile'], this>
format!: IChainedMapSet<OutputOptions['format'], this>
banner!: IChainedMapSet<OutputOptions['banner'], this>
footer!: IChainedMapSet<OutputOptions['footer'], this>
intro!: IChainedMapSet<OutputOptions['intro'], this>
outro!: IChainedMapSet<OutputOptions['outro'], this>
amd!: IChainedMapSet<OutputOptions['amd'], this>
exports!: IChainedMapSet<OutputOptions['exports'], this>
assetFileNames!: IChainedMapSet<OutputOptions['assetFileNames'], this>
chunkFileNames!: IChainedMapSet<OutputOptions['chunkFileNames'], this>
entryFileNames!: IChainedMapSet<OutputOptions['entryFileNames'], this>
namespaceToStringTag!: IChainedMapSet<boolean, this>
inlineDynamicImports!: IChainedMapSet<boolean, this>
manualChunks!: IChainedMapSet<OutputOptions['manualChunks'], this>

constructor(parent: T) {
super(parent)
Expand All @@ -21,12 +32,23 @@ class Output<T = any> extends ChainedMap<T> {
'globals',
'name',
'paths',
'esModule',
'compact',
'sourcemap',
'sourcemapFile',
'format',
'banner',
'footer',
'intro',
'outro'
'outro',
'amd',
'exports',
'assetFileNames',
'chunkFileNames',
'entryFileNames',
'namespaceToStringTag',
'inlineDynamicImports',
'manualChunks'
])
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-cmd-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@
},
"devDependencies": {
"@types/semver": "^7.3.4"
},
"peerDependencies": {
"core-js": "^3.9.0"
}
}
15 changes: 11 additions & 4 deletions packages/plugin-cmd-lib/src/getBabelConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ export default (ctx: IModifyRollupConfigCtx, isVue = false) => {
[
preset,
{
modules: false,
useESModules: true,
absoluteRuntime: false
absoluteRuntime: false,
vueJSX: isVue
}
]
]
Expand All @@ -22,9 +24,12 @@ export default (ctx: IModifyRollupConfigCtx, isVue = false) => {
[
preset,
{
modules: false,
usageMode: true,
useESModules: true,
useTransformRuntime: false
useTransformRuntime: false,
useDynamicImport: true,
vueJSX: isVue
}
]
]
Expand All @@ -37,7 +42,8 @@ export default (ctx: IModifyRollupConfigCtx, isVue = false) => {
{
targets: { node: 'current' },
absoluteRuntime: false,
useDynamicImport: true
useDynamicImport: true,
vueJSX: isVue
}
]
]
Expand All @@ -50,7 +56,8 @@ export default (ctx: IModifyRollupConfigCtx, isVue = false) => {
{
usageMode: true,
useTransformRuntime: false,
useDynamicImport: true
useDynamicImport: true,
vueJSX: isVue
}
]
]
Expand Down
27 changes: 22 additions & 5 deletions packages/plugin-cmd-lib/src/modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,23 @@ export const ensureOutput = (
if (ctx.esm) {
rc.output.file(outFile.replace(/.js$/, getExt('esm', isProd))).format('es')
} else if (ctx.cjs) {
rc.output.file(outFile.replace(/.js$/, getExt('cjs', isProd))).format('cjs')
rc.output
.file(outFile.replace(/.js$/, getExt('cjs', isProd)))
.format('cjs')
.exports('named')
} else if (ctx.browser) {
if (!globalName)
throw new Error(`build for browser should has a global name`)

rc.output
.file(outFile.replace(/.js$/, getExt('global', isProd)))
.format('iife')
.format('umd')
.globals({
vue: 'Vue',
react: 'React',
'react-dom': 'ReactDom'
})
.exports('named')
} else if (ctx.modern) {
rc.output
.file(outFile.replace(/.js$/, getExt('modern', isProd)))
Expand Down Expand Up @@ -112,8 +116,16 @@ export const ensureCommonPlugin = (
}

rc.plugin(BuiltInRollupPlugin.Json).use(json)
rc.plugin(BuiltInRollupPlugin.Alias).use(alias)
rc.plugin(BuiltInRollupPlugin.Replace).use(replace)
rc.plugin(BuiltInRollupPlugin.Alias).use(alias, [
{
entries: [{ find: '@', replacement: join(api.cwd, 'src') }]
}
])
rc.plugin(BuiltInRollupPlugin.Replace).use(replace, [
{
preventAssignment: true
}
])
}

export const ensureCorePlugin = (
Expand Down Expand Up @@ -147,6 +159,7 @@ export const ensureCorePlugin = (
}
])
}
api.logger.debug(`vue version ${vueVersion}`)

// babel
rc.plugin(BuiltInRollupPlugin.Babel).use(babel, [
Expand All @@ -172,6 +185,9 @@ export const ensureCorePlugin = (
`current build for typescript, but don't have a tsconfig.json`
)

// vue jsxFactory h
const vueTsx = vueVersion ? { jsx: 'preserve', jsxFactory: 'h' } : {}

rc.plugin(BuiltInRollupPlugin.Typescript)
.use(typescript2, [
{
Expand All @@ -181,7 +197,8 @@ export const ensureCorePlugin = (
tsconfig: tsconfigPath,
tsconfigDefaults: {
compilerOptions: {
declaration: true
declaration: true,
...vueTsx
}
},
tsconfigOverride: {
Expand Down
14 changes: 10 additions & 4 deletions packages/plugin-cmd-lib/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,28 @@ export default createPlugin({
usage: 'xus lib',
options: {
'--pkg': 'point pkg dir name with RegExp',
'--target': 'point build target esm|cjs|browser|modern',
'--targets': 'point build target esm|cjs|browser|modern',
'--watch': 'watch mode'
}
},
(args) => {
api.logger.debug(`raw args `)
api.logger.debug(args)

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

api.logger.debug(`build targets `)
api.logger.debug(targets)

api.logger.debug(`modify config `)
// default config should run before user modify
api.modifyRollupConfig({
Expand Down
11 changes: 0 additions & 11 deletions packages/plugin-rollup-compiler/README.md

This file was deleted.

43 changes: 0 additions & 43 deletions packages/plugin-rollup-compiler/package.json

This file was deleted.

16 changes: 0 additions & 16 deletions packages/plugin-rollup-compiler/src/commands.ts

This file was deleted.

0 comments on commit a69a346

Please sign in to comment.