Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): Support nuxt layers in a better way #2699

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
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 packages/nuxt/__tests__/nuxt-layer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @vitest-env node
*/
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { describe, it, expect } from 'vitest'
import { setup, $fetch } from '@nuxt/test-utils'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

await setup({
server: true,
rootDir: path.join(__dirname, '../layer/playground'),
nuxtConfig: {
hooks: {
'vite:extendConfig'(config, { isClient }) {
config.define!.__BROWSER__ = isClient
},
},
vite: {
define: {
__DEV__: false,
__TEST__: true,
__FEATURE_PROD_DEVTOOLS__: false,
__USE_DEVTOOLS__: false,
},
},
},
})

describe('works with nuxt layers', async () => {
it('loads stores when extending', async () => {
const html = await $fetch('/')
expect(html).toContain('Base store: 3')
expect(html).toContain('App store: xyz')
})
})
22 changes: 22 additions & 0 deletions packages/nuxt/layer/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { defineNuxtConfig } from 'nuxt/config'
import piniaModule from '../src/module'

const currentDir = dirname(fileURLToPath(import.meta.url))

export default defineNuxtConfig({
modules: [piniaModule],

pinia: {
storesDirs: [join(currentDir, 'stores')],
},

vite: {
define: {
__DEV__: JSON.stringify(process.env.NODE_ENV !== 'production'),
__USE_DEVTOOLS__: true,
__TEST__: false,
},
},
})
10 changes: 10 additions & 0 deletions packages/nuxt/layer/playground/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script lang="ts" setup>
const baseStore = useExampleStore();
const appStore = useXYZStore();
</script>
<template>
<div>
<p>Base store: {{ baseStore.example }}</p>
<p>App store: {{ appStore.foo }}</p>
</div>
</template>
15 changes: 15 additions & 0 deletions packages/nuxt/layer/playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineNuxtConfig } from 'nuxt/config'

export default defineNuxtConfig({
extends: ['..'],
pinia: {
storesDirs: ['xyz'],
},
vite: {
define: {
__DEV__: JSON.stringify(process.env.NODE_ENV !== 'production'),
__USE_DEVTOOLS__: true,
__TEST__: false,
},
},
})
6 changes: 6 additions & 0 deletions packages/nuxt/layer/playground/xyz/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const useXYZStore = defineStore('xyz', () => {
const foo = ref<string>('xyz')
return {
foo,
}
})
6 changes: 6 additions & 0 deletions packages/nuxt/layer/stores/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const useExampleStore = defineStore('example', () => {
const example = ref<number>(3)
return {
example,
}
})
1 change: 1 addition & 0 deletions packages/nuxt/playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineNuxtConfig({
},

modules: [piniaModule],
// extends: ['../layer'],

telemetry: {
enabled: false,
Expand Down
10 changes: 4 additions & 6 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isNuxtMajorVersion,
addImports,
createResolver,
resolvePath,
addImportsDir,
} from '@nuxt/kit'
import type { NuxtModule } from '@nuxt/schema'
Expand Down Expand Up @@ -43,8 +44,7 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
defaults: {
disableVuex: true,
},
setup(options, nuxt) {
// configure transpilation
async setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))

Expand Down Expand Up @@ -98,10 +98,8 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
options.storesDirs = [resolve(nuxt.options.srcDir, 'stores')]
}

if (options.storesDirs) {
for (const storeDir of options.storesDirs) {
addImportsDir(resolve(nuxt.options.rootDir, storeDir))
}
for (const storeDir of options.storesDirs) {
addImportsDir(resolve(nuxt.options.rootDir, storeDir))
}
},
})
Expand Down
2 changes: 1 addition & 1 deletion packages/online-playground/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@pinia/playground",
"name": "@pinia/online-playground",
"version": "0.0.0",
"type": "module",
"private": true,
Expand Down
Loading