Skip to content

Commit

Permalink
fix(plugin-vue-jsx): support ssr
Browse files Browse the repository at this point in the history
close #1939
  • Loading branch information
yyx990803 committed Feb 8, 2021
1 parent 9066f27 commit 30e92a1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/playground/ssr-vue/__tests__/ssr-vue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ test('asset', async () => {
)
})

test('jsx', async () => {
expect(await page.textContent('.jsx')).toMatch('from JSX')
})

test('hydration', async () => {
expect(await page.textContent('button')).toMatch('0')
await page.click('button')
Expand Down
9 changes: 9 additions & 0 deletions packages/playground/ssr-vue/src/components/Foo.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineComponent } from 'vue'

// named exports w/ variable declaration: ok
export const Foo = defineComponent({
name: 'foo',
setup() {
return () => <div class="jsx">from JSX</div>
}
})
4 changes: 3 additions & 1 deletion packages/playground/ssr-vue/src/pages/Home.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<template>
<h1>Home</h1>
<p>
<img src="../assets/logo.png" alt="logo">
<img src="../assets/logo.png" alt="logo" />
</p>
<button @click="state.count++">count is: {{ state.count }}</button>
<Foo/>
</template>

<script setup>
import { reactive } from 'vue'
import { Foo } from '../components/Foo'
const state = reactive({ count: 0 })
</script>
Expand Down
3 changes: 2 additions & 1 deletion packages/playground/ssr-vue/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const vuePlugin = require('@vitejs/plugin-vue')
const vueJsx = require('@vitejs/plugin-vue-jsx')

/**
* @type {import('vite').UserConfig}
*/
module.exports = {
plugins: [vuePlugin()],
plugins: [vuePlugin(), vueJsx()],
build: {
minify: false
}
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-vue-jsx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const importMeta = require('@babel/plugin-syntax-import-meta')
const hash = require('hash-sum')

/**
* @param {import('.').Options} options
* @param {import('@vue/babel-plugin-jsx').VueJSXPluginOptions} options
* @returns {import('vite').Plugin}
*/
function vueJsxPlugin(options = {}) {
Expand Down Expand Up @@ -35,7 +35,7 @@ function vueJsxPlugin(options = {}) {
needSourceMap = config.command === 'serve' || !!config.build.sourcemap
},

transform(code, id) {
transform(code, id, ssr) {
if (/\.[jt]sx$/.test(id)) {
const plugins = [importMeta, [jsx, options]]
if (id.endsWith('.tsx')) {
Expand All @@ -53,7 +53,7 @@ function vueJsxPlugin(options = {}) {
sourceFileName: id
})

if (!needHmr) {
if (ssr || !needHmr) {
return {
code: result.code,
map: result.map
Expand Down

0 comments on commit 30e92a1

Please sign in to comment.