-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
optimizeDeps.ts
152 lines (146 loc) · 4.33 KB
/
optimizeDeps.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
import * as path from 'path';
import { normalizePath, resolveConfig } from 'vite';
import type { InlineConfig as ViteInlineConfig, UserConfig } from 'vite';
import type { Options } from '@storybook/types';
import { listStories } from './list-stories';
const INCLUDE_CANDIDATES = [
'@base2/pretty-print-object',
'@emotion/core',
'@emotion/is-prop-valid',
'@emotion/styled',
'@mdx-js/react',
'@storybook/addon-docs > acorn-jsx',
'@storybook/addon-docs',
'@storybook/addon-essentials/docs/mdx-react-shim',
'@storybook/channel-postmessage',
'@storybook/channel-websocket',
'@storybook/client-api',
'@storybook/client-logger',
'@storybook/core/client',
'@storybook/global',
'@storybook/preview-api',
'@storybook/preview-web',
'@storybook/react > acorn-jsx',
'@storybook/react',
'@storybook/svelte',
'@storybook/types',
'@storybook/vue3',
'acorn-jsx',
'acorn-walk',
'acorn',
'airbnb-js-shims',
'ansi-to-html',
'axe-core',
'color-convert',
'deep-object-diff',
'doctrine',
'emotion-theming',
'escodegen',
'estraverse',
'fast-deep-equal',
'html-tags',
'isobject',
'jest-mock',
'loader-utils',
'lodash/camelCase.js',
'lodash/camelCase',
'lodash/cloneDeep.js',
'lodash/cloneDeep',
'lodash/countBy.js',
'lodash/countBy',
'lodash/debounce.js',
'lodash/debounce',
'lodash/isEqual.js',
'lodash/isEqual',
'lodash/isFunction.js',
'lodash/isFunction',
'lodash/isPlainObject.js',
'lodash/isPlainObject',
'lodash/isString.js',
'lodash/isString',
'lodash/kebabCase.js',
'lodash/kebabCase',
'lodash/mapKeys.js',
'lodash/mapKeys',
'lodash/mapValues.js',
'lodash/mapValues',
'lodash/merge.js',
'lodash/merge',
'lodash/mergeWith.js',
'lodash/mergeWith',
'lodash/pick.js',
'lodash/pick',
'lodash/pickBy.js',
'lodash/pickBy',
'lodash/startCase.js',
'lodash/startCase',
'lodash/throttle.js',
'lodash/throttle',
'lodash/uniq.js',
'lodash/uniq',
'lodash/upperFirst.js',
'lodash/upperFirst',
'markdown-to-jsx',
'memoizerific',
'overlayscrollbars',
'polished',
'prettier/parser-babel',
'prettier/parser-flow',
'prettier/parser-typescript',
'prop-types',
'qs',
'react-dom',
'react-dom/client',
'react-fast-compare',
'react-is',
'react-textarea-autosize',
'react',
'react/jsx-runtime',
'refractor/core',
'refractor/lang/bash.js',
'refractor/lang/css.js',
'refractor/lang/graphql.js',
'refractor/lang/js-extras.js',
'refractor/lang/json.js',
'refractor/lang/jsx.js',
'refractor/lang/markdown.js',
'refractor/lang/markup.js',
'refractor/lang/tsx.js',
'refractor/lang/typescript.js',
'refractor/lang/yaml.js',
'regenerator-runtime/runtime.js',
'slash',
'store2',
'synchronous-promise',
'telejson',
'ts-dedent',
'unfetch',
'util-deprecate',
'vue',
'warning',
];
/**
* Helper function which allows us to `filter` with an async predicate. Uses Promise.all for performance.
*/
const asyncFilter = async (arr: string[], predicate: (val: string) => Promise<boolean>) =>
Promise.all(arr.map(predicate)).then((results) => arr.filter((_v, index) => results[index]));
export async function getOptimizeDeps(config: ViteInlineConfig, options: Options) {
const { root = process.cwd() } = config;
const absoluteStories = await listStories(options);
const stories = absoluteStories.map((storyPath) => normalizePath(path.relative(root, storyPath)));
// TODO: check if resolveConfig takes a lot of time, possible optimizations here
const resolvedConfig = await resolveConfig(config, 'serve', 'development');
// This function converts ids which might include ` > ` to a real path, if it exists on disk.
// See https://github.com/vitejs/vite/blob/67d164392e8e9081dc3f0338c4b4b8eea6c5f7da/packages/vite/src/node/optimizer/index.ts#L182-L199
const resolve = resolvedConfig.createResolver({ asSrc: false });
const include = await asyncFilter(INCLUDE_CANDIDATES, async (id) => Boolean(await resolve(id)));
const optimizeDeps: UserConfig['optimizeDeps'] = {
...config.optimizeDeps,
// We don't need to resolve the glob since vite supports globs for entries.
entries: stories,
// We need Vite to precompile these dependencies, because they contain non-ESM code that would break
// if we served it directly to the browser.
include: [...include, ...(config.optimizeDeps?.include || [])],
};
return optimizeDeps;
}