Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to Vite 3 ⚡️ #3570

Merged
merged 46 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
17c9111
Vite 3 test
matthewp Jun 10, 2022
f803d80
deps: bump to Vite beta.1
bholmesdev Jun 22, 2022
aab15e5
refactor: move to use optimizeDeps.force option
bholmesdev Jun 22, 2022
22ddb74
refactor: stub out new updateModuleInfo params
bholmesdev Jun 22, 2022
3364daf
nit: remove comment on deprecated Vite feature
bholmesdev Jun 22, 2022
257bdbf
nit: remove comment on deprecated vite feature
bholmesdev Jun 22, 2022
90df3c9
hail mary: destroy all ssr external / noexternal!
bholmesdev Jun 23, 2022
bde086a
fix: use new middlewareMode config settings
bholmesdev Jun 23, 2022
fa373a7
fix: resolve npm package paths for rollup input
bholmesdev Jun 23, 2022
3d52d8c
wip: revert to unresolved. Issue reported!
bholmesdev Jun 23, 2022
694b3f0
sad refactor: use legacy devDepsScanner for component HMR
bholmesdev Jun 23, 2022
89fa0a5
fix: add astro/components to noExternal for Code component
bholmesdev Jun 24, 2022
f08630b
refactor: use ALWAYS_NOEXTERNAL array
bholmesdev Jun 24, 2022
f69bc8c
refactor: add package.json to all test runners for noExternal error
bholmesdev Jun 24, 2022
7607ec4
deps: bump to latest vite 3 beta
bholmesdev Jun 27, 2022
2f00261
wip: add package.json to smoke
bholmesdev Jun 27, 2022
e72040a
fix: remove accidental "force true" on create-vite
bholmesdev Jun 27, 2022
2c23e8c
refactor: write smoke package.json programmatically
bholmesdev Jun 27, 2022
08820f1
refactor: add fontsource to noExternal
bholmesdev Jun 27, 2022
e3c4baa
fix: only add to ssr.noExternal if present in project
bholmesdev Jun 27, 2022
94fc44e
wip: what if we just... didn't have a memory test
bholmesdev Jun 28, 2022
169a9e5
deps: bump to latest vite beta
bholmesdev Jul 12, 2022
d825a35
Revert "wip: what if we just... didn't have a memory test"
bholmesdev Jul 12, 2022
58a2258
fix: add type check for plugin.name
bholmesdev Jul 12, 2022
39136c6
feat: remove legacy.devDepsScanner. Vite 3 strat is now Vite 2.x strat!
bholmesdev Jul 12, 2022
1aa8d29
fix: add ssr.noExternal to components ex
bholmesdev Jul 12, 2022
7d4f733
wip: ignore with-mdx starter
bholmesdev Jul 12, 2022
022238b
fix: add serviceEntryPoint to ssr.noExternal
bholmesdev Jul 12, 2022
5a0f51b
temp: reset NODE_ENV on prod builds
bholmesdev Jul 12, 2022
08f3659
fix: missing async tag
bholmesdev Jul 12, 2022
f153a14
VITE 3 IS STABLE BABY
bholmesdev Jul 13, 2022
e334620
deps: bump svelte to vite 3
bholmesdev Jul 13, 2022
0b1af9d
deps: bump vue to vite 3
bholmesdev Jul 13, 2022
8d7301f
fix: resolve plugins for proper sorting
bholmesdev Jul 13, 2022
e1a6f5c
sad fix: regex "export default" out of CSS ssr
bholmesdev Jul 14, 2022
5863fdf
chore: add TODO to understand sad fix
bholmesdev Jul 14, 2022
7cc577e
Revert "fix: resolve plugins for proper sorting"
bholmesdev Jul 15, 2022
f417191
Revert "sad fix: regex "export default" out of CSS ssr"
bholmesdev Jul 15, 2022
97920e9
fix: sort plugins WITHOUT resolveConfig
bholmesdev Jul 15, 2022
e626a10
Revert "wip: ignore with-mdx starter"
bholmesdev Jul 15, 2022
147c9d7
chore: revert memory test changes
bholmesdev Jul 15, 2022
6fd11fe
chore: add nanostores/preact ot noexternal
bholmesdev Jul 15, 2022
561d4e8
chore: changeset
bholmesdev Jul 15, 2022
c7278e6
chore: changeset
bholmesdev Jul 15, 2022
339f150
deps: use Vite ^3.0.0
bholmesdev Jul 15, 2022
247494e
fix: add back third party astro pkg scanner
bholmesdev Jul 15, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/thirty-beans-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'astro': minor
'@astrojs/image': minor
'@astrojs/svelte': minor
'@astrojs/vue': minor
---

Bump to Vite 3!
8 changes: 7 additions & 1 deletion examples/component/demo/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { defineConfig } from 'astro/config';

