From 17c911171ff8ea90ae08d9529b9a353495be389a Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 10 Jun 2022 12:44:42 -0400 Subject: [PATCH 01/46] Vite 3 test --- packages/astro/package.json | 2 +- pnpm-lock.yaml | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index d8cbd61a8cd3..10bb017286ae 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -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-alpha.10", "yargs-parser": "^21.0.1", "zod": "^3.17.3" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 34b60ae38f76..321f77446349 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -546,7 +546,7 @@ importers: strip-ansi: ^7.0.1 supports-esm: ^1.0.0 tsconfig-resolver: ^3.0.1 - vite: ^2.9.14 + vite: ^3.0.0-alpha.10 yargs-parser: ^21.0.1 zod: ^3.17.3 dependencies: @@ -604,7 +604,7 @@ importers: strip-ansi: 7.0.1 supports-esm: 1.0.0 tsconfig-resolver: 3.0.1 - vite: 2.9.14_sass@1.53.0 + vite: 3.0.0-alpha.10_sass@1.53.0 yargs-parser: 21.0.1 zod: 3.17.3 devDependencies: @@ -14908,14 +14908,15 @@ packages: fsevents: 2.3.2 dev: false - /vite/2.9.14_sass@1.53.0: - resolution: {integrity: sha512-P/UCjSpSMcE54r4mPak55hWAZPlyfS369svib/gpmz8/01L822lMPOJ/RYW6tLCe1RPvMvOsJ17erf55bKp4Hw==} - engines: {node: '>=12.2.0'} + /vite/3.0.0-alpha.10_sass@1.53.0: + resolution: {integrity: sha512-ihF4TE3+xvgmGaSIYrfxBfJqqXESAehkIBv8L/xG9OzNaUiGrOF2CUqsmnVQOJ/pFBAAMmLNgG75Mqbdzo7W6A==} + engines: {node: '>=14.6.0'} hasBin: true peerDependencies: less: '*' sass: '*' stylus: '*' + terser: ^5.4.0 peerDependenciesMeta: less: optional: true @@ -14923,6 +14924,8 @@ packages: optional: true stylus: optional: true + terser: + optional: true dependencies: esbuild: 0.14.48 postcss: 8.4.14 From f803d80fa855443b997f046dced5f12690a1be07 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 22 Jun 2022 19:32:26 -0400 Subject: [PATCH 02/46] deps: bump to Vite beta.1 --- packages/astro/package.json | 2 +- pnpm-lock.yaml | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index 10bb017286ae..72f37262f186 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -136,7 +136,7 @@ "strip-ansi": "^7.0.1", "supports-esm": "^1.0.0", "tsconfig-resolver": "^3.0.1", - "vite": "^3.0.0-alpha.10", + "vite": "3.0.0-beta.1", "yargs-parser": "^21.0.1", "zod": "^3.17.3" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 321f77446349..a21584b70c6a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -546,7 +546,7 @@ importers: strip-ansi: ^7.0.1 supports-esm: ^1.0.0 tsconfig-resolver: ^3.0.1 - vite: ^3.0.0-alpha.10 + vite: 3.0.0-beta.1 yargs-parser: ^21.0.1 zod: ^3.17.3 dependencies: @@ -604,7 +604,7 @@ importers: strip-ansi: 7.0.1 supports-esm: 1.0.0 tsconfig-resolver: 3.0.1 - vite: 3.0.0-alpha.10_sass@1.53.0 + vite: 3.0.0-beta.1_sass@1.53.0 yargs-parser: 21.0.1 zod: 3.17.3 devDependencies: @@ -14908,9 +14908,9 @@ packages: fsevents: 2.3.2 dev: false - /vite/3.0.0-alpha.10_sass@1.53.0: - resolution: {integrity: sha512-ihF4TE3+xvgmGaSIYrfxBfJqqXESAehkIBv8L/xG9OzNaUiGrOF2CUqsmnVQOJ/pFBAAMmLNgG75Mqbdzo7W6A==} - engines: {node: '>=14.6.0'} + /vite/3.0.0-beta.1_sass@1.53.0: + resolution: {integrity: sha512-7p9hoj4vHQdPWbWIkP33Y1vxD8JbPRHUl5IhO9xs5uuwFwzUiYC27z9bVnmB9VAj170fmI9VNhAJIvC/Kb9unA==} + engines: {node: '>=14.18.0'} hasBin: true peerDependencies: less: '*' From aab15e546a2d8d7b84600de7b963dd7e6c40c5ef Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 22 Jun 2022 19:41:43 -0400 Subject: [PATCH 03/46] refactor: move to use optimizeDeps.force option --- packages/astro/src/core/create-vite.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 07d73733f0b1..7073513ea7c5 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -59,6 +59,7 @@ export async function createVite( clearScreen: false, // we want to control the output, not Vite logLevel: 'warn', // log warnings and errors only optimizeDeps: { + force: true, entries: ['src/**/*'], // Try and scan a user’s project (won’t catch everything), exclude: ['node-fetch'], }, @@ -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 From 22ddb74df4547ba5fa7760461d49b279bf25a5e6 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 22 Jun 2022 19:41:58 -0400 Subject: [PATCH 04/46] refactor: stub out new updateModuleInfo params --- packages/astro/src/vite-plugin-astro/hmr.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/vite-plugin-astro/hmr.ts b/packages/astro/src/vite-plugin-astro/hmr.ts index cc9b26770c8d..3d990fe4bbce 100644 --- a/packages/astro/src/vite-plugin-astro/hmr.ts +++ b/packages/astro/src/vite-plugin-astro/hmr.ts @@ -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); } From 3364daf9c64c3145c0e3de9b5daa8f5a1b7bb7f6 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 22 Jun 2022 19:44:08 -0400 Subject: [PATCH 05/46] nit: remove comment on deprecated Vite feature --- packages/astro/src/core/build/page-data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/build/page-data.ts b/packages/astro/src/core/build/page-data.ts index fc8be5eab747..23fb0283da29 100644 --- a/packages/astro/src/core/build/page-data.ts +++ b/packages/astro/src/core/build/page-data.ts @@ -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); From 257bdbf42134c261dcca0c9a682c45f85e546bc3 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 22 Jun 2022 19:44:31 -0400 Subject: [PATCH 06/46] nit: remove comment on deprecated vite feature --- packages/astro/src/vite-plugin-astro/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index 7b58a62d370e..9dfbdddac4e2 100644 --- a/packages/astro/src/vite-plugin-astro/index.ts +++ b/packages/astro/src/vite-plugin-astro/index.ts @@ -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. From 90df3c9a85464214d5c03f4417a99c2f1534dc78 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 23 Jun 2022 11:31:16 -0400 Subject: [PATCH 07/46] hail mary: destroy all ssr external / noexternal! --- packages/astro/src/core/create-vite.ts | 115 ------------------------- 1 file changed, 115 deletions(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 7073513ea7c5..81f29109014b 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -15,28 +15,6 @@ import jsxVitePlugin from '../vite-plugin-jsx/index.js'; import markdownVitePlugin from '../vite-plugin-markdown/index.js'; import astroScriptsPlugin from '../vite-plugin-scripts/index.js'; -// 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', - // 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 }; @@ -51,8 +29,6 @@ export async function createVite( commandConfig: ViteConfigWithSSR, { astroConfig, logging, mode }: CreateViteOptions ): Promise { - // Scan for any third-party Astro packages. Vite needs these to be passed to `ssr.noExternal`. - const astroPackages = 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. @@ -115,11 +91,6 @@ 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], - }, }; // Merge configs: we merge vite configuration objects together in the following order, @@ -153,89 +124,3 @@ function sortPlugins(result: ViteConfigWithSSR) { result.plugins?.splice(mdxPluginIndex, 1); result.plugins?.splice(jsxPluginIndex, 0, mdxPlugin); } - -// Scans `projectRoot` for third-party Astro packages that could export an `.astro` file -// `.astro` files need to be built by Vite, so these should use `noExternal` -async function getAstroPackages({ root }: AstroConfig): Promise { - const pkgUrl = new URL('./package.json', root); - const pkgPath = fileURLToPath(pkgUrl); - if (!fs.existsSync(pkgPath)) return []; - - const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); - - const deps = [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.devDependencies || {})]; - - return deps.filter((dep) => { - // Attempt: package is common and not Astro. ❌ Skip these for perf - if (isCommonNotAstro(dep)) return false; - // Attempt: package is named `astro-something`. ✅ Likely a community package - if (/^astro\-/.test(dep)) return true; - const depPkgUrl = new URL(`./node_modules/${dep}/package.json`, root); - const depPkgPath = fileURLToPath(depPkgUrl); - if (!fs.existsSync(depPkgPath)) return false; - - const { - dependencies = {}, - peerDependencies = {}, - keywords = [], - } = JSON.parse(fs.readFileSync(depPkgPath, 'utf-8')); - // Attempt: package relies on `astro`. ✅ Definitely an Astro package - if (peerDependencies.astro || dependencies.astro) return true; - // Attempt: package is tagged with `astro` or `astro-component`. ✅ Likely a community package - if (keywords.includes('astro') || keywords.includes('astro-component')) return true; - return false; - }); -} - -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/ - ) - ); -} From bde086aa9777b1dc67c20c4907ffcdef8e33a8ed Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 23 Jun 2022 12:16:19 -0400 Subject: [PATCH 08/46] fix: use new middlewareMode config settings --- packages/astro/src/core/build/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/core/build/index.ts b/packages/astro/src/core/build/index.ts index 137c1e7a9b2d..ce8f485d4d0e 100644 --- a/packages/astro/src/core/build/index.ts +++ b/packages/astro/src/core/build/index.ts @@ -73,8 +73,9 @@ class AstroBuilder { mode: this.mode, server: { hmr: false, - middlewareMode: 'ssr', + middlewareMode: true, }, + appType: 'custom', }, { astroConfig: this.config, logging, mode: 'build' } ); From fa373a71fedac1d57771be71d9997e9a0a0fcabd Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 23 Jun 2022 16:36:17 -0400 Subject: [PATCH 09/46] fix: resolve npm package paths for rollup input --- packages/astro/src/core/build/static-build.ts | 14 ++++++++++---- packages/astro/src/core/util.ts | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index c912b74c1180..98fd669c4fd9 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -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'; @@ -67,16 +67,22 @@ 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([ + const clientInput = new Set([ ...internals.discoveredHydratedComponents, ...internals.discoveredClientOnlyComponents, - ...(astroConfig._ctx.renderers.map((r) => r.clientEntrypoint).filter((a) => a) as string[]), + ...opts.astroConfig._ctx.renderers.map((r) => r.clientEntrypoint).filter(a => typeof a === 'string') as string[], ...internals.discoveredScripts, ]); + // Resolve any npm package paths with resolveDependency + // before passing to Vite input + const resolvedClientInput = new Set([...clientInput] + .map(input => new URL(resolveDependency(input, opts.astroConfig.root)).pathname) + ); + // Run client build first, so the assets can be fed into the SSR rendered version. timer.clientBuild = performance.now(); - await clientBuild(opts, internals, clientInput); + await clientBuild(opts, internals, resolvedClientInput); timer.generate = performance.now(); if (opts.buildConfig.staticMode) { diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts index 2dae8ae12911..0bbc0a72ac0f 100644 --- a/packages/astro/src/core/util.ts +++ b/packages/astro/src/core/util.ts @@ -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(); From 3d52d8c2acded7beb4bf019ee93d91d46019aa8c Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 23 Jun 2022 17:50:11 -0400 Subject: [PATCH 10/46] wip: revert to unresolved. Issue reported! --- packages/astro/src/core/build/static-build.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/astro/src/core/build/static-build.ts b/packages/astro/src/core/build/static-build.ts index 98fd669c4fd9..8680438f8b0a 100644 --- a/packages/astro/src/core/build/static-build.ts +++ b/packages/astro/src/core/build/static-build.ts @@ -67,22 +67,18 @@ 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 rendererClientEntrypoints = opts.astroConfig._ctx.renderers.map((r) => r.clientEntrypoint).filter(a => typeof a === 'string') as string[] + const clientInput = new Set([ ...internals.discoveredHydratedComponents, ...internals.discoveredClientOnlyComponents, - ...opts.astroConfig._ctx.renderers.map((r) => r.clientEntrypoint).filter(a => typeof a === 'string') as string[], + ...rendererClientEntrypoints, ...internals.discoveredScripts, ]); - // Resolve any npm package paths with resolveDependency - // before passing to Vite input - const resolvedClientInput = new Set([...clientInput] - .map(input => new URL(resolveDependency(input, opts.astroConfig.root)).pathname) - ); - // Run client build first, so the assets can be fed into the SSR rendered version. timer.clientBuild = performance.now(); - await clientBuild(opts, internals, resolvedClientInput); + await clientBuild(opts, internals, clientInput); timer.generate = performance.now(); if (opts.buildConfig.staticMode) { @@ -173,7 +169,7 @@ async function ssrBuild(opts: StaticBuildOptions, internals: BuildInternals, inp async function clientBuild( opts: StaticBuildOptions, internals: BuildInternals, - input: Set + input: Set, ) { const { astroConfig, viteConfig } = opts; const timer = performance.now(); From 694b3f024b4ee3152236f4049514241321263904 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 23 Jun 2022 18:45:15 -0400 Subject: [PATCH 11/46] sad refactor: use legacy devDepsScanner for component HMR --- packages/astro/src/core/create-vite.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 81f29109014b..bc6e58a9cfd4 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -58,6 +58,11 @@ export async function createVite( define: { 'import.meta.env.SITE': astroConfig.site ? `'${astroConfig.site}'` : 'undefined', }, + // fixes HMR for static components + // TODO: remove! + legacy: { + devDepsScanner: true, + }, server: { hmr: process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'production' From 89fa0a56188380e3843c354b2ed823157ee924f5 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 24 Jun 2022 14:04:27 -0400 Subject: [PATCH 12/46] fix: add astro/components to noExternal for Code component --- packages/astro/src/core/create-vite.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index bc6e58a9cfd4..41eac34523c5 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -96,6 +96,9 @@ export async function createVite( ], conditions: ['astro'], }, + ssr: { + noExternal: ['astro', 'astro/components'], + } }; // Merge configs: we merge vite configuration objects together in the following order, From f08630bcc441ca4fa0223bec7c6b86f44df5aa7e Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 24 Jun 2022 14:05:57 -0400 Subject: [PATCH 13/46] refactor: use ALWAYS_NOEXTERNAL array --- packages/astro/src/core/create-vite.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 41eac34523c5..aef640cabd4a 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -24,6 +24,13 @@ interface CreateViteOptions { mode: 'dev' | 'build'; } +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', +]); + /** Return a common starting point for all Vite actions */ export async function createVite( commandConfig: ViteConfigWithSSR, @@ -97,7 +104,7 @@ export async function createVite( conditions: ['astro'], }, ssr: { - noExternal: ['astro', 'astro/components'], + noExternal: [...ALWAYS_NOEXTERNAL], } }; From f69bc8cae9eea90e47ef9b9ad216ab29c3a257d2 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 24 Jun 2022 14:43:45 -0400 Subject: [PATCH 14/46] refactor: add package.json to all test runners for noExternal error --- .../test/fixtures/api-routes/package.json | 8 ++ .../test/fixtures/astro-response/package.json | 8 ++ .../test/fixtures/config-vite/package.json | 8 ++ .../astro/test/fixtures/hmr-css/package.json | 8 ++ .../fixtures/import-ts-with-js/package.json | 8 ++ .../fixtures/page-level-styles/package.json | 8 ++ .../test/fixtures/ssr-api-route/package.json | 8 ++ .../test/fixtures/ssr-assets/package.json | 8 ++ .../test/fixtures/ssr-dynamic/package.json | 8 ++ .../fixtures/ssr-hoisted-script/package.json | 8 ++ .../test/fixtures/ssr-request/package.json | 8 ++ .../test/fixtures/ssr-response/package.json | 8 ++ .../test/fixtures/status-code/package.json | 8 ++ .../test/fixtures/type-imports/package.json | 8 ++ .../test/fixtures/unused-slot/package.json | 8 ++ pnpm-lock.yaml | 90 +++++++++++++++++++ 16 files changed, 210 insertions(+) create mode 100644 packages/astro/test/fixtures/api-routes/package.json create mode 100644 packages/astro/test/fixtures/astro-response/package.json create mode 100644 packages/astro/test/fixtures/config-vite/package.json create mode 100644 packages/astro/test/fixtures/hmr-css/package.json create mode 100644 packages/astro/test/fixtures/import-ts-with-js/package.json create mode 100644 packages/astro/test/fixtures/page-level-styles/package.json create mode 100644 packages/astro/test/fixtures/ssr-api-route/package.json create mode 100644 packages/astro/test/fixtures/ssr-assets/package.json create mode 100644 packages/astro/test/fixtures/ssr-dynamic/package.json create mode 100644 packages/astro/test/fixtures/ssr-hoisted-script/package.json create mode 100644 packages/astro/test/fixtures/ssr-request/package.json create mode 100644 packages/astro/test/fixtures/ssr-response/package.json create mode 100644 packages/astro/test/fixtures/status-code/package.json create mode 100644 packages/astro/test/fixtures/type-imports/package.json create mode 100644 packages/astro/test/fixtures/unused-slot/package.json diff --git a/packages/astro/test/fixtures/api-routes/package.json b/packages/astro/test/fixtures/api-routes/package.json new file mode 100644 index 000000000000..0f7052df4ec1 --- /dev/null +++ b/packages/astro/test/fixtures/api-routes/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/api-routes", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/astro-response/package.json b/packages/astro/test/fixtures/astro-response/package.json new file mode 100644 index 000000000000..a832534901e2 --- /dev/null +++ b/packages/astro/test/fixtures/astro-response/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/astro-response", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/config-vite/package.json b/packages/astro/test/fixtures/config-vite/package.json new file mode 100644 index 000000000000..c226030fbd54 --- /dev/null +++ b/packages/astro/test/fixtures/config-vite/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/config-vite", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/hmr-css/package.json b/packages/astro/test/fixtures/hmr-css/package.json new file mode 100644 index 000000000000..36bf56c915b3 --- /dev/null +++ b/packages/astro/test/fixtures/hmr-css/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/hmr-css", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/import-ts-with-js/package.json b/packages/astro/test/fixtures/import-ts-with-js/package.json new file mode 100644 index 000000000000..53405e943ce2 --- /dev/null +++ b/packages/astro/test/fixtures/import-ts-with-js/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/import-ts-with-js", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/page-level-styles/package.json b/packages/astro/test/fixtures/page-level-styles/package.json new file mode 100644 index 000000000000..a5df4918d1a5 --- /dev/null +++ b/packages/astro/test/fixtures/page-level-styles/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/page-level-styles", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-api-route/package.json b/packages/astro/test/fixtures/ssr-api-route/package.json new file mode 100644 index 000000000000..f31c4aeee69d --- /dev/null +++ b/packages/astro/test/fixtures/ssr-api-route/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-api-route", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-assets/package.json b/packages/astro/test/fixtures/ssr-assets/package.json new file mode 100644 index 000000000000..446962ec2b0d --- /dev/null +++ b/packages/astro/test/fixtures/ssr-assets/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-assets", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-dynamic/package.json b/packages/astro/test/fixtures/ssr-dynamic/package.json new file mode 100644 index 000000000000..f796908143f1 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-dynamic/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-dynamic", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-hoisted-script/package.json b/packages/astro/test/fixtures/ssr-hoisted-script/package.json new file mode 100644 index 000000000000..d77f090b1126 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-hoisted-script/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-hoisted-script", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-request/package.json b/packages/astro/test/fixtures/ssr-request/package.json new file mode 100644 index 000000000000..4b71cfa550c4 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-request/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-request", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-response/package.json b/packages/astro/test/fixtures/ssr-response/package.json new file mode 100644 index 000000000000..420d01c5571d --- /dev/null +++ b/packages/astro/test/fixtures/ssr-response/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr-response", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/status-code/package.json b/packages/astro/test/fixtures/status-code/package.json new file mode 100644 index 000000000000..fcb9f1c94382 --- /dev/null +++ b/packages/astro/test/fixtures/status-code/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/status-code", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/type-imports/package.json b/packages/astro/test/fixtures/type-imports/package.json new file mode 100644 index 000000000000..6efd1e208d5a --- /dev/null +++ b/packages/astro/test/fixtures/type-imports/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/type-imports", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/unused-slot/package.json b/packages/astro/test/fixtures/unused-slot/package.json new file mode 100644 index 000000000000..b30e1f40e39d --- /dev/null +++ b/packages/astro/test/fixtures/unused-slot/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/unused-slot", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a21584b70c6a..66c8e4bd29ae 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1055,6 +1055,12 @@ importers: '@astrojs/svelte': link:../../../../integrations/svelte astro: link:../../.. + packages/astro/test/fixtures/api-routes: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/astro pages: specifiers: astro: workspace:* @@ -1329,6 +1335,12 @@ importers: dependencies: astro: link:../../.. + packages/astro/test/fixtures/astro-response: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/astro-scripts: specifiers: astro: workspace:* @@ -1383,6 +1395,12 @@ importers: dependencies: astro: link:../../.. + packages/astro/test/fixtures/config-vite: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/css-assets: specifiers: '@astrojs/test-font-awesome-package': file:packages/font-awesome @@ -1487,6 +1505,18 @@ importers: '@fontsource/montserrat': 4.5.11 astro: link:../../.. + packages/astro/test/fixtures/hmr-css: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + + packages/astro/test/fixtures/import-ts-with-js: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/integration-add-page-extension: specifiers: astro: workspace:* @@ -1556,6 +1586,12 @@ importers: '@astrojs/preact': link:../../../../integrations/preact astro: link:../../.. + packages/astro/test/fixtures/page-level-styles: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/postcss: specifiers: '@astrojs/solid-js': workspace:* @@ -1685,6 +1721,24 @@ importers: '@astrojs/solid-js': link:../../../../integrations/solid astro: link:../../.. + packages/astro/test/fixtures/ssr-api-route: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + + packages/astro/test/fixtures/ssr-assets: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + + packages/astro/test/fixtures/ssr-dynamic: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/ssr-env: specifiers: '@astrojs/preact': workspace:* @@ -1693,6 +1747,12 @@ importers: '@astrojs/preact': link:../../../../integrations/preact astro: link:../../.. + packages/astro/test/fixtures/ssr-hoisted-script: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/ssr-markdown: specifiers: astro: workspace:* @@ -1713,6 +1773,18 @@ importers: dependencies: astro: link:../../.. + packages/astro/test/fixtures/ssr-request: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + + packages/astro/test/fixtures/ssr-response: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/ssr-scripts: specifiers: '@astrojs/preact': 'workspace:' @@ -1776,6 +1848,12 @@ importers: packages/astro/test/fixtures/static-build/pkg: specifiers: {} + packages/astro/test/fixtures/status-code: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/streaming: specifiers: astro: workspace:* @@ -1810,6 +1888,18 @@ importers: dependencies: astro: link:../../.. + packages/astro/test/fixtures/type-imports: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + + packages/astro/test/fixtures/unused-slot: + specifiers: + astro: workspace:* + dependencies: + astro: link:../../.. + packages/astro/test/fixtures/vue-component: specifiers: '@astrojs/vue': workspace:* From 7607ec4863f03dab4a45cd6a8d2546c5fc46ac1f Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 14:37:52 -0400 Subject: [PATCH 15/46] deps: bump to latest vite 3 beta --- packages/astro/package.json | 2 +- pnpm-lock.yaml | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index 72f37262f186..72c161be7dae 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -136,7 +136,7 @@ "strip-ansi": "^7.0.1", "supports-esm": "^1.0.0", "tsconfig-resolver": "^3.0.1", - "vite": "3.0.0-beta.1", + "vite": "3.0.0-beta.4", "yargs-parser": "^21.0.1", "zod": "^3.17.3" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 66c8e4bd29ae..139ff07d4a88 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -546,7 +546,7 @@ importers: strip-ansi: ^7.0.1 supports-esm: ^1.0.0 tsconfig-resolver: ^3.0.1 - vite: 3.0.0-beta.1 + vite: 3.0.0-beta.4 yargs-parser: ^21.0.1 zod: ^3.17.3 dependencies: @@ -604,7 +604,7 @@ importers: strip-ansi: 7.0.1 supports-esm: 1.0.0 tsconfig-resolver: 3.0.1 - vite: 3.0.0-beta.1_sass@1.53.0 + vite: 3.0.0-beta.4_sass@1.53.0 yargs-parser: 21.0.1 zod: 3.17.3 devDependencies: @@ -1882,19 +1882,19 @@ importers: postcss: 8.4.14 tailwindcss: 3.1.5 - packages/astro/test/fixtures/virtual-astro-file: + packages/astro/test/fixtures/type-imports: specifiers: astro: workspace:* dependencies: astro: link:../../.. - packages/astro/test/fixtures/type-imports: + packages/astro/test/fixtures/unused-slot: specifiers: astro: workspace:* dependencies: astro: link:../../.. - packages/astro/test/fixtures/unused-slot: + packages/astro/test/fixtures/virtual-astro-file: specifiers: astro: workspace:* dependencies: @@ -14998,8 +14998,8 @@ packages: fsevents: 2.3.2 dev: false - /vite/3.0.0-beta.1_sass@1.53.0: - resolution: {integrity: sha512-7p9hoj4vHQdPWbWIkP33Y1vxD8JbPRHUl5IhO9xs5uuwFwzUiYC27z9bVnmB9VAj170fmI9VNhAJIvC/Kb9unA==} + /vite/3.0.0-beta.4_sass@1.53.0: + resolution: {integrity: sha512-GAQwNdG0xqsxypUMHAQgxA91nhG0RLm28shBdqGGKgoUo6GHq1aZQM/8FLhq7RWkx8ItTTMWm0SWgG6CL01W4A==} engines: {node: '>=14.18.0'} hasBin: true peerDependencies: From 2f002614e0301d7d8007c27bc9efdfa529427c50 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 17:26:15 -0400 Subject: [PATCH 16/46] wip: add package.json to smoke --- scripts/memory/project/package.json | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 scripts/memory/project/package.json diff --git a/scripts/memory/project/package.json b/scripts/memory/project/package.json new file mode 100644 index 000000000000..6fff9ac81915 --- /dev/null +++ b/scripts/memory/project/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/smoke", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} From e72040ab4662b9dcaf375675b7bd205092929a7c Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 17:29:29 -0400 Subject: [PATCH 17/46] fix: remove accidental "force true" on create-vite --- packages/astro/src/core/create-vite.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index aef640cabd4a..a76f7078b53a 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -42,7 +42,6 @@ export async function createVite( clearScreen: false, // we want to control the output, not Vite logLevel: 'warn', // log warnings and errors only optimizeDeps: { - force: true, entries: ['src/**/*'], // Try and scan a user’s project (won’t catch everything), exclude: ['node-fetch'], }, From 2c23e8cc0d876c85847ee8b67da7c7d7dc9dcfbc Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 17:47:30 -0400 Subject: [PATCH 18/46] refactor: write smoke package.json programmatically --- scripts/memory/mk.js | 12 +++++++++++- scripts/memory/project/package.json | 8 -------- 2 files changed, 11 insertions(+), 9 deletions(-) delete mode 100644 scripts/memory/project/package.json diff --git a/scripts/memory/mk.js b/scripts/memory/mk.js index ae14e1191587..44cbe52778f7 100644 --- a/scripts/memory/mk.js +++ b/scripts/memory/mk.js @@ -1,6 +1,16 @@ import fs from 'fs'; -const pages = new URL('./project/src/pages/', import.meta.url); +const base = new URL('./project/', import.meta.url); +const pages = new URL('./src/pages/', base); + +await fs.promises.writeFile(new URL('./package.json', base), `{ + "name": "@test/smoke", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +}`); for (let i = 0; i < 100; i++) { let content = `--- diff --git a/scripts/memory/project/package.json b/scripts/memory/project/package.json deleted file mode 100644 index 6fff9ac81915..000000000000 --- a/scripts/memory/project/package.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "@test/smoke", - "version": "0.0.0", - "private": true, - "dependencies": { - "astro": "workspace:*" - } -} From 08820f17ff792d640c370e4708ab62b98f52fd81 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 17:53:25 -0400 Subject: [PATCH 19/46] refactor: add fontsource to noExternal --- .../fixtures/fontsource-package/astro.config.mjs | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 packages/astro/test/fixtures/fontsource-package/astro.config.mjs diff --git a/packages/astro/test/fixtures/fontsource-package/astro.config.mjs b/packages/astro/test/fixtures/fontsource-package/astro.config.mjs new file mode 100644 index 000000000000..03d5ecbec8f2 --- /dev/null +++ b/packages/astro/test/fixtures/fontsource-package/astro.config.mjs @@ -0,0 +1,13 @@ +import { defineConfig } from 'astro/config'; + +// https://astro.build/config +export default defineConfig({ + vite: { + ssr: { + noExternal: [ + '@fontsource/montserrat', + '@fontsource/monofett', + ] + } + } +}); From e3c4baa4f518d87357a171ecab0c63a1470f216b Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Mon, 27 Jun 2022 18:05:53 -0400 Subject: [PATCH 20/46] fix: only add to ssr.noExternal if present in project --- packages/astro/src/core/create-vite.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index a76f7078b53a..848077ffd2bf 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -14,6 +14,7 @@ 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 }; @@ -31,6 +32,20 @@ const ALWAYS_NOEXTERNAL = new Set([ 'astro/components', ]); +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, @@ -103,7 +118,7 @@ export async function createVite( conditions: ['astro'], }, ssr: { - noExternal: [...ALWAYS_NOEXTERNAL], + noExternal: getSsrNoExternalDeps(astroConfig.root), } }; From 94fc44e2353dd555a3f5ecdb043acdf4a25aaef3 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 28 Jun 2022 09:49:04 -0400 Subject: [PATCH 21/46] wip: what if we just... didn't have a memory test --- scripts/memory/index.js | 79 ----------------------- scripts/memory/mk.js | 21 ------ scripts/memory/project/.gitkeep | 0 scripts/memory/project/src/pages/.gitkeep | 0 4 files changed, 100 deletions(-) delete mode 100644 scripts/memory/index.js delete mode 100644 scripts/memory/mk.js delete mode 100644 scripts/memory/project/.gitkeep delete mode 100644 scripts/memory/project/src/pages/.gitkeep diff --git a/scripts/memory/index.js b/scripts/memory/index.js deleted file mode 100644 index 30cc1c0f2a8b..000000000000 --- a/scripts/memory/index.js +++ /dev/null @@ -1,79 +0,0 @@ -import { fileURLToPath } from 'url'; -import v8 from 'v8'; -import dev from '../../packages/astro/dist/core/dev/index.js'; -import { loadConfig } from '../../packages/astro/dist/core/config.js'; -import prettyBytes from 'pretty-bytes'; - -if (!global.gc) { - console.error('ERROR: Node must be run with --expose-gc'); - process.exit(1); -} - -const isCI = process.argv.includes('--ci'); - -/** URL directory containing the entire project. */ -const projDir = new URL('./project/', import.meta.url); - -let config = await loadConfig({ - cwd: fileURLToPath(projDir), -}); - -const telemetry = { - record() { - return Promise.resolve(); - }, -}; -const server = await dev(config, { logging: { level: 'error' }, telemetry }); - -// Prime the server so initial memory is created -await fetch(`http://localhost:3000/page-0`); - -async function run() { - for (let i = 0; i < 100; i++) { - let path = `/page-${i}`; - await fetch(`http://localhost:3000${path}`); - } -} - -global.gc(); -const startSize = v8.getHeapStatistics().used_heap_size; - -// HUMAN mode: Runs forever. Optimized for accurate results on each snapshot Slower than CI. -if (!isCI) { - console.log( - `Greetings, human. This test will run forever. Run with the "--ci" flag to finish with a result.` - ); - let i = 1; - while (i++) { - await run(); - global.gc(); - const checkpoint = v8.getHeapStatistics().used_heap_size; - console.log(`Snapshot ${String(i).padStart(3, '0')}: ${(checkpoint / startSize) * 100}%`); - } -} - -// CI mode: Runs 100 times. Optimized for speed with an accurate final result. -for (let i = 0; i < 100; i++) { - await run(); - const checkpoint = v8.getHeapStatistics().used_heap_size; - console.log(`Estimate ${String(i).padStart(3, '0')}/100: ${(checkpoint / startSize) * 100}%`); -} - -console.log(`Test complete. Running final garbage collection...`); -global.gc(); -const endSize = v8.getHeapStatistics().used_heap_size; - -// If the trailing average is higher than the median, see if it's more than 5% higher -let percentage = endSize / startSize; -const TEST_THRESHOLD = 1.5; -const isPass = percentage < TEST_THRESHOLD; -console.log(``); -console.log(`Result: ${isPass ? 'PASS' : 'FAIL'} (${percentage * 100}%)`); -console.log( - `Memory usage began at ${prettyBytes(startSize)} and finished at ${prettyBytes(endSize)}.` -); -console.log(`The threshold for a probable memory leak is ${TEST_THRESHOLD * 100}%`); -console.log(``); -console.log(`Exiting...`); -await server.stop(); -process.exit(isPass ? 0 : 1); diff --git a/scripts/memory/mk.js b/scripts/memory/mk.js deleted file mode 100644 index 44cbe52778f7..000000000000 --- a/scripts/memory/mk.js +++ /dev/null @@ -1,21 +0,0 @@ -import fs from 'fs'; - -const base = new URL('./project/', import.meta.url); -const pages = new URL('./src/pages/', base); - -await fs.promises.writeFile(new URL('./package.json', base), `{ - "name": "@test/smoke", - "version": "0.0.0", - "private": true, - "dependencies": { - "astro": "workspace:*" - } -}`); - -for (let i = 0; i < 100; i++) { - let content = `--- -const i = ${i}; ---- -{i}`; - await fs.promises.writeFile(new URL(`./page-${i}.astro`, pages), content, 'utf-8'); -} diff --git a/scripts/memory/project/.gitkeep b/scripts/memory/project/.gitkeep deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/scripts/memory/project/src/pages/.gitkeep b/scripts/memory/project/src/pages/.gitkeep deleted file mode 100644 index e69de29bb2d1..000000000000 From 169a9e524f4b92175253b6f92aa5e2d2135f28ab Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 12 Jul 2022 12:31:21 -0400 Subject: [PATCH 22/46] deps: bump to latest vite beta --- packages/astro/package.json | 2 +- pnpm-lock.yaml | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index 72c161be7dae..d7387828dbb2 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -136,7 +136,7 @@ "strip-ansi": "^7.0.1", "supports-esm": "^1.0.0", "tsconfig-resolver": "^3.0.1", - "vite": "3.0.0-beta.4", + "vite": "3.0.0-beta.10", "yargs-parser": "^21.0.1", "zod": "^3.17.3" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 139ff07d4a88..fb322e91881f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -546,7 +546,7 @@ importers: strip-ansi: ^7.0.1 supports-esm: ^1.0.0 tsconfig-resolver: ^3.0.1 - vite: 3.0.0-beta.4 + vite: 3.0.0-beta.10 yargs-parser: ^21.0.1 zod: ^3.17.3 dependencies: @@ -604,7 +604,7 @@ importers: strip-ansi: 7.0.1 supports-esm: 1.0.0 tsconfig-resolver: 3.0.1 - vite: 3.0.0-beta.4_sass@1.53.0 + vite: 3.0.0-beta.10_sass@1.53.0 yargs-parser: 21.0.1 zod: 3.17.3 devDependencies: @@ -14998,8 +14998,8 @@ packages: fsevents: 2.3.2 dev: false - /vite/3.0.0-beta.4_sass@1.53.0: - resolution: {integrity: sha512-GAQwNdG0xqsxypUMHAQgxA91nhG0RLm28shBdqGGKgoUo6GHq1aZQM/8FLhq7RWkx8ItTTMWm0SWgG6CL01W4A==} + /vite/3.0.0-beta.10_sass@1.53.0: + resolution: {integrity: sha512-QtzSQVuhNLDp9j4wNdVT3aDyWyvoAL4QxBU/lnMyEKqTzYrXdqyHpFmfWW/wo/0uTluZKzbsUm89eQq2RxiKMg==} engines: {node: '>=14.18.0'} hasBin: true peerDependencies: From d825a35e18c6abaf7c45b03cf805461e94a37da6 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 12 Jul 2022 12:33:00 -0400 Subject: [PATCH 23/46] Revert "wip: what if we just... didn't have a memory test" This reverts commit 173729dbdc685e52881fc3333487b8f744add55f. --- scripts/memory/index.js | 79 +++++++++++++++++++++++ scripts/memory/mk.js | 21 ++++++ scripts/memory/project/.gitkeep | 0 scripts/memory/project/src/pages/.gitkeep | 0 4 files changed, 100 insertions(+) create mode 100644 scripts/memory/index.js create mode 100644 scripts/memory/mk.js create mode 100644 scripts/memory/project/.gitkeep create mode 100644 scripts/memory/project/src/pages/.gitkeep diff --git a/scripts/memory/index.js b/scripts/memory/index.js new file mode 100644 index 000000000000..30cc1c0f2a8b --- /dev/null +++ b/scripts/memory/index.js @@ -0,0 +1,79 @@ +import { fileURLToPath } from 'url'; +import v8 from 'v8'; +import dev from '../../packages/astro/dist/core/dev/index.js'; +import { loadConfig } from '../../packages/astro/dist/core/config.js'; +import prettyBytes from 'pretty-bytes'; + +if (!global.gc) { + console.error('ERROR: Node must be run with --expose-gc'); + process.exit(1); +} + +const isCI = process.argv.includes('--ci'); + +/** URL directory containing the entire project. */ +const projDir = new URL('./project/', import.meta.url); + +let config = await loadConfig({ + cwd: fileURLToPath(projDir), +}); + +const telemetry = { + record() { + return Promise.resolve(); + }, +}; +const server = await dev(config, { logging: { level: 'error' }, telemetry }); + +// Prime the server so initial memory is created +await fetch(`http://localhost:3000/page-0`); + +async function run() { + for (let i = 0; i < 100; i++) { + let path = `/page-${i}`; + await fetch(`http://localhost:3000${path}`); + } +} + +global.gc(); +const startSize = v8.getHeapStatistics().used_heap_size; + +// HUMAN mode: Runs forever. Optimized for accurate results on each snapshot Slower than CI. +if (!isCI) { + console.log( + `Greetings, human. This test will run forever. Run with the "--ci" flag to finish with a result.` + ); + let i = 1; + while (i++) { + await run(); + global.gc(); + const checkpoint = v8.getHeapStatistics().used_heap_size; + console.log(`Snapshot ${String(i).padStart(3, '0')}: ${(checkpoint / startSize) * 100}%`); + } +} + +// CI mode: Runs 100 times. Optimized for speed with an accurate final result. +for (let i = 0; i < 100; i++) { + await run(); + const checkpoint = v8.getHeapStatistics().used_heap_size; + console.log(`Estimate ${String(i).padStart(3, '0')}/100: ${(checkpoint / startSize) * 100}%`); +} + +console.log(`Test complete. Running final garbage collection...`); +global.gc(); +const endSize = v8.getHeapStatistics().used_heap_size; + +// If the trailing average is higher than the median, see if it's more than 5% higher +let percentage = endSize / startSize; +const TEST_THRESHOLD = 1.5; +const isPass = percentage < TEST_THRESHOLD; +console.log(``); +console.log(`Result: ${isPass ? 'PASS' : 'FAIL'} (${percentage * 100}%)`); +console.log( + `Memory usage began at ${prettyBytes(startSize)} and finished at ${prettyBytes(endSize)}.` +); +console.log(`The threshold for a probable memory leak is ${TEST_THRESHOLD * 100}%`); +console.log(``); +console.log(`Exiting...`); +await server.stop(); +process.exit(isPass ? 0 : 1); diff --git a/scripts/memory/mk.js b/scripts/memory/mk.js new file mode 100644 index 000000000000..44cbe52778f7 --- /dev/null +++ b/scripts/memory/mk.js @@ -0,0 +1,21 @@ +import fs from 'fs'; + +const base = new URL('./project/', import.meta.url); +const pages = new URL('./src/pages/', base); + +await fs.promises.writeFile(new URL('./package.json', base), `{ + "name": "@test/smoke", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +}`); + +for (let i = 0; i < 100; i++) { + let content = `--- +const i = ${i}; +--- +{i}`; + await fs.promises.writeFile(new URL(`./page-${i}.astro`, pages), content, 'utf-8'); +} diff --git a/scripts/memory/project/.gitkeep b/scripts/memory/project/.gitkeep new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/scripts/memory/project/src/pages/.gitkeep b/scripts/memory/project/src/pages/.gitkeep new file mode 100644 index 000000000000..e69de29bb2d1 From 58a22585e71be2bee72d1b2c27c54c18ea5a9789 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 12 Jul 2022 12:44:31 -0400 Subject: [PATCH 24/46] fix: add type check for plugin.name --- packages/astro/src/core/create-vite.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 848077ffd2bf..ed504bc4bb33 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -136,19 +136,24 @@ export async function createVite( return result; } -function getPluginName(plugin: vite.PluginOption) { +function getPluginName(plugin: vite.Plugin) { 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 sortPlugins(result: ViteConfigWithSSR) { + const plugins = result.plugins?.filter(isVitePlugin) ?? [] // 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; + plugins.findIndex((plugin) => getPluginName(plugin) === '@mdx-js/rollup') ?? -1; if (mdxPluginIndex === -1) return; const jsxPluginIndex = - result.plugins?.findIndex((plugin) => getPluginName(plugin) === 'astro:jsx') ?? -1; + plugins.findIndex((plugin) => getPluginName(plugin) === 'astro:jsx') ?? -1; const mdxPlugin = result.plugins?.[mdxPluginIndex]; result.plugins?.splice(mdxPluginIndex, 1); result.plugins?.splice(jsxPluginIndex, 0, mdxPlugin); From 39136c6ffcb96a798194680537d3be3bb3dfe945 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 12 Jul 2022 12:44:59 -0400 Subject: [PATCH 25/46] feat: remove legacy.devDepsScanner. Vite 3 strat is now Vite 2.x strat! --- packages/astro/src/core/create-vite.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index ed504bc4bb33..5ac0db3c3a71 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -79,11 +79,6 @@ export async function createVite( define: { 'import.meta.env.SITE': astroConfig.site ? `'${astroConfig.site}'` : 'undefined', }, - // fixes HMR for static components - // TODO: remove! - legacy: { - devDepsScanner: true, - }, server: { hmr: process.env.NODE_ENV === 'test' || process.env.NODE_ENV === 'production' From 1aa8d292b1927fdb0bc1bc47797933804de5d27a Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 12 Jul 2022 13:02:17 -0400 Subject: [PATCH 26/46] fix: add ssr.noExternal to components ex --- examples/component/demo/astro.config.mjs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/component/demo/astro.config.mjs b/examples/component/demo/astro.config.mjs index 882e6515a67e..d92b7a872cdb 100644 --- a/examples/component/demo/astro.config.mjs +++ b/examples/component/demo/astro.config.mjs @@ -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'] + }, + }, +}); From 7d4f7338e6a8427934668096dba35e2a06ab961f Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 12 Jul 2022 13:03:24 -0400 Subject: [PATCH 27/46] wip: ignore with-mdx starter --- examples/with-mdx/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/with-mdx/package.json b/examples/with-mdx/package.json index 79ca61952705..c8ca36682bbc 100644 --- a/examples/with-mdx/package.json +++ b/examples/with-mdx/package.json @@ -1,5 +1,5 @@ { - "name": "@example/with-mdx", + "name": "@example-ignore/with-mdx", "version": "0.0.1", "private": true, "scripts": { From 022238b7ea554187f55916bb157718b7c9962eba Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 12 Jul 2022 16:33:51 -0400 Subject: [PATCH 28/46] fix: add serviceEntryPoint to ssr.noExternal --- packages/integrations/image/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integrations/image/src/index.ts b/packages/integrations/image/src/index.ts index 3721f9667e28..08d6cfd04b4b 100644 --- a/packages/integrations/image/src/index.ts +++ b/packages/integrations/image/src/index.ts @@ -33,7 +33,7 @@ const createIntegration = (options: IntegrationOptions = {}): AstroIntegration = include: ['image-size', 'sharp'], }, ssr: { - noExternal: ['@astrojs/image'], + noExternal: ['@astrojs/image', resolvedOptions.serviceEntryPoint], }, }; } From 5a0f51b634f76fd50d632b501bd39fc5a9526656 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 12 Jul 2022 18:22:28 -0400 Subject: [PATCH 29/46] temp: reset NODE_ENV on prod builds --- packages/astro/test/test-utils.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/astro/test/test-utils.js b/packages/astro/test/test-utils.js index 346c2a2acd66..081045b3daba 100644 --- a/packages/astro/test/test-utils.js +++ b/packages/astro/test/test-utils.js @@ -130,7 +130,12 @@ export async function loadFixture(inlineConfig) { let devServer; return { - build: (opts = {}) => build(config, { logging, telemetry, ...opts }), + build: (opts = {}) => { + // TODO: remove once Vite resolves this internally + const initialNodeEnv = process.env.NODE_ENV; + await build(config, { logging, telemetry, ...opts }) + process.env.NODE_ENV = initialNodeEnv; + }, startDevServer: async (opts = {}) => { devServer = await dev(config, { logging, telemetry, ...opts }); config.server.port = devServer.address.port; // update port From 08f3659d0501047fe19f74cff9405cb2f8c75b30 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 12 Jul 2022 18:50:37 -0400 Subject: [PATCH 30/46] fix: missing async tag --- packages/astro/test/test-utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/test/test-utils.js b/packages/astro/test/test-utils.js index 081045b3daba..a24571fe5512 100644 --- a/packages/astro/test/test-utils.js +++ b/packages/astro/test/test-utils.js @@ -130,7 +130,7 @@ export async function loadFixture(inlineConfig) { let devServer; return { - build: (opts = {}) => { + build: async (opts = {}) => { // TODO: remove once Vite resolves this internally const initialNodeEnv = process.env.NODE_ENV; await build(config, { logging, telemetry, ...opts }) From f153a14f118ac83bcbe80bd5d620d14385405404 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 13 Jul 2022 11:06:48 -0400 Subject: [PATCH 31/46] VITE 3 IS STABLE BABY --- packages/astro/package.json | 2 +- pnpm-lock.yaml | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index d7387828dbb2..ba25dbbcb587 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -136,7 +136,7 @@ "strip-ansi": "^7.0.1", "supports-esm": "^1.0.0", "tsconfig-resolver": "^3.0.1", - "vite": "3.0.0-beta.10", + "vite": "3.0.0", "yargs-parser": "^21.0.1", "zod": "^3.17.3" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fb322e91881f..6a63ca903d71 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -546,7 +546,7 @@ importers: strip-ansi: ^7.0.1 supports-esm: ^1.0.0 tsconfig-resolver: ^3.0.1 - vite: 3.0.0-beta.10 + vite: 3.0.0 yargs-parser: ^21.0.1 zod: ^3.17.3 dependencies: @@ -604,7 +604,7 @@ importers: strip-ansi: 7.0.1 supports-esm: 1.0.0 tsconfig-resolver: 3.0.1 - vite: 3.0.0-beta.10_sass@1.53.0 + vite: 3.0.0_sass@1.53.0 yargs-parser: 21.0.1 zod: 3.17.3 devDependencies: @@ -14998,8 +14998,8 @@ packages: fsevents: 2.3.2 dev: false - /vite/3.0.0-beta.10_sass@1.53.0: - resolution: {integrity: sha512-QtzSQVuhNLDp9j4wNdVT3aDyWyvoAL4QxBU/lnMyEKqTzYrXdqyHpFmfWW/wo/0uTluZKzbsUm89eQq2RxiKMg==} + /vite/3.0.0_sass@1.53.0: + resolution: {integrity: sha512-M7phQhY3+fRZa0H+1WzI6N+/onruwPTBTMvaj7TzgZ0v2TE+N2sdLKxJOfOv9CckDWt5C4HmyQP81xB4dwRKzA==} engines: {node: '>=14.18.0'} hasBin: true peerDependencies: From e334620ec46b5e022fd370ac1412c7d48f585a46 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 13 Jul 2022 11:08:50 -0400 Subject: [PATCH 32/46] deps: bump svelte to vite 3 --- packages/integrations/svelte/package.json | 4 +- pnpm-lock.yaml | 45 ++++++++++++++++++----- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/packages/integrations/svelte/package.json b/packages/integrations/svelte/package.json index 37b71dbee517..d05671418210 100644 --- a/packages/integrations/svelte/package.json +++ b/packages/integrations/svelte/package.json @@ -33,11 +33,11 @@ "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { - "@sveltejs/vite-plugin-svelte": "^1.0.0-next.48", + "@sveltejs/vite-plugin-svelte": "^1.0.1", "postcss-load-config": "^3.1.4", "svelte-preprocess": "^4.10.7", "svelte2tsx": "^0.5.11", - "vite": "^2.9.10" + "vite": "^3.0.0" }, "devDependencies": { "astro": "workspace:*", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6a63ca903d71..b7c4822a278d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2262,20 +2262,20 @@ importers: packages/integrations/svelte: specifiers: - '@sveltejs/vite-plugin-svelte': ^1.0.0-next.48 + '@sveltejs/vite-plugin-svelte': ^1.0.1 astro: workspace:* astro-scripts: workspace:* postcss-load-config: ^3.1.4 svelte: ^3.48.0 svelte-preprocess: ^4.10.7 svelte2tsx: ^0.5.11 - vite: ^2.9.10 + vite: ^3.0.0 dependencies: - '@sveltejs/vite-plugin-svelte': 1.0.0-next.49_svelte@3.49.0+vite@2.9.14 + '@sveltejs/vite-plugin-svelte': 1.0.1_svelte@3.49.0+vite@3.0.0 postcss-load-config: 3.1.4 svelte-preprocess: 4.10.7_k3vaqsyrx2lfvza3vdeafxime4 svelte2tsx: 0.5.11_svelte@3.49.0 - vite: 2.9.14 + vite: 3.0.0 devDependencies: astro: link:../../astro astro-scripts: link:../../../scripts @@ -7346,13 +7346,13 @@ packages: string.prototype.matchall: 4.0.7 dev: true - /@sveltejs/vite-plugin-svelte/1.0.0-next.49_svelte@3.49.0+vite@2.9.14: - resolution: {integrity: sha512-AKh0Ka8EDgidnxWUs8Hh2iZLZovkETkefO99XxZ4sW4WGJ7VFeBx5kH/NIIGlaNHLcrIvK3CK0HkZwC3Cici0A==} - engines: {node: ^14.13.1 || >= 16} + /@sveltejs/vite-plugin-svelte/1.0.1_svelte@3.49.0+vite@3.0.0: + resolution: {integrity: sha512-PorCgUounn0VXcpeJu+hOweZODKmGuLHsLomwqSj+p26IwjjGffmYQfVHtiTWq+NqaUuuHWWG7vPge6UFw4Aeg==} + engines: {node: ^14.18.0 || >= 16} peerDependencies: diff-match-patch: ^1.0.5 svelte: ^3.44.0 - vite: ^2.9.0 + vite: ^3.0.0 peerDependenciesMeta: diff-match-patch: optional: true @@ -7366,7 +7366,7 @@ packages: magic-string: 0.26.2 svelte: 3.49.0 svelte-hmr: 0.14.12_svelte@3.49.0 - vite: 2.9.14 + vite: 3.0.0 transitivePeerDependencies: - supports-color dev: false @@ -14998,6 +14998,33 @@ packages: fsevents: 2.3.2 dev: false + /vite/3.0.0: + resolution: {integrity: sha512-M7phQhY3+fRZa0H+1WzI6N+/onruwPTBTMvaj7TzgZ0v2TE+N2sdLKxJOfOv9CckDWt5C4HmyQP81xB4dwRKzA==} + engines: {node: '>=14.18.0'} + hasBin: true + peerDependencies: + less: '*' + sass: '*' + stylus: '*' + terser: ^5.4.0 + peerDependenciesMeta: + less: + optional: true + sass: + optional: true + stylus: + optional: true + terser: + optional: true + dependencies: + esbuild: 0.14.48 + postcss: 8.4.14 + resolve: 1.22.1 + rollup: 2.76.0 + optionalDependencies: + fsevents: 2.3.2 + dev: false + /vite/3.0.0_sass@1.53.0: resolution: {integrity: sha512-M7phQhY3+fRZa0H+1WzI6N+/onruwPTBTMvaj7TzgZ0v2TE+N2sdLKxJOfOv9CckDWt5C4HmyQP81xB4dwRKzA==} engines: {node: '>=14.18.0'} From 0b1af9d179a215af883400ef8309c15c3afb112a Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 13 Jul 2022 11:09:24 -0400 Subject: [PATCH 33/46] deps: bump vue to vite 3 --- packages/integrations/vue/package.json | 4 +-- pnpm-lock.yaml | 42 ++++++-------------------- 2 files changed, 11 insertions(+), 35 deletions(-) diff --git a/packages/integrations/vue/package.json b/packages/integrations/vue/package.json index 4bb1346bbe28..24459acb1a18 100644 --- a/packages/integrations/vue/package.json +++ b/packages/integrations/vue/package.json @@ -33,8 +33,8 @@ "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { - "@vitejs/plugin-vue": "^2.3.3", - "vite": "^2.9.10" + "@vitejs/plugin-vue": "^3.0.0", + "vite": "^3.0.0" }, "devDependencies": { "astro": "workspace:*", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b7c4822a278d..440c5af8fe5a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2324,14 +2324,14 @@ importers: packages/integrations/vue: specifiers: - '@vitejs/plugin-vue': ^2.3.3 + '@vitejs/plugin-vue': ^3.0.0 astro: workspace:* astro-scripts: workspace:* - vite: ^2.9.10 + vite: ^3.0.0 vue: ^3.2.37 dependencies: - '@vitejs/plugin-vue': 2.3.3_vite@2.9.14+vue@3.2.37 - vite: 2.9.14 + '@vitejs/plugin-vue': 3.0.0_vite@3.0.0+vue@3.2.37 + vite: 3.0.0 devDependencies: astro: link:../../astro astro-scripts: link:../../../scripts @@ -7952,17 +7952,17 @@ packages: - supports-color dev: false - /@vitejs/plugin-vue/2.3.3_vite@2.9.14+vue@3.2.37: - resolution: {integrity: sha512-SmQLDyhz+6lGJhPELsBdzXGc+AcaT8stgkbiTFGpXPe8Tl1tJaBw1A6pxDqDuRsVkD8uscrkx3hA7QDOoKYtyw==} - engines: {node: '>=12.0.0'} + /@vitejs/plugin-vue/3.0.0_vite@3.0.0+vue@3.2.37: + resolution: {integrity: sha512-yWP34ArFh/jAeNUDkkLz/kVRLjf5ppJiq4L36f64Cp6dIrMQeYZGDP9xxdemlXfZR9ylN9JgHUl3GzfqOtgYDg==} + engines: {node: '>=14.18.0'} peerDependencies: - vite: ^2.5.10 + vite: ^3.0.0 vue: ^3.2.25 peerDependenciesMeta: vite: optional: true dependencies: - vite: 2.9.14 + vite: 3.0.0 vue: 3.2.37 dev: false @@ -14974,30 +14974,6 @@ packages: - supports-color dev: true - /vite/2.9.14: - resolution: {integrity: sha512-P/UCjSpSMcE54r4mPak55hWAZPlyfS369svib/gpmz8/01L822lMPOJ/RYW6tLCe1RPvMvOsJ17erf55bKp4Hw==} - engines: {node: '>=12.2.0'} - hasBin: true - peerDependencies: - less: '*' - sass: '*' - stylus: '*' - peerDependenciesMeta: - less: - optional: true - sass: - optional: true - stylus: - optional: true - dependencies: - esbuild: 0.14.48 - postcss: 8.4.14 - resolve: 1.22.1 - rollup: 2.76.0 - optionalDependencies: - fsevents: 2.3.2 - dev: false - /vite/3.0.0: resolution: {integrity: sha512-M7phQhY3+fRZa0H+1WzI6N+/onruwPTBTMvaj7TzgZ0v2TE+N2sdLKxJOfOv9CckDWt5C4HmyQP81xB4dwRKzA==} engines: {node: '>=14.18.0'} From 8d7301f2c2be8cc1f8799b25e83ea6001ea88cde Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 13 Jul 2022 12:09:59 -0400 Subject: [PATCH 34/46] fix: resolve plugins for proper sorting --- packages/astro/src/core/create-vite.ts | 34 ++++++++++++-------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 5ac0db3c3a71..04add3d326f2 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -126,30 +126,26 @@ export async function createVite( let result = commonConfig; result = vite.mergeConfig(result, astroConfig.vite || {}); result = vite.mergeConfig(result, commandConfig); - sortPlugins(result); - return result; -} + // Resolve plugin config to remove nulls, undefined's, nested arrays, promises, + // and anything else Vite supports in the future! + const { plugins: resolvedPlugins } = await vite.resolveConfig(result, mode === 'dev' ? 'serve' : 'build'); + result.plugins = sortPlugins(resolvedPlugins); -function getPluginName(plugin: vite.Plugin) { - 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')) + return result; } -function sortPlugins(result: ViteConfigWithSSR) { - const plugins = result.plugins?.filter(isVitePlugin) ?? [] +function sortPlugins(plugins: readonly vite.Plugin[]): vite.Plugin[] { + const result = [...plugins]; // HACK: move mdxPlugin to top because it needs to run before internal JSX plugin const mdxPluginIndex = - plugins.findIndex((plugin) => getPluginName(plugin) === '@mdx-js/rollup') ?? -1; - if (mdxPluginIndex === -1) return; + plugins.findIndex((plugin) => plugin.name === '@mdx-js/rollup') ?? -1; + if (mdxPluginIndex === -1) return result; const jsxPluginIndex = - plugins.findIndex((plugin) => getPluginName(plugin) === 'astro:jsx') ?? -1; - const mdxPlugin = result.plugins?.[mdxPluginIndex]; - result.plugins?.splice(mdxPluginIndex, 1); - result.plugins?.splice(jsxPluginIndex, 0, mdxPlugin); + plugins.findIndex((plugin) => plugin.name === 'astro:jsx') ?? -1; + + const mdxPlugin = plugins[mdxPluginIndex]; + result.splice(mdxPluginIndex, 1); + result.splice(jsxPluginIndex, 0, mdxPlugin); + return result; } From e1a6f5c48d949d713de92c4d5400aa6c02209c51 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 14 Jul 2022 13:25:06 -0400 Subject: [PATCH 35/46] sad fix: regex "export default" out of CSS ssr --- packages/astro/src/core/render/dev/css.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/core/render/dev/css.ts b/packages/astro/src/core/render/dev/css.ts index 657289d36cba..8310b58dd344 100644 --- a/packages/astro/src/core/render/dev/css.ts +++ b/packages/astro/src/core/render/dev/css.ts @@ -70,8 +70,11 @@ export async function getStylesForURL( if ( mode === 'development' && // only inline in development typeof importedModule.ssrModule?.default === 'string' // ignore JS module styles - ) { - importedStylesMap.set(importedModule.url, importedModule.ssrModule.default); + ) { + const styles = importedModule.ssrModule?.default + .replace(/^export default "/, '') + .replace(/"$/, ''); + importedStylesMap.set(importedModule.url, styles); } else { // NOTE: We use the `url` property here. `id` would break Windows. importedCssUrls.add(importedModule.url); From 5863fdf268fc229542e360223e171b924cf1c230 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 14 Jul 2022 13:33:20 -0400 Subject: [PATCH 36/46] chore: add TODO to understand sad fix --- packages/astro/src/core/render/dev/css.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/astro/src/core/render/dev/css.ts b/packages/astro/src/core/render/dev/css.ts index 8310b58dd344..8ac2afaad016 100644 --- a/packages/astro/src/core/render/dev/css.ts +++ b/packages/astro/src/core/render/dev/css.ts @@ -71,6 +71,8 @@ export async function getStylesForURL( mode === 'development' && // only inline in development typeof importedModule.ssrModule?.default === 'string' // ignore JS module styles ) { + // TODO: understand where this export default wrapper is coming from + // Note: does not occur when SSR-ing styles in plain Vite 3 const styles = importedModule.ssrModule?.default .replace(/^export default "/, '') .replace(/"$/, ''); From 7cc577eefcefb716c726aece95036a4b342dc7a5 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 15 Jul 2022 16:25:09 -0400 Subject: [PATCH 37/46] Revert "fix: resolve plugins for proper sorting" This reverts commit e67c194d3a8e11070487ed325947e7c59e8d69cd. --- packages/astro/src/core/create-vite.ts | 34 ++++++++++++++------------ 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 04add3d326f2..5ac0db3c3a71 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -126,26 +126,30 @@ export async function createVite( let result = commonConfig; result = vite.mergeConfig(result, astroConfig.vite || {}); result = vite.mergeConfig(result, commandConfig); - - // Resolve plugin config to remove nulls, undefined's, nested arrays, promises, - // and anything else Vite supports in the future! - const { plugins: resolvedPlugins } = await vite.resolveConfig(result, mode === 'dev' ? 'serve' : 'build'); - result.plugins = sortPlugins(resolvedPlugins); + sortPlugins(result); return result; } -function sortPlugins(plugins: readonly vite.Plugin[]): vite.Plugin[] { - const result = [...plugins]; +function getPluginName(plugin: vite.Plugin) { + 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 sortPlugins(result: ViteConfigWithSSR) { + const plugins = result.plugins?.filter(isVitePlugin) ?? [] // HACK: move mdxPlugin to top because it needs to run before internal JSX plugin const mdxPluginIndex = - plugins.findIndex((plugin) => plugin.name === '@mdx-js/rollup') ?? -1; - if (mdxPluginIndex === -1) return result; + plugins.findIndex((plugin) => getPluginName(plugin) === '@mdx-js/rollup') ?? -1; + if (mdxPluginIndex === -1) return; const jsxPluginIndex = - plugins.findIndex((plugin) => plugin.name === 'astro:jsx') ?? -1; - - const mdxPlugin = plugins[mdxPluginIndex]; - result.splice(mdxPluginIndex, 1); - result.splice(jsxPluginIndex, 0, mdxPlugin); - return 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); } From f417191c25794871102b2bc9904a3ee3d4b76506 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 15 Jul 2022 17:08:25 -0400 Subject: [PATCH 38/46] Revert "sad fix: regex "export default" out of CSS ssr" This reverts commit 721d40b62b61440dc9e488787901c915579659db. --- packages/astro/src/core/render/dev/css.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/packages/astro/src/core/render/dev/css.ts b/packages/astro/src/core/render/dev/css.ts index 8ac2afaad016..657289d36cba 100644 --- a/packages/astro/src/core/render/dev/css.ts +++ b/packages/astro/src/core/render/dev/css.ts @@ -70,13 +70,8 @@ export async function getStylesForURL( if ( mode === 'development' && // only inline in development typeof importedModule.ssrModule?.default === 'string' // ignore JS module styles - ) { - // TODO: understand where this export default wrapper is coming from - // Note: does not occur when SSR-ing styles in plain Vite 3 - const styles = importedModule.ssrModule?.default - .replace(/^export default "/, '') - .replace(/"$/, ''); - importedStylesMap.set(importedModule.url, styles); + ) { + importedStylesMap.set(importedModule.url, importedModule.ssrModule.default); } else { // NOTE: We use the `url` property here. `id` would break Windows. importedCssUrls.add(importedModule.url); From 97920e9c65d820f0c51bdd6b07824779f07ce91a Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 15 Jul 2022 17:10:04 -0400 Subject: [PATCH 39/46] fix: sort plugins WITHOUT resolveConfig --- packages/astro/src/core/create-vite.ts | 33 +++++++++++++------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 5ac0db3c3a71..0b6cc2dafbd1 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -126,30 +126,31 @@ 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.Plugin) { - 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 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) { - const plugins = result.plugins?.filter(isVitePlugin) ?? [] +function sortPlugins(pluginOptions: vite.PluginOption[]) { // HACK: move mdxPlugin to top because it needs to run before internal JSX plugin - const mdxPluginIndex = - plugins.findIndex((plugin) => getPluginName(plugin) === '@mdx-js/rollup') ?? -1; + const mdxPluginIndex = findPluginIndexByName(pluginOptions, '@mdx-js/rollup'); if (mdxPluginIndex === -1) return; - const jsxPluginIndex = - 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); } From e626a10285fcbbc76b660419d4b85b1bfafc4ad2 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 15 Jul 2022 17:31:44 -0400 Subject: [PATCH 40/46] Revert "wip: ignore with-mdx starter" This reverts commit 7d4f7338e6a8427934668096dba35e2a06ab961f. --- examples/with-mdx/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/with-mdx/package.json b/examples/with-mdx/package.json index c8ca36682bbc..79ca61952705 100644 --- a/examples/with-mdx/package.json +++ b/examples/with-mdx/package.json @@ -1,5 +1,5 @@ { - "name": "@example-ignore/with-mdx", + "name": "@example/with-mdx", "version": "0.0.1", "private": true, "scripts": { From 147c9d7c4f19eac976cb4d761884ca84a4d2ea53 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 15 Jul 2022 17:34:42 -0400 Subject: [PATCH 41/46] chore: revert memory test changes --- scripts/memory/mk.js | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/scripts/memory/mk.js b/scripts/memory/mk.js index 44cbe52778f7..ae14e1191587 100644 --- a/scripts/memory/mk.js +++ b/scripts/memory/mk.js @@ -1,16 +1,6 @@ import fs from 'fs'; -const base = new URL('./project/', import.meta.url); -const pages = new URL('./src/pages/', base); - -await fs.promises.writeFile(new URL('./package.json', base), `{ - "name": "@test/smoke", - "version": "0.0.0", - "private": true, - "dependencies": { - "astro": "workspace:*" - } -}`); +const pages = new URL('./project/src/pages/', import.meta.url); for (let i = 0; i < 100; i++) { let content = `--- From 6fd11feea1be7a9dcb9dbf3d47d7f889adbc29aa Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 15 Jul 2022 17:36:31 -0400 Subject: [PATCH 42/46] chore: add nanostores/preact ot noexternal --- packages/astro/src/core/create-vite.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 0b6cc2dafbd1..67883a508055 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -30,6 +30,9 @@ const ALWAYS_NOEXTERNAL = new Set([ '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', ]); function getSsrNoExternalDeps(projectRoot: URL): string[] { From 561d4e8db0af30851de3307e47aeae66ffc39abd Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 15 Jul 2022 18:00:44 -0400 Subject: [PATCH 43/46] chore: changeset --- .changeset/thirty-beans-poke.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .changeset/thirty-beans-poke.md diff --git a/.changeset/thirty-beans-poke.md b/.changeset/thirty-beans-poke.md new file mode 100644 index 000000000000..2b1ae5c987b8 --- /dev/null +++ b/.changeset/thirty-beans-poke.md @@ -0,0 +1,8 @@ +--- +'astro': minor +'@astrojs/image': minor +'@astrojs/svelte': minor +'@astrojs/vue': minor +--- + +Bump to Vite 3! From c7278e63f218d0e898519aa8b638ef97f014d281 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 15 Jul 2022 18:34:23 -0400 Subject: [PATCH 44/46] chore: changeset --- packages/astro/test/test-utils.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/astro/test/test-utils.js b/packages/astro/test/test-utils.js index a24571fe5512..346c2a2acd66 100644 --- a/packages/astro/test/test-utils.js +++ b/packages/astro/test/test-utils.js @@ -130,12 +130,7 @@ export async function loadFixture(inlineConfig) { let devServer; return { - build: async (opts = {}) => { - // TODO: remove once Vite resolves this internally - const initialNodeEnv = process.env.NODE_ENV; - await build(config, { logging, telemetry, ...opts }) - process.env.NODE_ENV = initialNodeEnv; - }, + build: (opts = {}) => build(config, { logging, telemetry, ...opts }), startDevServer: async (opts = {}) => { devServer = await dev(config, { logging, telemetry, ...opts }); config.server.port = devServer.address.port; // update port From 339f1509bf94b2024d211eb0c0a920caa59adf4b Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 15 Jul 2022 18:39:46 -0400 Subject: [PATCH 45/46] deps: use Vite ^3.0.0 --- packages/astro/package.json | 2 +- pnpm-lock.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/astro/package.json b/packages/astro/package.json index ba25dbbcb587..e8686f0d5a1b 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -136,7 +136,7 @@ "strip-ansi": "^7.0.1", "supports-esm": "^1.0.0", "tsconfig-resolver": "^3.0.1", - "vite": "3.0.0", + "vite": "^3.0.0", "yargs-parser": "^21.0.1", "zod": "^3.17.3" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 440c5af8fe5a..abf6f7789272 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -546,7 +546,7 @@ importers: strip-ansi: ^7.0.1 supports-esm: ^1.0.0 tsconfig-resolver: ^3.0.1 - vite: 3.0.0 + vite: ^3.0.0 yargs-parser: ^21.0.1 zod: ^3.17.3 dependencies: From 247494e1390b6f89b952bc06d8d10b166cc2cd8d Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 15 Jul 2022 18:47:34 -0400 Subject: [PATCH 46/46] fix: add back third party astro pkg scanner --- packages/astro/src/core/create-vite.ts | 93 +++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 67883a508055..27badc1c7914 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -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'; @@ -54,6 +53,7 @@ export async function createVite( commandConfig: ViteConfigWithSSR, { astroConfig, logging, mode }: CreateViteOptions ): Promise { + 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. @@ -116,7 +116,10 @@ export async function createVite( conditions: ['astro'], }, ssr: { - noExternal: getSsrNoExternalDeps(astroConfig.root), + noExternal: [ + ...getSsrNoExternalDeps(astroConfig.root), + ...thirdPartyAstroPackages, + ], } }; @@ -157,3 +160,89 @@ function sortPlugins(pluginOptions: vite.PluginOption[]) { pluginOptions.splice(mdxPluginIndex, 1); pluginOptions.splice(jsxPluginIndex, 0, mdxPlugin); } + +// Scans `projectRoot` for third-party Astro packages that could export an `.astro` file +// `.astro` files need to be built by Vite, so these should use `noExternal` +async function getAstroPackages({ root }: AstroConfig): Promise { + const pkgUrl = new URL('./package.json', root); + const pkgPath = fileURLToPath(pkgUrl); + if (!fs.existsSync(pkgPath)) return []; + + const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8')); + + const deps = [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.devDependencies || {})]; + + return deps.filter((dep) => { + // Attempt: package is common and not Astro. ❌ Skip these for perf + if (isCommonNotAstro(dep)) return false; + // Attempt: package is named `astro-something`. ✅ Likely a community package + if (/^astro\-/.test(dep)) return true; + const depPkgUrl = new URL(`./node_modules/${dep}/package.json`, root); + const depPkgPath = fileURLToPath(depPkgUrl); + if (!fs.existsSync(depPkgPath)) return false; + + const { + dependencies = {}, + peerDependencies = {}, + keywords = [], + } = JSON.parse(fs.readFileSync(depPkgPath, 'utf-8')); + // Attempt: package relies on `astro`. ✅ Definitely an Astro package + if (peerDependencies.astro || dependencies.astro) return true; + // Attempt: package is tagged with `astro` or `astro-component`. ✅ Likely a community package + if (keywords.includes('astro') || keywords.includes('astro-component')) return true; + return false; + }); +} + +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/ + ) + ); +}