From 1df888ce42e027f7da386fd1a217e14346d8c6a7 Mon Sep 17 00:00:00 2001 From: Drew Powers Date: Tue, 21 Sep 2021 12:28:23 -0600 Subject: [PATCH] Allow users to pass config to Vite --- .changeset/pink-trainers-learn.md | 5 +++++ .github/workflows/ci.yml | 3 ++- docs/src/pages/reference/configuration-reference.md | 4 ---- packages/astro/src/@types/astro.ts | 2 ++ packages/astro/src/build/index.ts | 1 + packages/astro/src/config.ts | 1 + packages/astro/src/dev/index.ts | 1 + packages/astro/src/runtime/vite/config.ts | 7 +++---- packages/astro/src/runtime/vite/plugin-astro.ts | 1 - packages/astro/test/astro-basic.test.js | 10 +++++----- 10 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 .changeset/pink-trainers-learn.md diff --git a/.changeset/pink-trainers-learn.md b/.changeset/pink-trainers-learn.md new file mode 100644 index 000000000000..166d42de9535 --- /dev/null +++ b/.changeset/pink-trainers-learn.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Use new compiler, move Astro to Vite diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 44776cdc2f4d..c44a5252d1c9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,8 @@ jobs: - os: windows-latest node_version: 14 fail-fast: false - + env: + LANG: en-us name: 'Test: node-${{ matrix.node_version }}, ${{ matrix.os }}' steps: - name: Checkout diff --git a/docs/src/pages/reference/configuration-reference.md b/docs/src/pages/reference/configuration-reference.md index c181c0ea2dcd..071179b59b3f 100644 --- a/docs/src/pages/reference/configuration-reference.md +++ b/docs/src/pages/reference/configuration-reference.md @@ -22,7 +22,3 @@ export default /** @type {import('astro').AstroUserConfig} */ ( } ); ``` - -## Snowpack Config - -Astro is powered internally by Snowpack. You can configure Snowpack directly by creating a `snowpack.config.mjs` file. See [snowpack.dev](https://www.snowpack.dev/reference/configuration) for full documentation on this file. diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 8ac4fb4153cc..506870dd8711 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -91,6 +91,8 @@ export interface AstroUserConfig { */ trailingSlash?: 'always' | 'never' | 'ignore'; }; + /** Pass configuration options to Vite */ + vite?: vite.InlineConfig; } // NOTE(fks): We choose to keep our hand-generated AstroUserConfig interface so that diff --git a/packages/astro/src/build/index.ts b/packages/astro/src/build/index.ts index 541286997a42..d9732befc652 100644 --- a/packages/astro/src/build/index.ts +++ b/packages/astro/src/build/index.ts @@ -62,6 +62,7 @@ class AstroBuilder { hmr: { overlay: false }, middlewareMode: 'ssr', }, + ...(this.config.vite || {}), }, { astroConfig: this.config, logging } ); diff --git a/packages/astro/src/config.ts b/packages/astro/src/config.ts index fe007dea9661..6592cdef4a59 100644 --- a/packages/astro/src/config.ts +++ b/packages/astro/src/config.ts @@ -66,6 +66,7 @@ export const AstroConfigSchema = z.object({ }) .optional() .default({}), + vite: z.any().optional().default({}), // TODO: we don’t need validation, but can we get better type inference? }); /** Turn raw config values into normalized values */ diff --git a/packages/astro/src/dev/index.ts b/packages/astro/src/dev/index.ts index 95ff6b305a42..257a5dad73ad 100644 --- a/packages/astro/src/dev/index.ts +++ b/packages/astro/src/dev/index.ts @@ -174,6 +174,7 @@ export class AstroDevServer { middlewareMode: 'ssr', host: this.hostname, }, + ...(this.config.vite || {}), }, { astroConfig: this.config, logging: this.logging, devServer: this } ); diff --git a/packages/astro/src/runtime/vite/config.ts b/packages/astro/src/runtime/vite/config.ts index 46724e604382..7a025a3ca501 100644 --- a/packages/astro/src/runtime/vite/config.ts +++ b/packages/astro/src/runtime/vite/config.ts @@ -1,12 +1,11 @@ -import type { InlineConfig, Plugin } from 'vite'; import type { AstroConfig } from '../../@types/astro'; import type { LogOptions } from '../../logger'; import fs from 'fs'; import slash from 'slash'; -import deepmerge from 'deepmerge'; import { fileURLToPath } from 'url'; import { createRequire } from 'module'; +import vite from 'vite'; import { getPackageJSON, parseNpmName } from '../util.js'; import astro from './plugin-astro.js'; import markdown from './plugin-markdown.js'; @@ -16,7 +15,7 @@ import { AstroDevServer } from '../../dev'; const require = createRequire(import.meta.url); // note: ssr is still an experimental API hence the type omission -type ViteConfigWithSSR = InlineConfig & { ssr?: { external?: string[]; noExternal?: string[] } }; +type ViteConfigWithSSR = vite.InlineConfig & { ssr?: { external?: string[]; noExternal?: string[] } }; /** Return a common starting point for all Vite actions */ export async function loadViteConfig( @@ -70,7 +69,7 @@ export async function loadViteConfig( optimizedDeps.add(`astro/client/${hydrator}`); // always prepare these for client }); - return deepmerge( + return vite.mergeConfig( { cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.projectRoot)), // using local caches allows Astro to be used in monorepos, etc. clearScreen: false, diff --git a/packages/astro/src/runtime/vite/plugin-astro.ts b/packages/astro/src/runtime/vite/plugin-astro.ts index bc206480981f..c9cdcf2c087b 100644 --- a/packages/astro/src/runtime/vite/plugin-astro.ts +++ b/packages/astro/src/runtime/vite/plugin-astro.ts @@ -1,7 +1,6 @@ import type { TransformResult } from '@astrojs/compiler'; import type { Plugin } from 'vite'; import type { AstroConfig, Renderer } from '../../@types/astro.js'; -import type { TransformResult } from '@astrojs/compiler'; import esbuild from 'esbuild'; import fs from 'fs'; diff --git a/packages/astro/test/astro-basic.test.js b/packages/astro/test/astro-basic.test.js index 9359ea46db6e..1b38a48d54fe 100644 --- a/packages/astro/test/astro-basic.test.js +++ b/packages/astro/test/astro-basic.test.js @@ -87,10 +87,10 @@ describe('Astro basics', () => { const result = await fixture.fetch('/bad-url'); expect(result.status).toBe(404); }); - - // important: close preview server (free up port and connection) - afterAll(async () => { - await previewServer.stop(); - }); }); }); + +// important: close preview server (free up port and connection) +afterAll(async () => { + if (previewServer) await previewServer.stop(); +});