-
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
29 changed files
with
2,847 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
module.exports = { | ||
env: { | ||
node: true, | ||
node: true | ||
}, | ||
extends: [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier", | ||
"prettier/@typescript-eslint", | ||
'eslint:recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'prettier', | ||
'prettier/@typescript-eslint' | ||
], | ||
parser: "@typescript-eslint/parser", | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: "module", | ||
sourceType: 'module' | ||
}, | ||
plugins: ["@typescript-eslint"], | ||
plugins: ['@typescript-eslint'], | ||
rules: { | ||
"@typescript-eslint/no-explicit-any": 0, | ||
"@typescript-eslint/no-non-null-assertion": 0, | ||
}, | ||
}; | ||
'@typescript-eslint/no-explicit-any': 0, | ||
'@typescript-eslint/no-non-null-assertion': 0, | ||
'@typescript-eslint/explicit-module-boundary-types': 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
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,24 @@ | ||
import type { IPluginAPI } from '@xus/cli' | ||
import type { IRollupManager } from './rollupManager' | ||
import { compileJs, compileTs, compileReact, compileVue } from './compiler' | ||
|
||
export async function build( | ||
cmd: string, | ||
rollupManager: IRollupManager, | ||
api: IPluginAPI | ||
): Promise<void> { | ||
switch (cmd) { | ||
case 'js': | ||
await compileJs(rollupManager, api) | ||
break | ||
case 'ts': | ||
await compileTs(rollupManager, api) | ||
break | ||
case 'react': | ||
await compileReact(rollupManager, api) | ||
break | ||
case 'vue': | ||
await compileVue(rollupManager, api) | ||
break | ||
} | ||
} |
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,34 @@ | ||
import type { IPluginAPI } from '@xus/cli' | ||
import type { IRollupManager } from '../rollupManager' | ||
import { addExternals, addPlugin } from '../rollupManager' | ||
// rollup plugins | ||
import { createBabelPlugin, createCommonjsPlugin } from './rollupPlugin' | ||
|
||
export async function compileJs( | ||
rollupManager: IRollupManager, | ||
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 base overrideFn | ||
rollupManager.registerOverrideFn( | ||
addExternals(({ isESM, isCJS }) => { | ||
if (isESM || isCJS) return [/@babel\/runtime/] | ||
return [] | ||
}), | ||
// handle of compile plugin | ||
addPlugin((mode) => { | ||
const plugins = [] | ||
// cjs plugins | ||
plugins.push(...createCommonjsPlugin(mode)) | ||
// babel plugin | ||
plugins.push(...createBabelPlugin(babelConfig, mode, false, false)) | ||
return plugins | ||
}) | ||
) | ||
// 2. run build | ||
return rollupManager.build() | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/cli-plugin-bundler-rollup/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,9 @@ | ||
import { IPluginAPI } from '@xus/cli' | ||
import type { IRollupManager } from '../rollupManager' | ||
|
||
export async function compileReact( | ||
rollupManager: IRollupManager, | ||
api: IPluginAPI | ||
): Promise<void> { | ||
console.log(`compile React `, rollupManager) | ||
} |
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,9 @@ | ||
import { IPluginAPI } from '@xus/cli' | ||
import type { IRollupManager } from '../rollupManager' | ||
|
||
export async function compileTs( | ||
rollupManager: IRollupManager, | ||
api: IPluginAPI | ||
): Promise<void> { | ||
console.log(`compile ts `, rollupManager) | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/cli-plugin-bundler-rollup/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,9 @@ | ||
import { IPluginAPI } from '@xus/cli' | ||
import type { IRollupManager } from '../rollupManager' | ||
|
||
export async function compileVue( | ||
rollupManager: IRollupManager, | ||
api: IPluginAPI | ||
): Promise<void> { | ||
console.log(`compile vue `, rollupManager) | ||
} |
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' |
18 changes: 18 additions & 0 deletions
18
packages/cli-plugin-bundler-rollup/src/compiler/rollupPlugin/createAliasAndReplacePlugin.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,18 @@ | ||
import type { RollupConfig } from '../../types' | ||
import replacePlugin from 'rollup-plugin-replace' | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
import aliasPlugin from 'rollup-plugin-alias' | ||
|
||
export const createAliasAndReplacePlugin = (config: RollupConfig) => { | ||
const plugins = [] | ||
const alias = config?.alias | ||
const replace = config?.replace | ||
if (alias) { | ||
plugins.push(aliasPlugin(alias)) | ||
} | ||
if (replace) { | ||
plugins.push(replacePlugin(replace)) | ||
} | ||
return plugins | ||
} |
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,67 @@ | ||
import type { Plugin } from 'rollup' | ||
import type { CompileMode } from '../../types' | ||
import { | ||
getBabelOutputPlugin, | ||
RollupBabelOutputPluginOptions | ||
} from '@rollup/plugin-babel' | ||
import vue from 'rollup-plugin-vue' | ||
|
||
export const createBabelPlugin = ( | ||
babelConfig: RollupBabelOutputPluginOptions, | ||
{ isBrowsers, isCJS, isESM, isUMD }: CompileMode, | ||
isReact: boolean, | ||
isVue: boolean | ||
) => { | ||
if (babelConfig) return [getBabelOutputPlugin(babelConfig)] | ||
const preset = | ||
(isReact && '@xus/babel-preset/lib/react') || | ||
(isVue && '@xus/babel-preset/lib/vue') || | ||
'@xus/babel-preset' | ||
return [ | ||
isVue && | ||
vue({ | ||
target: isCJS ? 'node' : 'browser', | ||
exposeFilename: false | ||
}), | ||
(isBrowsers || isUMD) && | ||
getBabelOutputPlugin({ | ||
presets: [ | ||
[ | ||
preset, | ||
{ | ||
useTransformRuntime: false, | ||
useDynamicImport: true, | ||
vueJSX: isVue | ||
} | ||
] | ||
] | ||
}), | ||
isESM && | ||
getBabelOutputPlugin({ | ||
presets: [ | ||
[ | ||
preset, | ||
{ | ||
useESModules: true, | ||
absoluteRuntime: false, | ||
vueJSX: isVue | ||
} | ||
] | ||
] | ||
}), | ||
isCJS && | ||
getBabelOutputPlugin({ | ||
presets: [ | ||
[ | ||
preset, | ||
{ | ||
targets: { node: 'current' }, | ||
absoluteRuntime: false, | ||
useDynamicImport: true, | ||
vueJSX: isVue | ||
} | ||
] | ||
] | ||
}) | ||
].filter(Boolean) as Plugin[] | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/cli-plugin-bundler-rollup/src/compiler/rollupPlugin/createCommonjsPlugin.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,19 @@ | ||
import type { CompileMode } from '../../types' | ||
import { nodeResolve } from '@rollup/plugin-node-resolve' | ||
import commonjs from '@rollup/plugin-commonjs' | ||
// import nodeBuiltins from 'rollup-plugin-node-builtins' | ||
// import nodeGlobals from 'rollup-plugin-node-globals' | ||
|
||
export const createCommonjsPlugin = ({ isCJS }: CompileMode) => { | ||
if (!isCJS) { | ||
return [ | ||
nodeResolve({ | ||
preferBuiltins: true | ||
}), | ||
commonjs({ sourceMap: false }) | ||
// nodeGlobals(), | ||
// nodeBuiltins() | ||
] | ||
} | ||
return [] | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/cli-plugin-bundler-rollup/src/compiler/rollupPlugin/index.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,3 @@ | ||
export * from './createBabelPlugin' | ||
export * from './createCommonjsPlugin' | ||
export * from './createAliasAndReplacePlugin' |
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,45 @@ | ||
import type { IPluginAPI } from '@xus/cli' | ||
import type { FinalConfig, FinalArgs } from './types' | ||
import chalk from 'chalk' | ||
import { error } from '@xus/cli' | ||
import { compileCmds } from './compiler' | ||
import { rollupSchema } from './options' | ||
import { build } from './commands' | ||
import RollupManager from './rollupManager' | ||
|
||
export default function (api: IPluginAPI, projectConfig: FinalConfig): void { | ||
// config validator | ||
api.registerConfigValidator('rollup', rollupSchema) | ||
api.registerCommand( | ||
'rollup', | ||
{ | ||
usage: 'xus rollup <command> [options]', | ||
desc: 'a js/ts/react/vue bundler based on rollup', | ||
options: { | ||
'--input': 'point rollup build entry', | ||
'--formats': 'point build format' | ||
} | ||
}, | ||
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) | ||
} | ||
// 1. init rollupManager | ||
const rollupManager = new RollupManager(api) | ||
// 2. merge args config | ||
rollupManager.setup(projectConfig?.pluginOps?.rollup || {}, args) | ||
// 3. build | ||
return build(buildCmd, rollupManager, api).catch((err) => { | ||
error(`\nbuild failed` + `\n${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,22 @@ | ||
import type { RollupConfig } from './types' | ||
import { createSchema, validate, ValidateCb } from '@xus/cli' | ||
|
||
export const rollupSchema = createSchema<RollupConfig>((joi) => | ||
joi.object({ | ||
overrides: joi.array(), | ||
input: joi.alternatives(joi.string(), joi.object()), | ||
formats: joi.array().valid('esm', 'cjs', 'umd', 'browsers'), | ||
alias: joi.object(), | ||
replace: joi.object() | ||
}) | ||
) | ||
|
||
export function rollupConfigValidator(obj: RollupConfig, cb: ValidateCb): void { | ||
return validate(obj, rollupSchema, cb) | ||
} | ||
|
||
export function defaultRollupConfig(): RollupConfig { | ||
return { | ||
formats: ['esm', 'cjs'] | ||
} | ||
} |
Oops, something went wrong.