-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
300 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
126
packages/cli-plugin-rollup-compiler/src/compiler/compileJs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/cli-plugin-rollup-compiler/src/compiler/compileReact.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
5
packages/cli-plugin-rollup-compiler/src/compiler/compileTs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/cli-plugin-rollup-compiler/src/compiler/compileVue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
}) | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './override' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
}, | ||
"extends": "../../tsconfig.json", | ||
"include": ["./src/**/*"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
] |