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

feat: support reactivity transform #3737

Merged
merged 8 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/with-reactivity-transform/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script setup lang="ts">
import Label from './label.vue'

let count = $ref(0)

function inc () {
count++
}
function dec () {
count--
}
</script>

<template>
<NuxtExampleLayout :show-tips="true" example="with-reactivity-transform">
<div>
<Label :count="count" />
<div class="flex gap-1 justify-center">
<NButton @click="inc()">
Inc
</NButton>
<NButton @click="dec()">
Dec
</NButton>
</div>
</div>

<template #tips>
<div class="flex-auto">
Read the documentation about
<NLink href="https://vuejs.org/guide/extras/reactivity-transform.html" target="_blank">
Reactivity Transform.
</NLink>
</div>
</template>
</NuxtExampleLayout>
</template>
13 changes: 13 additions & 0 deletions examples/with-reactivity-transform/label.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script setup lang="ts">
const { count } = defineProps<{
count: number,
}>()
const doubled = $computed(() => count * 2)
</script>

<template>
<div class="pb2">
Count <b>{{ count }}</b><br>
Doubled <b>{{ doubled }}</b>
</div>
</template>
11 changes: 11 additions & 0 deletions examples/with-reactivity-transform/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineNuxtConfig } from 'nuxt3'

export default defineNuxtConfig({
modules: [
'@nuxt/ui'
],
experimental: {
reactivityTransform: true
}
// builder: 'webpack'
})
13 changes: 13 additions & 0 deletions examples/with-reactivity-transform/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "example-with-reactivity-transform",
"private": true,
"scripts": {
"build": "nuxi build",
"dev": "nuxi dev",
"start": "nuxi preview"
},
"devDependencies": {
"@nuxt/ui": "npm:@nuxt/ui-edge@latest",
"nuxt3": "latest"
}
}
3 changes: 3 additions & 0 deletions examples/with-reactivity-transform/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.nuxt/tsconfig.json"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"play": "yarn run nuxi dev playground",
"release": "yarn && yarn lint && FORCE_COLOR=1 lerna publish -m \"chore: release\" && yarn stub",
"stub": "lerna run prepack -- --stub",
"test:fixtures": "JITI_ESM_RESOLVE=1 vitest --dir test",
"test:fixtures": "yarn nuxi prepare test/fixtures/basic && JITI_ESM_RESOLVE=1 vitest --dir test",
"test:fixtures:webpack": "TEST_WITH_WEBPACK=1 yarn test:fixtures",
"test:types": "yarn run nuxi prepare test/fixtures/basic && cd test/fixtures/basic && npx vue-tsc --noEmit",
"test:unit": "JITI_ESM_RESOLVE=1 yarn vitest --dir packages",
Expand Down
4 changes: 4 additions & 0 deletions packages/nuxi/src/utils/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export const writeTypes = async (nuxt: Nuxt) => {
.filter(f => typeof f === 'string')
.map(id => ({ types: getNearestPackage(id, modulePaths)?.name || id }))

if (nuxt.options.experimental.reactivityTransform) {
references.push({ types: 'vue/macros-global' })
}

const declarations: string[] = []

await nuxt.callHook('prepare:types', { references, declarations, tsConfig })
Expand Down
9 changes: 8 additions & 1 deletion packages/schema/src/config/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@ export default {
* Use vite-node for on-demand server chunk loading
* @version 3
*/
viteNode: process.env.EXPERIMENTAL_VITE_NODE ? true : false
viteNode: process.env.EXPERIMENTAL_VITE_NODE ? true : false,

/**
* Enable Vue's reactivity transform
* @see https://vuejs.org/guide/extras/reactivity-transform.html
* @version 3
*/
reactivityTransform: false
}
3 changes: 3 additions & 0 deletions packages/vite/src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export async function bundle (nuxt: Nuxt) {
plugins: [
virtual(nuxt.vfs)
],
vue: {
reactivityTransform: nuxt.options.experimental.reactivityTransform
},
server: {
watch: {
ignored: isIgnored
Expand Down
5 changes: 4 additions & 1 deletion packages/webpack/src/presets/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export function vue (ctx: WebpackConfigContext) {
config.module.rules.push({
test: /\.vue$/i,
loader: 'vue-loader',
options: options.webpack.loaders.vue
options: {
reactivityTransform: ctx.nuxt.options.experimental.reactivityTransform,
...options.webpack.loaders.vue
}
})

if (ctx.isClient) {
Expand Down
8 changes: 8 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,12 @@ describe('fixtures:basic', async () => {
expect(html).toContain('Custom Layout:')
})
})

describe('reactivity transform', () => {
it('should works', async () => {
const html = await $fetch('/')

expect(html).toContain('Sugar Counter 12 x 2 = 24')
})
})
})
14 changes: 14 additions & 0 deletions test/fixtures/basic/components/SugarCounter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script setup lang="ts">
const { count } = defineProps<{
count: number,
}>()
// eslint-disable-next-line prefer-const
let multiplier = $ref(2)
const doubled = $computed(() => count * multiplier)
</script>

<template>
<div>
Sugar Counter {{ count }} x {{ multiplier }} = {{ doubled }}
</div>
</template>
3 changes: 3 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ export default defineNuxtConfig({
filePath: '~/other-components-folder/named-export'
})
}
},
experimental: {
reactivityTransform: true
}
})
1 change: 1 addition & 0 deletions test/fixtures/basic/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<div>Composable | foo: {{ foo }}</div>
<div>Composable | bar: {{ bar }}</div>
<div>Plugin | myPlugin: {{ $myPlugin() }}</div>
<SugarCounter :count="12" />
<CustomComponent />
</div>
</template>
Expand Down
3 changes: 3 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ export default defineConfig({
alias: {
'#app': resolve('./packages/nuxt3/src/app/index.ts'),
'@nuxt/test-utils': resolve('./packages/test-utils/src/index.ts')
},
esbuild: {
tsconfigRaw: '{}'
}
})
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10580,6 +10580,15 @@ __metadata:
languageName: unknown
linkType: soft

"example-with-reactivity-transform@workspace:examples/with-reactivity-transform":
version: 0.0.0-use.local
resolution: "example-with-reactivity-transform@workspace:examples/with-reactivity-transform"
dependencies:
"@nuxt/ui": "npm:@nuxt/ui-edge@latest"
nuxt3: latest
languageName: unknown
linkType: soft

"example-with-test@workspace:examples/with-test":
version: 0.0.0-use.local
resolution: "example-with-test@workspace:examples/with-test"
Expand Down