-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
index.ts
174 lines (163 loc) · 4.81 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { fileURLToPath } from 'node:url'
import { join } from 'node:path'
import replace from '@rollup/plugin-replace'
import type { Plugin } from 'histoire'
import type { Nuxt } from '@nuxt/schema'
import type { UserConfig as ViteConfig } from 'vite'
const ignorePlugins = [
'nuxt:vite-node-server',
'nuxt:dev-style-ssr',
'nuxt:vite-relative-asset',
'nuxt:cache-dir',
'nuxt:dynamic-base-path',
'nuxt:import-protection',
]
export function HstNuxt(): Plugin {
let nuxt: Nuxt
return {
name: '@histoire/plugin-nuxt',
async defaultConfig() {
const nuxtViteConfig = await useNuxtViteConfig()
const { viteConfig } = nuxtViteConfig
nuxt = nuxtViteConfig.nuxt // We save it to close it later
const plugins = viteConfig.plugins.filter((p: any) => !ignorePlugins.includes(p?.name))
return {
vite: {
server: {
watch: viteConfig.server.watch,
fs: {
allow: viteConfig.server.fs.allow,
},
middlewareMode: false,
},
define: {
...viteConfig.define,
'process.server': false,
'process.client': true,
'process.browser': true,
'process.nitro': false,
'process.prerender': false,
},
resolve: {
alias: viteConfig.resolve.alias,
extensions: viteConfig.resolve.extensions,
dedupe: viteConfig.resolve.dedupe,
},
plugins: [
...plugins,
replace({
values: {
'import.meta.server': 'false',
'import.meta.client': 'true',
},
preventAssignment: true,
}),
],
css: viteConfig.css,
publicDir: viteConfig.publicDir,
optimizeDeps: {
...viteConfig.optimizeDeps,
exclude: [
...viteConfig.optimizeDeps.exclude,
'@histoire/plugin-nuxt',
],
},
// @ts-expect-error Vue-specific config
vue: viteConfig.vue,
logLevel: 'info',
},
setupCode: [
`${nuxt.options.css.map(file => `import '${file}'`).join('\n')}`,
`import { setupNuxtApp } from '@histoire/plugin-nuxt/dist/runtime/app-setup.js'
export async function setupVue3 () {
await setupNuxtApp(${JSON.stringify(nuxt.options.runtimeConfig.public)})
}`,
],
viteNodeInlineDeps: [
/\/(nuxt|nuxt3)\//,
/^#/,
/\.nuxt/,
...(nuxt.options.build.transpile.filter(
r => typeof r === 'string' || r instanceof RegExp,
) as Array<string | RegExp>),
],
build: {
excludeFromVendorsChunk: [
/nuxt\/dist\/app/,
],
},
}
},
onDev(api, onCleanup) {
onCleanup(async () => {
nuxt?.close()
})
},
onBuild() {
nuxt?.close()
},
onPreview() {
nuxt?.close()
},
}
}
async function useNuxtViteConfig() {
const { loadNuxt, buildNuxt } = await import('@nuxt/kit')
const nuxt = await loadNuxt({
// cwd: process.cwd(),
ready: false,
dev: true,
overrides: {
devtools: { enabled: false },
ssr: false,
experimental: {
appManifest: false,
},
app: {
rootId: 'nuxt-test',
},
pages: false,
typescript: {
typeCheck: false,
},
},
})
if (nuxt.options.builder as string !== '@nuxt/vite-builder') {
throw new Error(`Histoire only supports Vite bundler, but Nuxt builder is currently set to '${nuxt.options.builder}'.`)
}
const runtimeDir = fileURLToPath(new URL('../runtime', import.meta.url))
nuxt.options.build.templates.push(
{ src: join(runtimeDir, 'components.mjs'), filename: 'histoire/components.mjs' },
)
nuxt.hook('app:templates', (app) => {
app.templates = app.templates.filter(template => template.filename !== 'app-component.mjs')
app.templates.push({ src: join(runtimeDir, 'app-component.mjs'), filename: 'app-component.mjs' })
})
return {
viteConfig: await new Promise<ViteConfig>((resolve, reject) => {
nuxt.hook('modules:done', () => {
nuxt.hook('components:extend', (components) => {
for (const name of ['NuxtLink']) {
Object.assign(components.find(c => c.pascalName === name) || {}, {
export: name,
filePath: '#build/histoire/components.mjs',
})
}
})
nuxt.hook('vite:configResolved', (config, { isClient }) => {
if (isClient) {
resolve(config)
}
})
})
nuxt.ready()
.then(() => buildNuxt(nuxt))
.catch((err) => {
if (!err.toString().includes('_stop_')) {
reject(err)
}
})
}),
nuxt,
}
}