diff --git a/packages/nuxt/__tests__/nuxt-layer.spec.ts b/packages/nuxt/__tests__/nuxt-layer.spec.ts new file mode 100644 index 0000000000..a844c11a60 --- /dev/null +++ b/packages/nuxt/__tests__/nuxt-layer.spec.ts @@ -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') + }) +}) diff --git a/packages/nuxt/layer/nuxt.config.ts b/packages/nuxt/layer/nuxt.config.ts new file mode 100644 index 0000000000..60e8eacd15 --- /dev/null +++ b/packages/nuxt/layer/nuxt.config.ts @@ -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, + }, + }, +}) diff --git a/packages/nuxt/layer/playground/app.vue b/packages/nuxt/layer/playground/app.vue new file mode 100644 index 0000000000..4bdbfbe983 --- /dev/null +++ b/packages/nuxt/layer/playground/app.vue @@ -0,0 +1,10 @@ + + \ No newline at end of file diff --git a/packages/nuxt/layer/playground/nuxt.config.ts b/packages/nuxt/layer/playground/nuxt.config.ts new file mode 100644 index 0000000000..2883b6efb9 --- /dev/null +++ b/packages/nuxt/layer/playground/nuxt.config.ts @@ -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, + }, + }, +}) diff --git a/packages/nuxt/layer/playground/xyz/store.ts b/packages/nuxt/layer/playground/xyz/store.ts new file mode 100644 index 0000000000..27db71a48f --- /dev/null +++ b/packages/nuxt/layer/playground/xyz/store.ts @@ -0,0 +1,6 @@ +export const useXYZStore = defineStore('xyz', () => { + const foo = ref('xyz') + return { + foo, + } +}) diff --git a/packages/nuxt/layer/stores/example.ts b/packages/nuxt/layer/stores/example.ts new file mode 100644 index 0000000000..deaeb69457 --- /dev/null +++ b/packages/nuxt/layer/stores/example.ts @@ -0,0 +1,6 @@ +export const useExampleStore = defineStore('example', () => { + const example = ref(3) + return { + example, + } +}) diff --git a/packages/nuxt/playground/nuxt.config.ts b/packages/nuxt/playground/nuxt.config.ts index b2c0a251be..cbceb45e7b 100644 --- a/packages/nuxt/playground/nuxt.config.ts +++ b/packages/nuxt/playground/nuxt.config.ts @@ -9,6 +9,7 @@ export default defineNuxtConfig({ }, modules: [piniaModule], + // extends: ['../layer'], telemetry: { enabled: false, diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index b425bda4e4..28863922b7 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -7,6 +7,7 @@ import { isNuxtMajorVersion, addImports, createResolver, + resolvePath, addImportsDir, } from '@nuxt/kit' import type { NuxtModule } from '@nuxt/schema' @@ -43,8 +44,7 @@ const module: NuxtModule = defineNuxtModule({ 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)) @@ -98,10 +98,8 @@ const module: NuxtModule = defineNuxtModule({ 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)) } }, }) diff --git a/packages/online-playground/package.json b/packages/online-playground/package.json index fae82a1e23..14baa00578 100644 --- a/packages/online-playground/package.json +++ b/packages/online-playground/package.json @@ -1,5 +1,5 @@ { - "name": "@pinia/playground", + "name": "@pinia/online-playground", "version": "0.0.0", "type": "module", "private": true,