Skip to content

Commit

Permalink
fix: 修复js-compiler构建问题
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Feb 5, 2021
1 parent e673b07 commit 13ca469
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 56 deletions.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,17 @@
"hooks": {
"commit-msg": "npx xus commit-lint"
}
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
23 changes: 18 additions & 5 deletions packages/cli-plugin-bundler-rollup/src/rollupChian/config.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import type { RollupOptions } from 'rollup'
import type { ChainedMapSet, CompileConfig } from './lib/types'
// ops
import { ChainedMap } from './lib'
import { ChainedMap, ChainedSet } from './lib'
import Output from './output'
import Plugin, { IPlugin } from './plugin'
import Treeshake from './treeshake'
import Watch from './watch'

class Config<T = any> extends ChainedMap<T> {
input!: ChainedMapSet<RollupOptions['input'], this>
external!: ChainedMapSet<RollupOptions['external'], this>
cache!: ChainedMapSet<RollupOptions['cache'], this>
onwarn!: ChainedMapSet<RollupOptions['onwarn'], this>
context!: ChainedMapSet<RollupOptions['context'], this>
external
watch
treeshake
output
private plugins

constructor(parent: T) {
super(parent)
this.external = new ChainedSet<this, string | RegExp>(this)
this.output = new Output<this>(this)
this.treeshake = new Treeshake<this>(this)
this.watch = new Watch<this>(this)
this.plugins = new ChainedMap<this>(this)
this.extend(['input', 'external', 'cache', 'onwarn', 'context'])
this.extend(['input', 'cache', 'onwarn', 'context'])
}

plugin(name: string) {
Expand All @@ -33,6 +34,7 @@ class Config<T = any> extends ChainedMap<T> {

toConfig() {
const entries = this.entries() || {}
entries.external = this.external.values()
entries.output = this.output.entries()
entries.treeshake = this.treeshake.toConfig()
entries.watch = this.watch.entries()
Expand All @@ -47,7 +49,8 @@ class Config<T = any> extends ChainedMap<T> {

entries() {
const entries = super.entries() || {}
// patch output plugins
// patch other
entries.external = this.external.values()
entries.output = this.output.entries()
entries.plugins = this.plugins.entries()
entries.treeshake = this.treeshake.toConfig()
Expand All @@ -57,7 +60,17 @@ class Config<T = any> extends ChainedMap<T> {
}

mergeBase(obj: Record<string, any>) {
super.mergeBase(obj, ['plugins', 'output', 'treeshake', 'watch'])
super.mergeBase(obj, [
'plugins',
'output',
'treeshake',
'watch',
'external'
])
// merge external
if ('external' in obj) {
this.external.mergeBase(obj?.external || [])
}
// merge output
if ('output' in obj) {
this.output.mergeBase(obj?.output || {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ChainedSet<T = any, U = any> extends Chainable<T> {
}

// values
protected values() {
values() {
return [...this.store]
}

Expand Down
9 changes: 4 additions & 5 deletions packages/cli-plugin-bundler-rollup/src/rollupManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,15 @@ class RollupManager {
api.EnvManager.babelEnv = target
// build
const { output, ...bundleOps } = config
logWithSpinner(chalk.yellow(`run ${target} build...`))
console.info(chalk.blue(`\n${bundleOps.input} -> ${output.file}`))
console.info(chalk.blueBright(`\n${bundleOps.input} -> ${output.file}`))
logWithSpinner(`run ${target} build...`)
try {
const bundle = await rollup(bundleOps)
await bundle.write(output)
succeedSpinner(`${target} build succeed`)
} catch (error) {
failSpinner(chalk.red(`build failed`))
throw error
failSpinner(`${target} build faild ${error}`)
}
succeedSpinner(chalk.green(`${target} build success`))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-plugin-rollup-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-node-resolve": "^11.1.0",
"@vue/compiler-sfc": "^3.0.5",
"@xus/babel-preset": "^0.1.3",
"@xus/babel-preset": "^0.1.6",
"@xus/cli": "^0.0.1",
"@xus/cli-plugin-bundler-rollup": "^0.0.1",
"chalk": "^4.1.0",
Expand Down
21 changes: 17 additions & 4 deletions packages/cli-plugin-rollup-compiler/src/compiler/compileJs.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { IPluginAPI } from '@xus/cli'
import DefaultBabelConfig from '../config/babel.config'
import createBabelConfig from '../config/babel.config'
// rollup plugins
import nodeResolvePlugin from '@rollup/plugin-node-resolve'
import commonjsPlugin from '@rollup/plugin-commonjs'
import { getBabelOutputPlugin } from '@rollup/plugin-babel'
import { babel } from '@rollup/plugin-babel'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import aliasPlugin from 'rollup-plugin-alias'
Expand All @@ -13,7 +13,7 @@ export async function compileJs(api: IPluginAPI): Promise<void> {
const babelConfig =
(await api.ConfigManager.loadConfig(
api.PathManager.getPath('babel.config.js')
)) || DefaultBabelConfig
)) || createBabelConfig('@xus/babel-preset')
// 1.2 register common config
api.RollupManager.registerChainFn((rollupChain) => {
rollupChain
Expand Down Expand Up @@ -60,10 +60,23 @@ export async function compileJs(api: IPluginAPI): Promise<void> {
.plugin('commonjs')
.end()

// base babel
rollupChain
.when('all')
.plugin('babel')
.use(getBabelOutputPlugin, [babelConfig])
.use(babel, [{ ...babelConfig, babelHelpers: 'runtime' }])
// set external
rollupChain.when('esm-bundler').external.set(/^@babel\/runtime/)
rollupChain.when('node').external.set(/^@babel\/runtime/)
// set full pkg babel
rollupChain
.when('global')
.plugin('babel')
.use(babel, [{ ...babelConfig, babelHelpers: 'bundled' }])
rollupChain
.when('esm-browser')
.plugin('babel')
.use(babel, [{ ...babelConfig, babelHelpers: 'bundled' }])
}, true)
// 2. run build
return api.RollupManager.build(api)
Expand Down
29 changes: 17 additions & 12 deletions packages/cli-plugin-rollup-compiler/src/config/babel.config.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
export default {
type Preset =
| '@xus/babel-preset'
| '@xus/babel-preset/lib/react'
| '@xus/babel-preset/lib/vue'

export default (preset: Preset) => ({
exclude: 'node_modules/**',
env: {
'esm-bundler': {
presets: [
[
'@xus/babel-preset',
preset,
{
useESModules: true,
absoluteRuntime: false,
useDynamicImport: false
absoluteRuntime: false
}
]
]
},
'esm-browser': {
presets: [
[
'@xus/babel-preset',
preset,
{
usageMode: true,
useESModules: true,
useTransformRuntime: false,
useDynamicImport: false
useTransformRuntime: false
}
]
]
},
global: {
presets: [
[
'@xus/babel-preset',
preset,
{
usageMode: true,
useTransformRuntime: false,
useDynamicImport: true,
modules: 'umd'
useDynamicImport: true
}
]
]
},
node: {
presets: [
[
'@xus/babel-preset',
preset,
{
targets: { node: 'current' },
absoluteRuntime: false,
Expand All @@ -49,4 +54,4 @@ export default {
]
}
}
}
})
3 changes: 3 additions & 0 deletions packages/cli-plugin-rollup-compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ export default function (api: IPluginAPI, projectConfig: ProjectConfig): void {
)
process.exit(1)
}
// set env
if (args?.prod) {
api.EnvManager.mode = 'production'
} else {
api.EnvManager.mode = 'development'
}
// handle of options
if (args?.sourcemap) {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@xus/cli-plugin-rollup-compiler": "^0.0.1",
"chalk": "^4.1.0",
"joi": "^17.3.0",
"log-symbols": "^4.0.0",
"minimist": "^1.2.5",
"ora": "^5.3.0"
},
Expand Down
48 changes: 24 additions & 24 deletions packages/cli/src/utils/spinner.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import ora from 'ora'
import chalk from 'chalk'
import logSymbols from 'log-symbols'

type LastMsg = { symbol: string; text: string } | null
type OraIns = ora.Ora | null

const spinner = ora()
let lastMsg: LastMsg = null
let oraIns: OraIns = null

export const logWithSpinner = (symbol: string, msg?: string) => {
if (!msg) {
msg = symbol
symbol = chalk.green('✔')
export const logWithSpinner = (msg: string) => {
if (oraIns) {
oraIns.stop()
}
if (lastMsg) {
spinner.stop()
}
spinner.text = ' ' + msg
lastMsg = {
symbol: symbol + ' ',
text: msg
}
spinner.start()
spinner.text = chalk.yellow(' ' + msg)
oraIns = spinner.start()
}

export const stopSpinner = () => {
if (!spinner.isSpinning) {
if (!oraIns) {
return
}
if (lastMsg) {
spinner.stop()
}
lastMsg = null
oraIns.stop()
oraIns = null
}

export const failSpinner = (text: string) => {
spinner.fail(text)
export const failSpinner = (text?: string) => {
oraIns &&
oraIns.stopAndPersist({
symbol: logSymbols.error,
text: chalk.red(text || oraIns.text)
})
}

export const succeedSpinner = (text: string) => {
spinner.succeed(text)
export const succeedSpinner = (text?: string) => {
oraIns &&
oraIns.stopAndPersist({
symbol: logSymbols.success,
text: chalk.green(text || oraIns.text)
})
}
9 changes: 5 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2230,11 +2230,12 @@
resolved "https://npm.c2cloud.cn/@vue/shared/download/@vue/shared-3.0.5.tgz#c131d88bd6713cc4d93b3bb1372edb1983225ff0"
integrity sha1-wTHYi9ZxPMTZOzuxNy7bGYMiX/A=

"@xus/babel-preset@^0.1.3":
version "0.1.3"
resolved "https://registry.yarnpkg.com/@xus/babel-preset/-/babel-preset-0.1.3.tgz#bfcfc9b01e9e5f6c22281b504b986ef7590dfec0"
integrity sha512-J+0saSE0B141sJ8/gZSuN1a5mHGeA+BOaycxIEF6aUDEgd787LdCSrO2TyZjZqw8bGO9pWxhf1H/MxAU7bmglQ==
"@xus/babel-preset@^0.1.6":
version "0.1.6"
resolved "https://registry.yarnpkg.com/@xus/babel-preset/-/babel-preset-0.1.6.tgz#6cfb148b41f2b197d1fd65b938f8552fc832a131"
integrity sha512-kao+wbk3STgEvcgFhgHgH1IZMCzGD+Nl0rl905qBNhRzA/FTd1uwW/I3rQA/w5pPjiUgGbMPK9B8taduqxjnIw==
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"
Expand Down

0 comments on commit 13ca469

Please sign in to comment.