-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
create-vite.ts
269 lines (253 loc) · 8.85 KB
/
create-vite.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import type { AstroSettings } from '../@types/astro';
import type { LogOptions } from './logger/core';
import nodeFs from 'fs';
import { fileURLToPath } from 'url';
import * as vite from 'vite';
import { crawlFrameworkPkgs } from 'vitefu';
import astroPostprocessVitePlugin from '../vite-plugin-astro-postprocess/index.js';
import { vitePluginAstroServer } from '../vite-plugin-astro-server/index.js';
import astroVitePlugin from '../vite-plugin-astro/index.js';
import configAliasVitePlugin from '../vite-plugin-config-alias/index.js';
import envVitePlugin from '../vite-plugin-env/index.js';
import htmlVitePlugin from '../vite-plugin-html/index.js';
import astroIntegrationsContainerPlugin from '../vite-plugin-integrations-container/index.js';
import jsxVitePlugin from '../vite-plugin-jsx/index.js';
import astroLoadFallbackPlugin from '../vite-plugin-load-fallback/index.js';
import legacyMarkdownVitePlugin from '../vite-plugin-markdown-legacy/index.js';
import markdownVitePlugin from '../vite-plugin-markdown/index.js';
import astroScriptsPlugin from '../vite-plugin-scripts/index.js';
import astroScriptsPageSSRPlugin from '../vite-plugin-scripts/page-ssr.js';
import { createCustomViteLogger } from './errors/dev/index.js';
import { resolveDependency } from './util.js';
interface CreateViteOptions {
settings: AstroSettings;
logging: LogOptions;
mode: 'dev' | 'build' | string;
fs?: typeof nodeFs;
}
const ALWAYS_NOEXTERNAL = new Set([
// This is only because Vite's native ESM doesn't resolve "exports" correctly.
'astro',
// Vite fails on nested `.astro` imports without bundling
'astro/components',
// Handle recommended nanostores. Only @nanostores/preact is required from our testing!
// Full explanation and related bug report: https://github.com/withastro/astro/pull/3667
'@nanostores/preact',
// fontsource packages are CSS that need to be processed
'@fontsource/*',
]);
function getSsrNoExternalDeps(projectRoot: URL): string[] {
let noExternalDeps = [];
for (const dep of ALWAYS_NOEXTERNAL) {
try {
resolveDependency(dep, projectRoot);
noExternalDeps.push(dep);
} catch {
// ignore dependency if *not* installed / present in your project
// prevents hard error from Vite!
}
}
return noExternalDeps;
}
/** Return a common starting point for all Vite actions */
export async function createVite(
commandConfig: vite.InlineConfig,
{ settings, logging, mode, fs = nodeFs }: CreateViteOptions
): Promise<vite.InlineConfig> {
const astroPkgsConfig = await crawlFrameworkPkgs({
root: fileURLToPath(settings.config.root),
isBuild: mode === 'build',
viteUserConfig: settings.config.vite,
isFrameworkPkgByJson(pkgJson) {
return (
// Attempt: package relies on `astro`. ✅ Definitely an Astro package
pkgJson.peerDependencies?.astro ||
pkgJson.dependencies?.astro ||
// Attempt: package is tagged with `astro` or `astro-component`. ✅ Likely a community package
pkgJson.keywords?.includes('astro') ||
pkgJson.keywords?.includes('astro-component') ||
// Attempt: package is named `astro-something` or `@scope/astro-something`. ✅ Likely a community package
/^(@[^\/]+\/)?astro\-/.test(pkgJson.name)
);
},
isFrameworkPkgByName(pkgName) {
const isNotAstroPkg = isCommonNotAstro(pkgName);
if (isNotAstroPkg) {
return false;
} else {
return undefined;
}
},
});
// Start with the Vite configuration that Astro core needs
const commonConfig: vite.InlineConfig = {
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', settings.config.root)), // using local caches allows Astro to be used in monorepos, etc.
clearScreen: false, // we want to control the output, not Vite
logLevel: 'warn', // log warnings and errors only
appType: 'custom',
optimizeDeps: {
entries: ['src/**/*'],
exclude: ['node-fetch'],
},
plugins: [
configAliasVitePlugin({ settings }),
astroLoadFallbackPlugin({ fs, root: settings.config.root }),
astroVitePlugin({ settings, logging }),
astroScriptsPlugin({ settings }),
// The server plugin is for dev only and having it run during the build causes
// the build to run very slow as the filewatcher is triggered often.
mode !== 'build' && vitePluginAstroServer({ settings, logging, fs }),
envVitePlugin({ settings }),
settings.config.legacy.astroFlavoredMarkdown
? legacyMarkdownVitePlugin({ settings, logging })
: markdownVitePlugin({ settings, logging }),
htmlVitePlugin(),
jsxVitePlugin({ settings, logging }),
astroPostprocessVitePlugin({ settings }),
astroIntegrationsContainerPlugin({ settings, logging }),
astroScriptsPageSSRPlugin({ settings }),
],
publicDir: fileURLToPath(settings.config.publicDir),
root: fileURLToPath(settings.config.root),
envPrefix: 'PUBLIC_',
define: {
'import.meta.env.SITE': settings.config.site ? `'${settings.config.site}'` : 'undefined',
},
server: {
hmr:
process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'production'
? false
: undefined, // disable HMR for test
// handle Vite URLs
proxy: {
// add proxies here
},
watch: {
// Prevent watching during the build to speed it up
ignored: mode === 'build' ? ['**'] : undefined,
},
},
css: {
postcss: settings.config.style.postcss || {},
},
resolve: {
alias: [
{
// This is needed for Deno compatibility, as the non-browser version
// of this module depends on Node `crypto`
find: 'randombytes',
replacement: 'randombytes/browser',
},
{
// Typings are imported from 'astro' (e.g. import { Type } from 'astro')
find: /^astro$/,
replacement: fileURLToPath(new URL('../@types/astro', import.meta.url)),
},
],
conditions: ['astro'],
// Astro imports in third-party packages should use the same version as root
dedupe: ['astro'],
},
ssr: {
noExternal: [
...getSsrNoExternalDeps(settings.config.root),
...astroPkgsConfig.ssr.noExternal,
],
// shiki is imported by Code.astro, which is no-externalized (processed by Vite).
// However, shiki's deps are in CJS and trips up Vite's dev SSR transform, externalize
// shiki to load it with node instead.
external: [...(mode === 'dev' ? ['shiki'] : []), ...astroPkgsConfig.ssr.external],
},
};
// Merge configs: we merge vite configuration objects together in the following order,
// where future values will override previous values.
// 1. common vite config
// 2. user-provided vite config, via AstroConfig
// 3. integration-provided vite config, via the `config:setup` hook
// 4. command vite config, passed as the argument to this function
let result = commonConfig;
result = vite.mergeConfig(result, settings.config.vite || {});
result = vite.mergeConfig(result, commandConfig);
if (result.plugins) {
sortPlugins(result.plugins);
}
result.customLogger = createCustomViteLogger(result.logLevel ?? 'warn');
return result;
}
function isVitePlugin(plugin: vite.PluginOption): plugin is vite.Plugin {
return Boolean(plugin?.hasOwnProperty('name'));
}
function findPluginIndexByName(pluginOptions: vite.PluginOption[], name: string): number {
return pluginOptions.findIndex(function (pluginOption) {
// Use isVitePlugin to ignore nulls, booleans, promises, and arrays
// CAUTION: could be a problem if a plugin we're searching for becomes async!
return isVitePlugin(pluginOption) && pluginOption.name === name;
});
}
function sortPlugins(pluginOptions: vite.PluginOption[]) {
// HACK: move mdxPlugin to top because it needs to run before internal JSX plugin
const mdxPluginIndex = findPluginIndexByName(pluginOptions, '@mdx-js/rollup');
if (mdxPluginIndex === -1) return;
const jsxPluginIndex = findPluginIndexByName(pluginOptions, 'astro:jsx');
const mdxPlugin = pluginOptions[mdxPluginIndex];
pluginOptions.splice(mdxPluginIndex, 1);
pluginOptions.splice(jsxPluginIndex, 0, mdxPlugin);
}
const COMMON_DEPENDENCIES_NOT_ASTRO = [
'autoprefixer',
'react',
'react-dom',
'preact',
'preact-render-to-string',
'vue',
'svelte',
'solid-js',
'lit',
'cookie',
'dotenv',
'esbuild',
'eslint',
'jest',
'postcss',
'prettier',
'astro',
'tslib',
'typescript',
'vite',
];
const COMMON_PREFIXES_NOT_ASTRO = [
'@webcomponents/',
'@fontsource/',
'@postcss-plugins/',
'@rollup/',
'@astrojs/renderer-',
'@types/',
'@typescript-eslint/',
'eslint-',
'jest-',
'postcss-plugin-',
'prettier-plugin-',
'remark-',
'rehype-',
'rollup-plugin-',
'vite-plugin-',
];
function isCommonNotAstro(dep: string): boolean {
return (
COMMON_DEPENDENCIES_NOT_ASTRO.includes(dep) ||
COMMON_PREFIXES_NOT_ASTRO.some(
(prefix) =>
prefix.startsWith('@')
? dep.startsWith(prefix)
: dep.substring(dep.lastIndexOf('/') + 1).startsWith(prefix) // check prefix omitting @scope/
)
);
}
interface PkgJSON {
name: string;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
peerDependencies?: Record<string, string>;
keywords?: string[];
[key: string]: any;
}