Skip to content

Commit

Permalink
vite: Add experimental support for Astro (#796)
Browse files Browse the repository at this point in the history
Co-authored-by: mattcompiles <[email protected]>
  • Loading branch information
mechairoi and mattcompiles authored Sep 9, 2022
1 parent d115695 commit ee83020
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-poets-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/vite-plugin': minor
---

Add experimental support for [Astro](https://astro.build/)
21 changes: 15 additions & 6 deletions packages/vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import { PostCSSConfigResult, resolvePostcssConfig } from './postcss';
const styleUpdateEvent = (fileId: string) =>
`vanilla-extract-style-update:${fileId}`;

const virtualExtCss = '.vanilla.css';
const virtualExtJs = '.vanilla.js';

interface Options {
identifiers?: IdentifierOption;
esbuildOptions?: CompileOptions['esbuildOptions'];
Expand All @@ -30,7 +33,7 @@ export function vanillaExtractPlugin({
let postCssConfig: PostCSSConfigResult | null;
const cssMap = new Map<string, string>();

let virtualExt: string;
let forceEmitCssInSsrBuild: boolean = !!process.env.VITE_RSC_BUILD;
let packageName: string;

const getAbsoluteVirtualFileId = (source: string) =>
Expand Down Expand Up @@ -65,11 +68,13 @@ export function vanillaExtractPlugin({
postCssConfig = await resolvePostcssConfig(config);
}

virtualExt = `.vanilla.${config.command === 'serve' ? 'js' : 'css'}`;
if (config.plugins.some((p) => p.name === 'astro:build')) {
forceEmitCssInSsrBuild = true;
}
},
resolveId(source) {
const [validId, query] = source.split('?');
if (!validId.endsWith(virtualExt)) {
if (!validId.endsWith(virtualExtCss) && !validId.endsWith(virtualExtJs)) {
return;
}

Expand Down Expand Up @@ -100,7 +105,7 @@ export function vanillaExtractPlugin({
return;
}

if (!server || server.config.isProduction) {
if (validId.endsWith(virtualExtCss)) {
return css;
}

Expand Down Expand Up @@ -136,7 +141,7 @@ export function vanillaExtractPlugin({
ssr = ssrParam?.ssr;
}

if (ssr && !process.env.VITE_RSC_BUILD) {
if (ssr && !forceEmitCssInSsrBuild) {
return addFileScope({
source: code,
filePath: normalizePath(validId),
Expand Down Expand Up @@ -165,7 +170,11 @@ export function vanillaExtractPlugin({
identOption:
identifiers ?? (config.mode === 'production' ? 'short' : 'debug'),
serializeVirtualCssPath: async ({ fileScope, source }) => {
const rootRelativeId = `${fileScope.filePath}${virtualExt}`;
const rootRelativeId = `${fileScope.filePath}${
config.command === 'build' || (ssr && forceEmitCssInSsrBuild)
? virtualExtCss
: virtualExtJs
}`;
const absoluteId = getAbsoluteVirtualFileId(rootRelativeId);

let cssSource = source;
Expand Down

0 comments on commit ee83020

Please sign in to comment.