This repository was archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpreset.ts
More file actions
129 lines (112 loc) · 3.29 KB
/
Copy pathpreset.ts
File metadata and controls
129 lines (112 loc) · 3.29 KB
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
import type { UserConfig as ViteConfig } from 'vite';
import type { StorybookConfig } from '@storybook/vue3-vite';
import { fileURLToPath } from 'node:url';
import { join } from 'node:path';
import { mergeConfig } from 'vite';
const runtimeDir = fileURLToPath(new URL('../runtime', import.meta.url));
export const viteFinal: StorybookConfig['viteFinal'] = async (config) => {
const { viteConfig } = await initNuxt();
if (config.plugins) {
config.plugins = config.plugins.filter((plugin) => {
return (plugin as any).name !== 'vite:vue';
});
}
return mergeConfig(
{
resolve: viteConfig.resolve,
optimizeDeps: viteConfig.optimizeDeps,
plugins: viteConfig.plugins,
define: viteConfig.define,
},
config
);
};
async function initNuxt() {
const { loadNuxt, buildNuxt } = await import('@nuxt/kit');
const nuxt = await loadNuxt({
// cwd: process.cwd(),
ready: false,
dev: process.env.NODE_ENV === 'development',
overrides: {
ssr: false,
app: {
rootId: 'nuxt-test',
},
pages: false,
// debug: true,
},
});
if ((nuxt.options.builder as string) !== '@nuxt/vite-builder') {
throw new Error(
`Storybook addon Nuxt only supports Vite bundler, but Nuxt builder is currently set to '${nuxt.options.builder}'.`
);
}
nuxt.options.build.templates.push(
{ src: join(runtimeDir, 'composables.mjs'), filename: 'storybook/composables.mjs' },
{ src: join(runtimeDir, 'components.mjs'), filename: 'storybook/components.mjs' }
);
stubApp();
stubComposables();
stubComponents();
const [viteConfig] = await Promise.all([waitForConfig(), waitForNuxt()]);
function stubApp() {
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',
});
});
}
function stubComposables() {
nuxt.hook('imports:sources', (presets) => {
const stubbedComposables = ['useNuxtApp'];
const appPreset = presets.find((p) => p.from === '#app');
if (appPreset) {
appPreset.imports = appPreset.imports.filter(
(i) => typeof i !== 'string' || !stubbedComposables.includes(i)
);
}
presets.push({
from: '#build/storybook/composables.mjs',
imports: stubbedComposables,
});
});
}
function stubComponents() {
nuxt.hook('components:extend', (components) => {
for (const name of ['NuxtLink']) {
const component = components.find((c) => c.pascalName === name);
if (component) {
Object.assign(component, {
export: name,
filePath: '#build/storybook/components.mjs',
});
}
}
});
}
async function waitForConfig() {
return new Promise<ViteConfig>((resolve) => {
nuxt.hook('vite:extendConfig', (config, { isClient }) => {
if (isClient) {
resolve(config);
}
});
});
}
async function waitForNuxt() {
try {
await nuxt.ready();
await buildNuxt(nuxt);
} catch (err) {
if (!err.toString().includes('_stop_')) {
throw err;
}
}
}
return {
viteConfig,
nuxt,
};
}