Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Vite 3.1 #4752

Merged
merged 4 commits into from Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-books-give.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Support Vite 3.1
6 changes: 3 additions & 3 deletions packages/astro/src/vite-plugin-astro/index.ts
Expand Up @@ -340,17 +340,17 @@ ${source}
throw err;
}
},
async handleHotUpdate(this: PluginContext, context) {
async handleHotUpdate(context) {
if (context.server.config.isProduction) return;
const compileProps: CompileProps = {
config,
filename: context.file,
moduleId: context.file,
source: await context.read(),
transformStyle: createTransformStyles(styleTransformer, context.file, true, this),
transformStyle: createTransformStyles(styleTransformer, context.file, true),
};
const compile = () => cachedCompilation(compileProps);
return handleHotUpdate.call(this, context, {
return handleHotUpdate(context, {
config,
logging,
compile,
Expand Down
8 changes: 1 addition & 7 deletions packages/astro/src/vite-style-transform/style-transform.ts
Expand Up @@ -30,14 +30,8 @@ export function createTransformStyles(
viteStyleTransformer: ViteStyleTransformer,
filename: string,
ssr: boolean,
pluginContext: PluginContext
pluginContext?: PluginContext
): TransformStyle {
// handleHotUpdate doesn't have `addWatchFile` used by transformStyleWithVite.
// TODO, refactor, why is this happening *here* ?
if (!pluginContext.addWatchFile) {
pluginContext.addWatchFile = () => {};
}

const normalizedID = getNormalizedIDForPostCSS(filename);

return async function (styleSource, lang) {
Expand Down
Expand Up @@ -46,7 +46,10 @@ export function createTransformStyleWithViteFn(

viteDevServer?.moduleGraph.ensureEntryFromUrl(styleId, ssr, false);

const transformResult = await transformCss.call(this, source, styleId, ssr);
// This function could be called in a custom Vite hook like `handleHotUpdate`
// which doesn't have a context
const ctx = this ?? { addWatchFile: () => {} };
const transformResult = await transformCss.call(ctx, source, styleId, ssr);

// NOTE: only `code` and `map` are returned by vite:css
const { code, map } = transformResult;
Expand Down