Skip to content

Commit

Permalink
Allow users to pass config to Vite
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Sep 21, 2021
1 parent cd8e959 commit 1df888c
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-trainers-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Use new compiler, move Astro to Vite
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions docs/src/pages/reference/configuration-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class AstroBuilder {
hmr: { overlay: false },
middlewareMode: 'ssr',
},
...(this.config.vite || {}),
},
{ astroConfig: this.config, logging }
);
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export class AstroDevServer {
middlewareMode: 'ssr',
host: this.hostname,
},
...(this.config.vite || {}),
},
{ astroConfig: this.config, logging: this.logging, devServer: this }
);
Expand Down
7 changes: 3 additions & 4 deletions packages/astro/src/runtime/vite/config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion packages/astro/src/runtime/vite/plugin-astro.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
10 changes: 5 additions & 5 deletions packages/astro/test/astro-basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 1df888c

Please sign in to comment.