// https://astro.build/config
export default defineConfig({});
export default defineConfig({
vite: {
ssr: {
noExternal: ['@example/my-component']
},
},
});
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"strip-ansi": "^7.0.1",
"supports-esm": "^1.0.0",
"tsconfig-resolver": "^3.0.1",
"vite": "^2.9.14",
"vite": "^3.0.0",
"yargs-parser": "^21.0.1",
"zod": "^3.17.3"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ class AstroBuilder {
mode: this.mode,
server: {
hmr: false,
middlewareMode: 'ssr',
middlewareMode: true,
},
appType: 'custom',
},
{ astroConfig: this.config, logging, mode: 'build' }
);
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function collectPagesData(
'build',
`${colors.bold(
route.component
)} is taking a bit longer to import. This is common for larger "Astro.glob(...)" or "import.meta.globEager(...)" calls, for instance. Hang tight!`
)} is taking a bit longer to import. This is common for larger "Astro.glob(...)" or "import.meta.glob(...)" calls, for instance. Hang tight!`
);
clearInterval(routeCollectionLogTimeout);
}, 10000);
Expand Down
10 changes: 6 additions & 4 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fileURLToPath } from 'url';
import * as vite from 'vite';
import { BuildInternals, createBuildInternals } from '../../core/build/internal.js';
import { prependForwardSlash } from '../../core/path.js';
import { emptyDir, removeDir } from '../../core/util.js';
import { emptyDir, removeDir, resolveDependency } from '../../core/util.js';
import { runHookBuildSetup } from '../../integrations/index.js';
import { rollupPluginAstroBuildCSS } from '../../vite-plugin-build-css/index.js';
import type { ViteConfigWithSSR } from '../create-vite';
Expand Down Expand Up @@ -67,10 +67,12 @@ export async function staticBuild(opts: StaticBuildOptions) {
const ssrResult = (await ssrBuild(opts, internals, pageInput)) as RollupOutput;
info(opts.logging, 'build', dim(`Completed in ${getTimeStat(timer.ssr, performance.now())}.`));

const clientInput = new Set<string>([
const rendererClientEntrypoints = opts.astroConfig._ctx.renderers.map((r) => r.clientEntrypoint).filter(a => typeof a === 'string') as string[]

const clientInput = new Set([
...internals.discoveredHydratedComponents,
...internals.discoveredClientOnlyComponents,
...(astroConfig._ctx.renderers.map((r) => r.clientEntrypoint).filter((a) => a) as string[]),
...rendererClientEntrypoints,
...internals.discoveredScripts,
]);

Expand Down Expand Up @@ -167,7 +169,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp
async function clientBuild(
opts: StaticBuildOptions,
internals: BuildInternals,
input: Set<string>
input: Set<string>,
) {
const { astroConfig, viteConfig } = opts;
const timer = performance.now();
Expand Down
91 changes: 49 additions & 42 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { AstroConfig } from '../@types/astro';
import type { LogOptions } from './logger/core';

import fs from 'fs';
import { builtinModules } from 'module';
import { fileURLToPath } from 'url';
import * as vite from 'vite';
import astroPostprocessVitePlugin from '../vite-plugin-astro-postprocess/index.js';
Expand All @@ -14,45 +13,47 @@ import astroIntegrationsContainerPlugin from '../vite-plugin-integrations-contai
import jsxVitePlugin from '../vite-plugin-jsx/index.js';
import markdownVitePlugin from '../vite-plugin-markdown/index.js';
import astroScriptsPlugin from '../vite-plugin-scripts/index.js';
import { resolveDependency } from './util.js';

// note: ssr is still an experimental API hence the type omission from `vite`
export type ViteConfigWithSSR = vite.InlineConfig & { ssr?: vite.SSROptions };

interface CreateViteOptions {
astroConfig: AstroConfig;
logging: LogOptions;
mode: 'dev' | 'build';
}

// Some packages are just external, and that’s the way it goes.
const ALWAYS_EXTERNAL = new Set([
...builtinModules.map((name) => `node:${name}`),
'@sveltejs/vite-plugin-svelte',
'micromark-util-events-to-acorn',
'@astrojs/markdown-remark',
// in-lined for markdown modules
'github-slugger',
'node-fetch',
'prismjs',
'shiki',
'unified',
'whatwg-url',
]);
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',
]);

// note: ssr is still an experimental API hence the type omission from `vite`
export type ViteConfigWithSSR = vite.InlineConfig & { ssr?: vite.SSROptions };

interface CreateViteOptions {
astroConfig: AstroConfig;
logging: LogOptions;
mode: 'dev' | 'build';
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: ViteConfigWithSSR,
{ astroConfig, logging, mode }: CreateViteOptions
): Promise<ViteConfigWithSSR> {
// Scan for any third-party Astro packages. Vite needs these to be passed to `ssr.noExternal`.
const astroPackages = await getAstroPackages(astroConfig);
const thirdPartyAstroPackages = await getAstroPackages(astroConfig);
// Start with the Vite configuration that Astro core needs
const commonConfig: ViteConfigWithSSR = {
cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.root)), // using local caches allows Astro to be used in monorepos, etc.
Expand Down Expand Up @@ -82,7 +83,6 @@ export async function createVite(
'import.meta.env.SITE': astroConfig.site ? `'${astroConfig.site}'` : 'undefined',
},
server: {
force: true, // force dependency rebuild (TODO: enabled only while next is unstable; eventually only call in "production" mode?)
hmr:
process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'production'
? false
Expand Down Expand Up @@ -115,11 +115,12 @@ export async function createVite(
],
conditions: ['astro'],
},
// Note: SSR API is in beta (https://vitejs.dev/guide/ssr.html)
ssr: {
external: [...ALWAYS_EXTERNAL],
noExternal: [...ALWAYS_NOEXTERNAL, ...astroPackages],
},
noExternal: [
...getSsrNoExternalDeps(astroConfig.root),
...thirdPartyAstroPackages,
],
}
};

// Merge configs: we merge vite configuration objects together in the following order,
Expand All @@ -131,27 +132,33 @@ export async function createVite(
let result = commonConfig;
result = vite.mergeConfig(result, astroConfig.vite || {});
result = vite.mergeConfig(result, commandConfig);
sortPlugins(result);
if (result.plugins) {
sortPlugins(result.plugins);
}

return result;
}

function getPluginName(plugin: vite.PluginOption) {
if (plugin && typeof plugin === 'object' && !Array.isArray(plugin)) {
return plugin.name;
}
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(result: ViteConfigWithSSR) {
function sortPlugins(pluginOptions: vite.PluginOption[]) {
// HACK: move mdxPlugin to top because it needs to run before internal JSX plugin
const mdxPluginIndex =
result.plugins?.findIndex((plugin) => getPluginName(plugin) === '@mdx-js/rollup') ?? -1;
const mdxPluginIndex = findPluginIndexByName(pluginOptions, '@mdx-js/rollup');
if (mdxPluginIndex === -1) return;
const jsxPluginIndex =
result.plugins?.findIndex((plugin) => getPluginName(plugin) === 'astro:jsx') ?? -1;
const mdxPlugin = result.plugins?.[mdxPluginIndex];
result.plugins?.splice(mdxPluginIndex, 1);
result.plugins?.splice(jsxPluginIndex, 0, mdxPlugin);
const jsxPluginIndex = findPluginIndexByName(pluginOptions, 'astro:jsx');
const mdxPlugin = pluginOptions[mdxPluginIndex];
pluginOptions.splice(mdxPluginIndex, 1);
pluginOptions.splice(jsxPluginIndex, 0, mdxPlugin);
}

// Scans `projectRoot` for third-party Astro packages that could export an `.astro` file
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ export function codeFrame(src: string, loc: ErrorPayload['err']['loc']): string
return output;
}

export function resolveDependency(dep: string, astroConfig: AstroConfig) {
export function resolveDependency(dep: string, projectRoot: URL) {
const resolved = resolve.sync(dep, {
basedir: fileURLToPath(astroConfig.root),
basedir: fileURLToPath(projectRoot),
});
// For Windows compat, we need a fully resolved `file://` URL string
return pathToFileURL(resolved).toString();
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-astro/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function trackCSSDependencies(
}

