Skip to content

Commit

Permalink
chore(nuxt): Resolve pinia when added via layer or module
Browse files Browse the repository at this point in the history
  • Loading branch information
David Marr committed Oct 23, 2024
1 parent 6fb480e commit db6e0c5
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 14 deletions.
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'],

pinia: {
storesDirs: ['./stores/**', './domain/*/stores'],
Expand Down
20 changes: 7 additions & 13 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
isNuxt2,
addImports,
createResolver,
resolveModule,
resolvePath,
addImportsDir,
} from '@nuxt/kit'
import type { NuxtModule } from '@nuxt/schema'
Expand Down Expand Up @@ -44,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 All @@ -64,12 +63,9 @@ const module: NuxtModule<ModuleOptions> = defineNuxtModule<ModuleOptions>({
nuxt.options.build.transpile.push(resolve(runtimeDir))

// Make sure we use the mjs build for pinia
nuxt.options.alias.pinia =
nuxt.options.alias.pinia ||
// FIXME: remove this deprecated call. Ensure it works in Nuxt 2 to 3
resolveModule('pinia/dist/pinia.mjs', {
paths: [nuxt.options.rootDir, import.meta.url],
})
if (!nuxt.options.alias.pinia) {
nuxt.options.alias.pinia = await resolvePath('pinia/dist/pinia.mjs')
}

nuxt.hook('prepare:types', ({ references }) => {
references.push({ types: '@pinia/nuxt' })
Expand Down Expand Up @@ -100,10 +96,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

0 comments on commit db6e0c5

Please sign in to comment.