Skip to content

Commit

Permalink
feat(compiler): 新增js-compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Feb 4, 2021
1 parent ac2c01b commit 5932f77
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Plugin<T = any> extends ChainedMap<T> {
})
}

use(plugin: PluginImpl, args?: any[]) {
use<T extends PluginImpl>(plugin: T, args?: Parameters<T>) {
this.set('plugin', plugin)
if (args) {
this.set('args', args)
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-plugin-bundler-rollup/src/rollupManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RollupManager {
}

// for plugin user
setup(pluginConfig: RollupPluginConfig) {
setup(pluginConfig: Partial<RollupPluginConfig>) {
// merge to default
this.rollupPluginConfig = Object.assign(defaultRollupConfig(), pluginConfig)

Expand Down
11 changes: 11 additions & 0 deletions packages/cli-plugin-rollup-compiler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `cli`

> TODO: description
## Usage

```
const cli = require('cli');
// TODO: DEMONSTRATE API
```
50 changes: 50 additions & 0 deletions packages/cli-plugin-rollup-compiler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "@xus/cli-plugin-rollup-compiler",
"version": "0.0.1",
"description": "xus cli plugin rollup compiler",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"keywords": [
"cli",
"rollup"
],
"author": "guo.xu <xuguo@outlook.it>",
"homepage": "https://github.com/xus-code/bundle-tools/tree/master/packages/cli#readme",
"license": "MIT",
"files": [
"lib"
],
"repository": {
"type": "git",
"url": "git+https://github.com/xus-code/bundle-tools.git"
},
"scripts": {
"dev:compile": "tsc --watch",
"build:compile": "tsc"
},
"bugs": {
"url": "https://github.com/xus-code/bundle-tools/issues"
},
"dependencies": {
"@babel/core": "^7.12.10",
"@rollup/plugin-babel": "^5.2.3",
"@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-node-resolve": "^11.1.0",
"@vue/compiler-sfc": "^3.0.5",
"@xus/babel-preset": "^0.1.1",
"@xus/cli": "^0.0.1",
"@xus/cli-plugin-bundler-rollup": "^0.0.1",
"chalk": "^4.1.0",
"deepmerge": "^4.2.2",
"rollup": "^2.38.3",
"rollup-plugin-alias": "^2.2.0",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-vue": "^6.0.0"
},
"devDependencies": {
"@types/rollup-plugin-node-builtins": "^2.1.1",
"@types/rollup-plugin-node-globals": "^1.4.0"
}
}
19 changes: 19 additions & 0 deletions packages/cli-plugin-rollup-compiler/src/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { IPluginAPI } from '@xus/cli'
import { compileJs, compileTs, compileReact, compileVue } from './compiler'

export async function build(cmd: string, api: IPluginAPI): Promise<void> {
switch (cmd) {
case 'js':
await compileJs(api)
break
case 'ts':
await compileTs(api)
break
case 'react':
await compileReact(api)
break
case 'vue':
await compileVue(api)
break
}
}
126 changes: 126 additions & 0 deletions packages/cli-plugin-rollup-compiler/src/compiler/compileJs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import type { IPluginAPI } from '@xus/cli'
// rollup plugins
import nodeResolvePlugin from '@rollup/plugin-node-resolve'
import commonjsPlugin from '@rollup/plugin-commonjs'
import { getBabelOutputPlugin } from '@rollup/plugin-babel'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import aliasPlugin from 'rollup-plugin-alias'

export async function compileJs(api: IPluginAPI): Promise<void> {
// 1. base override config
// 1.1 load babel config
const babelConfig = await api.ConfigManager.loadConfig(
api.PathManager.getPath('babel.config.js')
)
// 1.2 register common config
api.RollupManager.registerChainFn((rollupChain) => {
rollupChain
.when('all')
.input(api.PathManager.getPath('src/index.js'))
.treeshake.moduleSideEffects(false)
.end()
.plugin('nodeResolve')
.use(nodeResolvePlugin, [{ preferBuiltins: true }])
.before('commonjs')
.end()
.plugin('commonjs')
.use(commonjsPlugin, [{ sourceMap: false }])
.before('babel')
.end()
.plugin('alias')
.use(aliasPlugin, [{ '@': api.PathManager.getPath('src') }])
.end()
}, true)
// 1.3 register babel config
api.RollupManager.registerChainFn((rollupChain) => {
rollupChain
.when('esm-bundler')
.output.file(api.PathManager.getPath('lib/index.esm-bundler.js'))
.end()
.end()
.when('esm-browser')
.output.file(api.PathManager.getPath('lib/index.esm-browser.js'))
.end()
.end()
.when('global')
.output.file(api.PathManager.getPath('lib/index.global.js'))
.end()
.end()
.when('node')
.output.file(api.PathManager.getPath('lib/index.cjs.js'))
.end()
.end()

if (babelConfig) {
rollupChain
.when('all')
.plugin('babel')
.use(getBabelOutputPlugin, [babelConfig])
} else {
rollupChain
.when('esm-bundler')
.plugin('babel')
.use(getBabelOutputPlugin, [
{
presets: [
[
'@xus/babel-preset',
{ useESModules: true, absoluteRuntime: false }
]
]
}
])
.end()
.end()
.when('esm-browser')
.plugin('babel')
.use(getBabelOutputPlugin, [
{
presets: [
[
'@xus/babel-preset',
{
useESModules: true,
useTransformRuntime: false,
useDynamicImport: true
}
]
]
}
])
.end()
.end()
.when('global')
.plugin('babel')
.use(getBabelOutputPlugin, [
{
presets: [
[
'@xus/babel-preset',
{ useTransformRuntime: false, useDynamicImport: true }
]
]
}
])
.end()
.end()
.when('node')
.plugin('babel')
.use(getBabelOutputPlugin, [
{
presets: [
[
'@xus/babel-preset',
{ absoluteRuntime: false, useDynamicImport: true }
]
]
}
])
.end()
.end()
}
}, true)
// 2. run build
return api.RollupManager.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IPluginAPI } from '@xus/cli'

export async function compileReact(api: IPluginAPI): Promise<void> {
console.log(`compile React `, api)
}
5 changes: 5 additions & 0 deletions packages/cli-plugin-rollup-compiler/src/compiler/compileTs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IPluginAPI } from '@xus/cli'

export async function compileTs(api: IPluginAPI): Promise<void> {
console.log(`compile ts `, api)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IPluginAPI } from '@xus/cli'

export async function compileVue(api: IPluginAPI): Promise<void> {
console.log(`compile vue `, api)
}
5 changes: 5 additions & 0 deletions packages/cli-plugin-rollup-compiler/src/compiler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const compileCmds = ['js', 'ts', 'react', 'vue']
export * from './compileJs'
export * from './compileTs'
export * from './compileReact'
export * from './compileVue'
55 changes: 55 additions & 0 deletions packages/cli-plugin-rollup-compiler/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { IPluginAPI, ProjectConfig } from '@xus/cli'
import type {
RollupPluginConfig,
CompileTargets
} from '@xus/cli-plugin-bundler-rollup'
import type { FinalArgs } from './types'
import chalk from 'chalk'
import { error } from '@xus/cli'
import { compileCmds } from './compiler'
import { build } from './commands'

export default function (api: IPluginAPI, projectConfig: ProjectConfig): void {
api.registerCommand(
'rollup',
{
usage: 'xus rollup <command> [options]',
desc: 'a js/ts/react/vue bundler based on rollup',
options: {
'--targets': 'point build target',
'--sourcemap': 'generate sourcemap'
}
},
async (args: FinalArgs) => {
// valid buildCmd
const buildCmd = args._.shift()
if (!buildCmd || !compileCmds.includes(buildCmd)) {
error(
`\n invalid command: ${chalk.red(buildCmd)}` +
`\n support commands: ${chalk.green(
`${compileCmds.join(' / ')}`
)}\n`
)
process.exit(1)
}
// handle of options
if (args?.sourcemap) {
api.RollupManager.registerChainFn((rollupChain) => {
rollupChain.when('all').output.sourcemap(true)
})
}
// handle of plugin config
const rollupPluginConfig = projectConfig?.pluginOps?.rollup || {}
if (args?.targets) {
;(rollupPluginConfig as RollupPluginConfig).targets = args.targets.split(
','
) as CompileTargets[]
}
api.RollupManager.setup(rollupPluginConfig)

return build(buildCmd, api).catch((err) => {
console.log(`build failed ${err}`)
})
}
)
}
1 change: 1 addition & 0 deletions packages/cli-plugin-rollup-compiler/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './override'
6 changes: 6 additions & 0 deletions packages/cli-plugin-rollup-compiler/src/types/override.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Args } from '@xus/cli'

export type FinalArgs = {
targets?: string
sourcemap?: string
} & Args
7 changes: 7 additions & 0 deletions packages/cli-plugin-rollup-compiler/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"outDir": "./dist"
},
"extends": "../../tsconfig.json",
"include": ["./src/**/*"]
}
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@xus/cli-plugin-bundler-rollup": "^0.0.1",
"@xus/cli-plugin-command-commit-lint": "^0.0.1",
"@xus/cli-plugin-command-help": "^0.0.1",
"@xus/cli-plugin-rollup-compiler": "^0.0.1",
"chalk": "^4.1.0",
"joi": "^17.3.0",
"minimist": "^1.2.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const builtInMap = [
'@xus/cli-plugin-bundler-rollup',
'@xus/cli-plugin-command-help',
'@xus/cli-plugin-command-commit-lint'
'@xus/cli-plugin-command-commit-lint',
'@xus/cli-plugin-rollup-compiler'
]

0 comments on commit 5932f77

Please sign in to comment.