-
Notifications
You must be signed in to change notification settings - Fork 109
/
vite-config.ts
145 lines (129 loc) · 4.67 KB
/
vite-config.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
import * as path from 'path';
import fs from 'fs';
import { Plugin } from 'vite';
import { allowedEnvPrefix as envPrefix } from './envs';
import { TypescriptConfig } from '@storybook/core-common';
import { mockCoreJs } from './mock-core-js';
import { codeGeneratorPlugin } from './code-generator-plugin';
import { injectExportOrderPlugin } from './inject-export-order-plugin';
import { mdxPlugin } from './mdx-plugin';
import { noFouc } from './plugins/no-fouc';
import { sourceLoaderPlugin } from './source-loader-plugin';
import type { UserConfig } from 'vite';
import type { ExtendedOptions } from './types';
export type PluginConfigType = 'build' | 'development';
export function readPackageJson(): Record<string, any> | false {
const packageJsonPath = path.resolve('package.json');
if (!fs.existsSync(packageJsonPath)) {
return false;
}
const jsonContent = fs.readFileSync(packageJsonPath, 'utf8');
return JSON.parse(jsonContent);
}
// Vite config that is common to development and production mode
export async function commonConfig(
options: ExtendedOptions,
_type: PluginConfigType
): Promise<UserConfig & { configFile: false; root: string }> {
const { framework } = options;
return {
configFile: false,
root: path.resolve(options.configDir, '..'),
cacheDir: 'node_modules/.vite-storybook',
envPrefix,
define: {},
resolve:
framework === 'vue3'
? {
alias: {
vue: 'vue/dist/vue.esm-bundler.js',
},
}
: {},
plugins: await pluginConfig(options, _type),
};
}
export async function pluginConfig(options: ExtendedOptions, _type: PluginConfigType) {
const { framework, presets } = options;
const svelteOptions = await presets.apply('svelteOptions', {}, options);
const plugins = [
codeGeneratorPlugin(options),
mockCoreJs(),
sourceLoaderPlugin(),
mdxPlugin(),
noFouc(),
injectExportOrderPlugin,
] as Plugin[];
if (framework === 'vue' || framework === 'vue3') {
try {
const vuePlugin = require('@vitejs/plugin-vue');
plugins.push(vuePlugin());
const { vueDocgen } = await import('./plugins/vue-docgen');
plugins.push(vueDocgen());
} catch (err) {
if ((err as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {
throw new Error(
'@storybook/builder-vite requires @vitejs/plugin-vue to be installed ' +
'when using @storybook/vue or @storybook/vue3.' +
' Please install it and start storybook again.'
);
}
throw err;
}
}
if (framework === 'svelte') {
try {
const sveltePlugin = require('@sveltejs/vite-plugin-svelte').svelte;
plugins.push(sveltePlugin(svelteOptions));
} catch (err) {
if ((err as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {
throw new Error(
'@storybook/builder-vite requires @sveltejs/vite-plugin-svelte to be installed' +
' when using @storybook/svelte.' +
' Please install it and start storybook again.'
);
}
throw err;
}
try {
const csfPlugin = require('./svelte/csf-plugin').default;
plugins.push(csfPlugin(svelteOptions));
} catch (err) {
if ((err as NodeJS.ErrnoException).code !== 'MODULE_NOT_FOUND') {
throw new Error(
'@storybook/builder-vite requires @storybook/addon-svelte-csf to be installed when using @storybook/svelte.' +
' Please install it and start storybook again.'
);
}
throw err;
}
}
if (framework === 'react') {
plugins.push(
require('@vitejs/plugin-react')({
// Do not treat story files as HMR boundaries, storybook itself needs to handle them.
exclude: [/\.stories\.([tj])sx?$/, /node_modules/],
})
);
const { reactDocgen, reactDocgenTypescriptOptions } = await presets.apply('typescript', {} as TypescriptConfig);
let typescriptPresent;
try {
const pkgJson = readPackageJson();
typescriptPresent = pkgJson && (pkgJson?.devDependencies?.typescript || pkgJson?.dependencies?.typescript);
} catch (e) {
typescriptPresent = false;
}
if (reactDocgen === 'react-docgen-typescript' && typescriptPresent) {
plugins.push(require('@joshwooding/vite-plugin-react-docgen-typescript').default(reactDocgenTypescriptOptions));
} else if (reactDocgen) {
const { reactDocgen } = await import('./plugins/react-docgen');
// Needs to run before the react plugin, so add to the front
plugins.unshift(reactDocgen());
}
}
if (framework === 'glimmerx') {
const plugin = require('vite-plugin-glimmerx/index.cjs');
plugins.push(plugin.default());
}
return plugins;
}