Allow users to pass config to Vite

This commit is contained in:
Drew Powers 2021-09-21 12:28:23 -06:00 committed by Matthew Phillips
parent 8126686878
commit 91cb38f451
10 changed files with 20 additions and 15 deletions

View file

@ -0,0 +1,5 @@
---
'astro': minor
---
Use new compiler, move Astro to Vite

View file

@ -22,7 +22,8 @@ jobs:
- os: windows-latest - os: windows-latest
node_version: 14 node_version: 14
fail-fast: false fail-fast: false
env:
LANG: en-us
name: 'Test: node-${{ matrix.node_version }}, ${{ matrix.os }}' name: 'Test: node-${{ matrix.node_version }}, ${{ matrix.os }}'
steps: steps:
- name: Checkout - name: Checkout

View file

@ -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.

View file

@ -91,6 +91,8 @@ export interface AstroUserConfig {
*/ */
trailingSlash?: 'always' | 'never' | 'ignore'; 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 // NOTE(fks): We choose to keep our hand-generated AstroUserConfig interface so that

View file

@ -62,6 +62,7 @@ class AstroBuilder {
hmr: { overlay: false }, hmr: { overlay: false },
middlewareMode: 'ssr', middlewareMode: 'ssr',
}, },
...(this.config.vite || {}),
}, },
{ astroConfig: this.config, logging } { astroConfig: this.config, logging }
); );

View file

@ -66,6 +66,7 @@ export const AstroConfigSchema = z.object({
}) })
.optional() .optional()
.default({}), .default({}),
vite: z.any().optional().default({}), // TODO: we dont need validation, but can we get better type inference?
}); });
/** Turn raw config values into normalized values */ /** Turn raw config values into normalized values */

View file

@ -174,6 +174,7 @@ export class AstroDevServer {
middlewareMode: 'ssr', middlewareMode: 'ssr',
host: this.hostname, host: this.hostname,
}, },
...(this.config.vite || {}),
}, },
{ astroConfig: this.config, logging: this.logging, devServer: this } { astroConfig: this.config, logging: this.logging, devServer: this }
); );

View file

@ -1,12 +1,11 @@
import type { InlineConfig, Plugin } from 'vite';
import type { AstroConfig } from '../../@types/astro'; import type { AstroConfig } from '../../@types/astro';
import type { LogOptions } from '../../logger'; import type { LogOptions } from '../../logger';
import fs from 'fs'; import fs from 'fs';
import slash from 'slash'; import slash from 'slash';
import deepmerge from 'deepmerge';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { createRequire } from 'module'; import { createRequire } from 'module';
import vite from 'vite';
import { getPackageJSON, parseNpmName } from '../util.js'; import { getPackageJSON, parseNpmName } from '../util.js';
import astro from './plugin-astro.js'; import astro from './plugin-astro.js';
import markdown from './plugin-markdown.js'; import markdown from './plugin-markdown.js';
@ -16,7 +15,7 @@ import { AstroDevServer } from '../../dev';
const require = createRequire(import.meta.url); const require = createRequire(import.meta.url);
// note: ssr is still an experimental API hence the type omission // 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 */ /** Return a common starting point for all Vite actions */
export async function loadViteConfig( export async function loadViteConfig(
@ -70,7 +69,7 @@ export async function loadViteConfig(
optimizedDeps.add(`astro/client/${hydrator}`); // always prepare these for client 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. cacheDir: fileURLToPath(new URL('./node_modules/.vite/', astroConfig.projectRoot)), // using local caches allows Astro to be used in monorepos, etc.
clearScreen: false, clearScreen: false,

View file

@ -1,7 +1,6 @@
import type { TransformResult } from '@astrojs/compiler'; import type { TransformResult } from '@astrojs/compiler';
import type { Plugin } from 'vite'; import type { Plugin } from 'vite';
import type { AstroConfig, Renderer } from '../../@types/astro.js'; import type { AstroConfig, Renderer } from '../../@types/astro.js';
import type { TransformResult } from '@astrojs/compiler';
import esbuild from 'esbuild'; import esbuild from 'esbuild';
import fs from 'fs'; import fs from 'fs';

View file

@ -87,10 +87,10 @@ describe('Astro basics', () => {
const result = await fixture.fetch('/bad-url'); const result = await fixture.fetch('/bad-url');
expect(result.status).toBe(404); 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();
});