Skip to content

Commit

Permalink
feat: legacy打包降级插件
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Mar 15, 2021
1 parent aa9d7b6 commit 71ea0ab
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/cli-types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface IPluginAPI extends IPluginAPIBase {
// service api
BabelRegister: IBabelRegister
// service lifycycle
onSetuped: (config: IConfig) => void
onSetuped: IFastHookRegister<(config: IConfig) => void>
onRunCmd: noopFn
getCmdArgs: () => { args: IArgs; rawArgs: IRawArgs }
modifyProjectConfig: (config: IConfig) => void
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-cmd-roll-lib/src/bundler/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function doBuild(ops: IRollupBuildOps) {
watcher.close()
})
} else {
logger.debug(`do rollup: `)
logger.debug(`start rollup`)
const bundler = await rollup({
...inputConfig,
watch: false
Expand All @@ -58,7 +58,7 @@ async function doBuild(ops: IRollupBuildOps) {
path.join(pkgRoot, outputConfigs[0].dir!),
alwaysEmptyDistDir
)
logger.debug(`do ${isWrite ? 'write' : 'generate'}`)
logger.debug(`start ${isWrite ? 'write' : 'generate'}`)
for (const output of outputConfigs) {
logger.debug(output)
logger.info(
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-cmd-roll-lib/src/modify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function modifyConfig(
// esbuild
rc.plugin('$$esbuild').use(esbuildPlugin, [
{
include: /\.(jsx?|tsx?)$/,
target,
sourcemap
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-cmd-roll-lib/src/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface IEsbuildOps extends TransformOptions {

export function esbuildPlugin(ops: IEsbuildOps = {}): Plugin {
const filter = createFilter(
ops?.include || /\.(jsx?|tsx?)$/,
ops?.include || undefined,
ops?.exclude || undefined
)
delete ops?.include
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin-lib-legacy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `vue`

> TODO: description
## Usage

```
const cli = require('cli');
// TODO: DEMONSTRATE API
```
40 changes: 40 additions & 0 deletions packages/plugin-lib-legacy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@xus/plugin-lib-legacy",
"version": "0.1.0",
"description": "a bundle for legacy",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"keywords": [
"cli",
"bundle"
],
"author": "guo.xu <xuguo@outlook.it>",
"homepage": "https://github.com/xus-code/bundle-tools/tree/master/packages/cli#readme",
"license": "MIT",
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/xus-code/bundle-tools.git"
},
"scripts": {
"dev:legacy": "tsc --watch",
"build:legacy": "tsc"
},
"bugs": {
"url": "https://github.com/xus-code/bundle-tools/issues"
},
"dependencies": {
"@babel/core": "^7.13.10",
"@xus/babel-preset": "^0.1.11",
"@xus/cli-shared": "^0.1.4",
"@xus/cli-types": "^0.1.0",
"@xus/core": "^0.1.5",
"core-js": "^3.9.1",
"rollup": "^2.40.0"
},
"devDependencies": {
"@types/babel__core": "^7.1.12"
}
}
50 changes: 50 additions & 0 deletions packages/plugin-lib-legacy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { IPlugin } from '@xus/cli-types'
import { legacyPlugin } from './plugin'

export interface ILegacyOps {
targets?: string | string[]
helper?: 'bundled' | 'runtime'
}

export default (ops?: ILegacyOps) => {
const { helper = 'runtime', targets = 'defaults' } = ops || {}
return {
name: 'xus:lib:legacy',
apply(api) {
// ensure minify is not esbuild
api.onSetuped((config) => {
const minify = config?.libBuild?.minify
if (minify === 'esbuild') {
throw new Error(
`when use @xus/plugin-lib-legacy, minify should be use 'terser'!!! now is 'esbuild'.`
)
}
})

api.modifyRollupConfig({
fn(rc) {
rc.plugin('$$esbuild').tap((ops) => {
// turn esbuild only handle .js/.ts/.tsx file
!ops[0] && (ops[0] = {})
ops[0].exclude = /\.js$/
ops[0].target = 'es2019'
return ops
})
// babel
rc.plugin('$$legacy')
.use(legacyPlugin, [
{
helper,
targets,
sourceMaps: !!api.projectConfig?.libBuild?.sourcemap
}
])
.before('$$minify')

return rc
},
stage: 200
})
}
} as IPlugin
}
55 changes: 55 additions & 0 deletions packages/plugin-lib-legacy/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Logger } from '@xus/cli-shared'
import { Plugin } from 'rollup'
import * as babel from '@babel/core'
// @ts-ignore
import preset from '@xus/babel-preset'

interface ILegacyPluginOps {
targets?: string | string[]
helper?: 'bundled' | 'runtime'
sourceMaps?: boolean
}

const logger = new Logger(`xus:rollup:legacy`)

export function legacyPlugin(ops?: ILegacyPluginOps): Plugin {
const { helper = 'runtime', sourceMaps = false, targets = 'defaults' } =
ops || {}
return {
name: 'xus:rollup:legacy',
async renderChunk(code, chunk, ops) {
logger.debug(`transform ${chunk.facadeModuleId}`)
const presets = [
[
preset,
{
targets,
modules: false,
useESModules: true,
usageMode:
helper === 'bundled' || ['iife', 'umd'].includes(ops.format),
absoluteRuntime: false
}
]
]
const res = babel.transform(code, {
ast: true,
presets,
sourceMaps,
inputSourceMap: chunk.map,
babelrc: false,
configFile: false,
filename: chunk.fileName,
minified: false,
compact: false
})
logger.debug(`transform code: `)
logger.debug(res)

return {
code: res!.code!,
map: res!.map
}
}
}
}
7 changes: 7 additions & 0 deletions packages/plugin-lib-legacy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"outDir": "./dist"
},
"extends": "../../tsconfig.json",
"include": ["./src/**/*"]
}
2 changes: 1 addition & 1 deletion packages/plugin-lib-vue-jsx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface IVueJsxOps {
export default (ops?: IVueJsxOps) => {
const { version = 3, jsxOps = {} } = ops || {}
return {
name: 'xus:rollup:vuejsx',
name: 'xus:lib:vuejsx',
apply(api) {
api.modifyRollupConfig({
fn(rc) {
Expand Down
26 changes: 21 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@

"@babel/core@^7.13.10":
version "7.13.10"
resolved "https://registry.npm.taobao.org/@babel/core/download/@babel/core-7.13.10.tgz?cache=0&sync_timestamp=1615243326209&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40babel%2Fcore%2Fdownload%2F%40babel%2Fcore-7.13.10.tgz#07de050bbd8193fcd8a3c27918c0890613a94559"
integrity sha1-B94FC72Bk/zYo8J5GMCJBhOpRVk=
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.13.10.tgz#07de050bbd8193fcd8a3c27918c0890613a94559"
integrity sha512-bfIYcT0BdKeAZrovpMqX2Mx5NrgAckGbwT982AkdS5GNfn3KMGiprlBAtmBcFZRUmpaufS6WZFP8trvx8ptFDw==
dependencies:
"@babel/code-frame" "^7.12.13"
"@babel/generator" "^7.13.9"
Expand Down Expand Up @@ -2601,7 +2601,7 @@
dependencies:
defer-to-connect "^2.0.0"

"@types/babel__core@^7.0.0", "@types/babel__core@^7.1.12", "@types/babel__core@^7.1.7":
"@types/babel__core@^7.0.0", "@types/babel__core@^7.1.7":
version "7.1.12"
resolved "https://registry.npm.taobao.org/@types/babel__core/download/@types/babel__core-7.1.12.tgz?cache=0&sync_timestamp=1613378020017&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Fbabel__core%2Fdownload%2F%40types%2Fbabel__core-7.1.12.tgz#4d8e9e51eb265552a7e4f1ff2219ab6133bdfb2d"
integrity sha1-TY6eUesmVVKn5PH/IhmrYTO9+y0=
Expand All @@ -2612,6 +2612,17 @@
"@types/babel__template" "*"
"@types/babel__traverse" "*"

"@types/babel__core@^7.1.12":
version "7.1.12"
resolved "https://registry.yarnpkg.com/@types/babel__core/-/babel__core-7.1.12.tgz#4d8e9e51eb265552a7e4f1ff2219ab6133bdfb2d"
integrity sha512-wMTHiiTiBAAPebqaPiPDLFA4LYPKr6Ph0Xq/6rq1Ur3v66HXyG+clfR9CNETkD7MQS8ZHvpQOtA53DLws5WAEQ==
dependencies:
"@babel/parser" "^7.1.0"
"@babel/types" "^7.0.0"
"@types/babel__generator" "*"
"@types/babel__template" "*"
"@types/babel__traverse" "*"

"@types/babel__generator@*":
version "7.6.2"
resolved "https://registry.yarnpkg.com/@types/babel__generator/-/babel__generator-7.6.2.tgz#f3d71178e187858f7c45e30380f8f1b7415a12d8"
Expand Down Expand Up @@ -3084,8 +3095,8 @@

"@xus/babel-preset@^0.1.11":
version "0.1.11"
resolved "https://registry.yarnpkg.com/@xus/babel-preset/-/babel-preset-0.1.11.tgz#35acf74b8ed5f4ca2386dc8a439f8e6d28cac836"
integrity sha512-/M1K663BfkgYiMvSPlTq+gte0iWTBkZiQRyeSNSYBfUbfA/0Fmr+yUsVMFXmRhDWf9h0e7zfMYlVQ5gx3q5Oqg==
resolved "https://registry.npm.taobao.org/@xus/babel-preset/download/@xus/babel-preset-0.1.11.tgz#35acf74b8ed5f4ca2386dc8a439f8e6d28cac836"
integrity sha1-Naz3S47V9MojhtyKQ5+ObSjKyDY=
dependencies:
"@babel/core" "^7.12.10"
"@babel/plugin-proposal-class-properties" "7.12.1"
Expand Down Expand Up @@ -4550,6 +4561,11 @@ core-js@^3.8.1:
resolved "https://npm.c2cloud.cn/core-js/download/core-js-3.8.3.tgz#c21906e1f14f3689f93abcc6e26883550dd92dd0"
integrity sha1-whkG4fFPNon5OrzG4miDVQ3ZLdA=

core-js@^3.9.1:
version "3.9.1"
resolved "https://registry.npm.taobao.org/core-js/download/core-js-3.9.1.tgz?cache=0&sync_timestamp=1614537310447&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fcore-js%2Fdownload%2Fcore-js-3.9.1.tgz#cec8de593db8eb2a85ffb0dbdeb312cb6e5460ae"
integrity sha1-zsjeWT246yqF/7Db3rMSy25UYK4=

core-util-is@1.0.2, core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
Expand Down

0 comments on commit 71ea0ab

Please sign in to comment.