// Update the module graph, telling it about our CSS deps.
moduleGraph.updateModuleInfo(mod, depModules, new Set(), true);
moduleGraph.updateModuleInfo(mod, depModules, new Map(), new Set(), new Set(), true);
for (const dep of cssDeps) {
this.addWatchFile(dep);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/vite-plugin-astro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
configureServer(server) {
viteDevServer = server;
},
// note: don’t claim .astro files with resolveId() — it prevents Vite from transpiling the final JS (import.meta.globEager, etc.)
// note: don’t claim .astro files with resolveId() — it prevents Vite from transpiling the final JS (import.meta.glob, etc.)
async resolveId(id, from, opts) {
// If resolving from an astro subresource such as a hoisted script,
// we need to resolve relative paths ourselves.
Expand Down
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/api-routes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/api-routes",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/astro-response/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/astro-response",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/config-vite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/config-vite",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
13 changes: 13 additions & 0 deletions packages/astro/test/fixtures/fontsource-package/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineConfig } from 'astro/config';

// https://astro.build/config
export default defineConfig({
vite: {
ssr: {
noExternal: [
'@fontsource/montserrat',
'@fontsource/monofett',
]
}
}
});
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/hmr-css/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/hmr-css",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/import-ts-with-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/import-ts-with-js",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/page-level-styles/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/page-level-styles",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-api-route/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-api-route",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-assets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-assets",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-dynamic/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-dynamic",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-hoisted-script/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-hoisted-script",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-request/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-request",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/ssr-response/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/ssr-response",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/status-code/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/status-code",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Loading