Skip to content

Commit

Permalink
feat: vuejsx打包插件
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Mar 15, 2021
1 parent 435bcad commit aa9d7b6
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 1,963 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/manager/HookManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type IHookManagerOps = {
service: ICliService
}

const builtinHooks = ['configReady', 'onPluginsReady', 'onRunCmd']
const builtinHooks = ['configReady', 'onSetuped', 'onRunCmd']

const logger = new Logger(`xus:service:HookManager`)
export class HookManager {
Expand Down
11 changes: 11 additions & 0 deletions packages/plugin-lib-vue-jsx/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
```
39 changes: 39 additions & 0 deletions packages/plugin-lib-vue-jsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@xus/plugin-lib-vue-jsx",
"version": "0.1.0",
"description": "a bundle for vue jsx",
"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:vuejsx": "tsc --watch",
"build:vuejsx": "tsc"
},
"bugs": {
"url": "https://github.com/xus-code/bundle-tools/issues"
},
"dependencies": {
"@babel/core": "^7.13.10",
"@babel/plugin-transform-typescript": "^7.13.0",
"@vue/babel-plugin-jsx": "^1.0.3",
"@vue/babel-preset-jsx": "^1.2.4",
"@xus/cli-types": "^0.1.0",
"rollup": "^2.40.0"
},
"devDependencies": {
"@types/babel__core": "^7.1.12"
}
}
37 changes: 37 additions & 0 deletions packages/plugin-lib-vue-jsx/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { IPlugin } from '@xus/cli-types'
import { vuejsx, IVueJsx } from './plugin'

export interface IVueJsxOps {
version?: 2 | 3
jsxOps?: IVueJsx['jsxOps']
}

export default (ops?: IVueJsxOps) => {
const { version = 3, jsxOps = {} } = ops || {}
return {
name: 'xus:rollup:vuejsx',
apply(api) {
api.modifyRollupConfig({
fn(rc) {
rc.plugin('$$esbuild').tap((ops) => {
// turn esbuild only handle .js file
!ops[0] && (ops[0] = {})
ops[0].include = /\.(j|t)s$/
return ops
})
// vue jsx transform
rc.plugin('vuejsx').use(vuejsx, [
{
version,
jsxOps,
sourceMaps: !!api.projectConfig?.libBuild?.sourcemap
}
])
return rc
},
// last to run
stage: 100
})
}
} as IPlugin
}
54 changes: 54 additions & 0 deletions packages/plugin-lib-vue-jsx/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Plugin } from 'rollup'
import * as babel from '@babel/core'
import jsx3, { VueJSXPluginOptions } from '@vue/babel-plugin-jsx'
// @ts-ignore
import jsx2 from '@vue/babel-preset-jsx'

export interface IVueJsx {
version?: 2 | 3
jsxOps?: VueJSXPluginOptions | Record<string, any>
sourceMaps?: boolean
}

const filterRE = /\.[jt]sx$/
const tsRE = /\.tsx$/

export function vuejsx(ops?: IVueJsx): Plugin {
const { version = 3, jsxOps = {}, sourceMaps = false } = ops || {}
return {
name: 'xus:vuejsx',
async transform(code, id) {
if (filterRE.test(id)) {
const presets = []
const plugins = []
if (version === 3) {
plugins.push([jsx3, jsxOps])
} else {
presets.push([jsx2, jsxOps])
}
if (tsRE.test(id)) {
plugins.push([
// @ts-ignore
await import('@babel/plugin-transform-typescript'),
{ isTSX: true, allowExtensions: true }
])
}
// ready to transform
const res = babel.transformSync(code, {
ast: true,
presets,
plugins,
sourceFileName: id,
sourceMaps,
babelrc: false,
configFile: false
})
return {
code: res!.code!,
map: res!.map
}
}
return null
}
}
}
7 changes: 7 additions & 0 deletions packages/plugin-lib-vue-jsx/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"outDir": "./dist"
},
"extends": "../../tsconfig.json",
"include": ["./src/**/*"]
}

0 comments on commit aa9d7b6

Please sign in to comment.