Skip to content

Commit 2d976cd

Browse files
committed
fix: use transpile options for deps
1 parent 3a4eaeb commit 2d976cd

1 file changed

Lines changed: 26 additions & 55 deletions

File tree

src/module.ts

Lines changed: 26 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import path from 'path'
22
import fs from 'fs'
33
import { spawn, type ChildProcess } from 'child_process'
4-
import { fileURLToPath } from 'url'
54
import { createRequire } from 'module'
65
import { defineNuxtModule, createResolver, resolveFiles, addComponent, addImportsDir, extendPages } from '@nuxt/kit'
7-
import type { NuxtPage, ModuleDependencies, NuxtModule } from '@nuxt/schema'
6+
import type { NuxtPage, NuxtModule } from '@nuxt/schema'
87
import { joinURL, withoutLeadingSlash, withoutTrailingSlash } from 'ufo'
98
import { minimatch } from 'minimatch'
109
import { pascalToKebabCase } from './runtime/utils/string/pascal-to-kebab-case'
@@ -56,41 +55,22 @@ const _module: NuxtModule<NuxtStoriesOptions> = defineNuxtModule<NuxtStoriesOpti
5655
mode: 'all',
5756
framePort: 3000,
5857
},
59-
moduleDependencies(nuxt): ModuleDependencies {
60-
const mode =
61-
(process.env.NUXT_STORIES_MODE as string | undefined) ||
62-
(nuxt.options as unknown as { stories?: { mode?: string } }).stories?.mode ||
63-
'all'
64-
65-
if (mode === 'frame') return {}
66-
67-
// Resolve @primevue/nuxt-module from nuxt-stories' own node_modules so the
68-
// consuming app doesn't need to install it. Using an absolute path as the key
69-
// bypasses pnpm's strict isolation while still going through moduleDependencies.
70-
// import.meta.resolve follows ESM exports and returns the .mjs entry point.
71-
let primevuePath: string
72-
try {
73-
primevuePath = fileURLToPath(import.meta.resolve('@primevue/nuxt-module'))
74-
} catch {
75-
return {}
76-
}
77-
78-
return {
79-
[primevuePath]: {
80-
defaults: {
81-
autoImport: false,
82-
components: {
83-
prefix: 'pv',
84-
include: ['Tree', 'Button', 'InputText', 'Splitter', 'SplitterPanel', 'Toolbar', 'Menu'],
85-
},
86-
options: {
87-
theme: {
88-
preset: Aura,
89-
},
58+
moduleDependencies: {
59+
'@primevue/nuxt-module': {
60+
optional: true,
61+
defaults: {
62+
autoImport: false,
63+
components: {
64+
prefix: 'pv',
65+
include: ['Tree', 'Button', 'InputText', 'Splitter', 'SplitterPanel', 'Toolbar', 'Menu'],
66+
},
67+
options: {
68+
theme: {
69+
preset: Aura,
9070
},
9171
},
9272
},
93-
}
73+
},
9474
},
9575
async setup(options, nuxt) {
9676
if (!options.enabled) return
@@ -104,23 +84,20 @@ const _module: NuxtModule<NuxtStoriesOptions> = defineNuxtModule<NuxtStoriesOpti
10484

10585
// @primevue/nuxt-module generates code that imports from 'primevue', '@primeuix/themes', etc.
10686
// Those packages live in nuxt-stories' own node_modules, not the consumer's.
107-
// • alias → Vite client/SSR: virtual modules (virtual:#primevue-style) resolve
108-
// subpath imports (primevue/button/style) via explicit directory mapping
109-
// • modulesDir → Vite bare-import fallback
110-
// • nitro.nodeModulesDirs → Nitro server bundle + Node.js dev runtime resolution
111-
let storiesNodeModules: string | null = null
87+
// • alias → tells Vite where to find the package (resolves bare + subpath imports)
88+
// • build.transpile → prevents Vite AND Nitro from externalising them; Nuxt propagates
89+
// transpile entries to nitro.externals.inline automatically
11290
if (mode !== 'frame') {
113-
try {
114-
const _require = createRequire(import.meta.url)
115-
const primevueDir = path.dirname(_require.resolve('primevue/package.json'))
116-
storiesNodeModules = path.dirname(primevueDir)
117-
// Alias so Vite resolves primevue/* subpath imports from the virtual module context
118-
nuxt.options.alias['primevue'] = primevueDir
119-
if (!nuxt.options.modulesDir.includes(storiesNodeModules)) {
120-
nuxt.options.modulesDir.push(storiesNodeModules)
91+
const _require = createRequire(import.meta.url)
92+
for (const pkg of ['primevue', '@primeuix/themes', 'primeicons']) {
93+
try {
94+
nuxt.options.alias[pkg] = path.dirname(_require.resolve(`${pkg}/package.json`))
95+
} catch {
96+
// package not resolvable from this context – skip
97+
}
98+
if (!nuxt.options.build.transpile.includes(pkg)) {
99+
nuxt.options.build.transpile.push(pkg)
121100
}
122-
} catch {
123-
// ignore
124101
}
125102
}
126103

@@ -441,12 +418,6 @@ const _module: NuxtModule<NuxtStoriesOptions> = defineNuxtModule<NuxtStoriesOpti
441418

442419
// NITRO CONFIG — serve the module's static public assets (stories.css, etc.)
443420
nuxt.hook('nitro:config', (nitroConfig) => {
444-
// Expose nuxt-stories' node_modules to Nitro so primevue and related
445-
// packages can be resolved in the server bundle and at Node.js dev runtime.
446-
if (storiesNodeModules) {
447-
nitroConfig.nodeModulesDirs = [...(nitroConfig.nodeModulesDirs || []), storiesNodeModules]
448-
}
449-
450421
// Register story paths for static generation (nuxi generate).
451422
// Done here (not inside extendPages) to guarantee the routes are visible
452423
// to Nitro — pages:extend is awaited before nitro:config fires.

0 commit comments

Comments
 (0)