Skip to content

Commit

Permalink
feat: rollup bundler to bundle js
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Feb 2, 2021
1 parent adad67a commit be3f403
Show file tree
Hide file tree
Showing 29 changed files with 2,847 additions and 49 deletions.
25 changes: 13 additions & 12 deletions .eslintrc.js
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
}
}
23 changes: 23 additions & 0 deletions packages/cli-plugin-bundler-rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"name": "@xus/cli-plugin-bundler-rollup",
"version": "0.0.1",
"description": "xus cli plugin rollup bundler",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"keywords": [
"cli",
"rollup"
Expand All @@ -22,5 +24,26 @@
},
"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",
"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"
}
}
24 changes: 24 additions & 0 deletions packages/cli-plugin-bundler-rollup/src/commands.ts
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
}
}
34 changes: 34 additions & 0 deletions packages/cli-plugin-bundler-rollup/src/compiler/compileJs.ts
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()
}
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)
}
9 changes: 9 additions & 0 deletions packages/cli-plugin-bundler-rollup/src/compiler/compileTs.ts
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 packages/cli-plugin-bundler-rollup/src/compiler/compileVue.ts
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)
}
5 changes: 5 additions & 0 deletions packages/cli-plugin-bundler-rollup/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'
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
}
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[]
}
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 []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './createBabelPlugin'
export * from './createCommonjsPlugin'
export * from './createAliasAndReplacePlugin'
45 changes: 45 additions & 0 deletions packages/cli-plugin-bundler-rollup/src/index.ts
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}`)
})
}
)
}
22 changes: 22 additions & 0 deletions packages/cli-plugin-bundler-rollup/src/options.ts
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']
}
}
Loading

0 comments on commit be3f403

Please sign in to comment.