Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
feat: JSX handling
Browse files Browse the repository at this point in the history
close #5
  • Loading branch information
underfin committed Oct 13, 2020
1 parent d723e64 commit cbd619d
Show file tree
Hide file tree
Showing 9 changed files with 686 additions and 11 deletions.
26 changes: 24 additions & 2 deletions README.MD
Expand Up @@ -9,12 +9,34 @@
const { createVuePlugin } = require('vite-plugin-vue2')

module.exports = {
plugins: [createVuePlugin()],
plugins: [createVuePlugin(/*options*/)],
}
```

## [Options](https://github.com/underfin/vite-plugin-vue2/blob/master/src/index.ts#L7)

### `vueTemplateOptions`

Type: `Object`<br>
Default: `null`

The options for `@vue/component-compiler-utils`.

### `rollupPluginVueOptions`

Type: `Object`<br>
Default: `null`

The options for `rollup-plugin-vue`.

### `jsx`

Type: `Boolean`<br>
Default: `false`

The options for jsx transform.

## Todo

- JSX handing
- Custom Block
- SSR Build
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -40,8 +40,13 @@
]
},
"dependencies": {
"@babel/core": "^7.11.6",
"@vue/compiler-sfc": "^3.0.0-rc.5",
"@vue/component-compiler-utils": "^3.2.0",
"babel-helper-vue-jsx-merge-props": "^2.0.3",
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-plugin-transform-vue-jsx": "^3.7.0",
"babel-preset-env": "^1.7.0",
"chalk": "^4.1.0",
"debug": "^4.1.1",
"esbuild": "^0.5.19",
Expand Down
5 changes: 4 additions & 1 deletion playground/App.vue
Expand Up @@ -6,6 +6,7 @@
<TestCssModules/>
<TestHmr/>
<TestAssets/>
<TestJsx/>
</div>
</template>

Expand All @@ -15,6 +16,7 @@ import TestScopedCss from './css/TestScopedCss.vue'
import TestCssModules from './css/TestCssModules.vue'
import TestHmr from './hmr/TestHmr.vue'
import TestAssets from './test-assets/TestAssets.vue'
import TestJsx from './TestJsx.tsx'
export default {
name: 'App',
Expand All @@ -23,7 +25,8 @@ export default {
TestBlockSrcImport,
TestCssModules,
TestHmr,
TestAssets
TestAssets,
TestJsx
}
}
</script>
5 changes: 5 additions & 0 deletions playground/TestJsx.tsx
@@ -0,0 +1,5 @@
export default {
render() {
return <div class={'jsx'}> JSX works! </div>
}
}
2 changes: 1 addition & 1 deletion playground/vite.config.js
@@ -1,7 +1,7 @@
const { createVuePlugin } = require('../dist')

const config = {
plugins: [createVuePlugin()],
plugins: [createVuePlugin({ jsx: true })],
}

export default config
11 changes: 10 additions & 1 deletion src/index.ts
Expand Up @@ -2,6 +2,7 @@ import { resolver } from './resolver'
import { vuePlugin, setVueCompilerOptions } from './serverPlugin'
import { TemplateCompileOptions } from '@vue/component-compiler-utils/lib/compileTemplate'
import { VuePluginOptions } from 'rollup-plugin-vue'
import { jsxTransform } from './jsxTransform'

export interface VueViteOptions {
/**
Expand All @@ -12,16 +13,24 @@ export interface VueViteOptions {
* The options for `rollup-plugin-vue`.
*/
rollupPluginVueOptions?: VuePluginOptions
/**
* The options for jsx transform
* @default false
*/
jsx?: boolean
}

export function createVuePlugin(options: VueViteOptions = {}) {
const { vueTemplateOptions, rollupPluginVueOptions } = options
const { vueTemplateOptions, rollupPluginVueOptions, jsx } = options
if (vueTemplateOptions) {
setVueCompilerOptions(vueTemplateOptions)
}

return {
resolvers: [resolver],
transforms: [jsxTransform],
// if set truly `jsx` option, should disabled esbuild
enableEsbuild: !jsx,
configureServer: vuePlugin,
enableRollupPluginVue: false,
rollupInputOptions: {
Expand Down
20 changes: 20 additions & 0 deletions src/jsxTransform.ts
@@ -0,0 +1,20 @@
import { Transform } from 'vite/dist/node/transform'
import { transform } from '@babel/core'

export const jsxTransform: Transform = {
test({ path }) {
return /\.(tsx?|jsx?)$/.test(path)
},
transform({ id, code }) {
const result = transform(code, {
plugins: ['transform-vue-jsx'],
filename: id,
sourceMaps: true,
})!

return {
code: result.code as string,
map: result.map as any,
}
},
}
4 changes: 4 additions & 0 deletions test/util.ts
Expand Up @@ -198,4 +198,8 @@ export function declareTests(isBuild: boolean) {
// )
}
})

test('Jsx', async () => {
expect(await getText('.jsx')).toMatch('JSX works!')
})
}

0 comments on commit cbd619d

Please sign in to comment.