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

fix: don't mix Vite plugins when spawning temporary Vite server #6368

Merged
merged 11 commits into from
Feb 27, 2023
5 changes: 5 additions & 0 deletions .changeset/green-wombats-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix regression that caused some stateful Vite plugins to assume they were running in `dev` mode during the `build` and vice versa: also excluding any plugin in preview.
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class AstroBuilder {
middlewareMode: true,
},
},
{ settings: this.settings, logging, mode: 'build' }
{ settings: this.settings, logging, mode: 'build', command: 'build' }
);
await runHookConfigDone({ settings: this.settings, logging });

Expand Down
30 changes: 28 additions & 2 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface CreateViteOptions {
settings: AstroSettings;
logging: LogOptions;
mode: 'dev' | 'build' | string;
// will be undefined when using `sync`
command?: 'dev' | 'build' | 'preview';
userquin marked this conversation as resolved.
Show resolved Hide resolved
fs?: typeof nodeFs;
}

Expand All @@ -48,7 +50,7 @@ const ALWAYS_NOEXTERNAL = [
/** Return a common starting point for all Vite actions */
export async function createVite(
commandConfig: vite.InlineConfig,
{ settings, logging, mode, fs = nodeFs }: CreateViteOptions
{ settings, logging, mode, command, fs = nodeFs }: CreateViteOptions
): Promise<vite.InlineConfig> {
const astroPkgsConfig = await crawlFrameworkPkgs({
root: fileURLToPath(settings.config.root),
Expand Down Expand Up @@ -170,7 +172,31 @@ export async function createVite(
// 3. integration-provided vite config, via the `config:setup` hook
// 4. command vite config, passed as the argument to this function
let result = commonConfig;
result = vite.mergeConfig(result, settings.config.vite || {});
// command will be undefined when running sync
if (command && command !== 'preview') {
userquin marked this conversation as resolved.
Show resolved Hide resolved
let plugins = settings.config.vite?.plugins;
if (plugins) {
const { plugins: _, ...rest } = settings.config.vite
const applyToFilter = mode === 'build' ? 'serve' : 'build'
userquin marked this conversation as resolved.
Show resolved Hide resolved
const applyArgs = [{...settings.config.vite, mode}, { command, mode }]
// @ts-ignore we know what are doing
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
plugins = plugins.flat(Infinity).filter((p) => {
if (!p || p?.apply === applyToFilter) {
return false;
}

if (typeof p.apply === 'function') {
return p.apply(applyArgs[0], applyArgs[1])
}

return true;
});

result = vite.mergeConfig(result, { ...rest, plugins });
} else {
result = vite.mergeConfig(result, settings.config.vite || {});
}
}
Copy link
Contributor Author

@userquin userquin Feb 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add else case without plugins?

result = vite.mergeConfig(result, commandConfig);
if (result.plugins) {
sortPlugins(result.plugins);
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/dev/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export async function createContainer(params: CreateContainerParams = {}): Promi
: 'undefined',
},
},
{ settings, logging, mode: 'dev', fs }
{ settings, logging, mode: 'dev', command: 'dev', fs }
);
await runHookConfigDone({ settings, logging });
const viteServer = await vite.createServer(viteConfig);
Expand Down
24 changes: 12 additions & 12 deletions packages/webapi/mod.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// organize-imports-ignore
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
export { pathToPosix } from './lib/utils';
export { alert, ByteLengthQueuingStrategy, cancelAnimationFrame, cancelIdleCallback, CanvasRenderingContext2D, CharacterData, clearTimeout, Comment, CountQueuingStrategy, CSSStyleSheet, CustomElementRegistry, CustomEvent, Document, DocumentFragment, DOMException, Element, Event, EventTarget, fetch, File, FormData, Headers, HTMLBodyElement, HTMLCanvasElement, HTMLDivElement, HTMLDocument, HTMLElement, HTMLHeadElement, HTMLHtmlElement, HTMLImageElement, HTMLSpanElement, HTMLStyleElement, HTMLTemplateElement, HTMLUnknownElement, Image, ImageData, IntersectionObserver, MediaQueryList, MutationObserver, Node, NodeFilter, NodeIterator, OffscreenCanvas, ReadableByteStreamController, ReadableStream, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableStreamDefaultController, ReadableStreamDefaultReader, Request, requestAnimationFrame, requestIdleCallback, ResizeObserver, Response, setTimeout, ShadowRoot, structuredClone, StyleSheet, Text, TransformStream, TreeWalker, URLPattern, Window, WritableStream, WritableStreamDefaultController, WritableStreamDefaultWriter, } from './mod.js';
export declare const polyfill: {
(target: any, options?: PolyfillOptions): any;
internals(target: any, name: string): any;
};
interface PolyfillOptions {
exclude?: string | string[];
override?: Record<string, {
(...args: any[]): any;
}>;
}
export { pathToPosix } from './lib/utils';
export { alert, ByteLengthQueuingStrategy, cancelAnimationFrame, cancelIdleCallback, CanvasRenderingContext2D, CharacterData, clearTimeout, Comment, CountQueuingStrategy, CSSStyleSheet, CustomElementRegistry, CustomEvent, Document, DocumentFragment, DOMException, Element, Event, EventTarget, fetch, File, FormData, Headers, HTMLBodyElement, HTMLCanvasElement, HTMLDivElement, HTMLDocument, HTMLElement, HTMLHeadElement, HTMLHtmlElement, HTMLImageElement, HTMLSpanElement, HTMLStyleElement, HTMLTemplateElement, HTMLUnknownElement, Image, ImageData, IntersectionObserver, MediaQueryList, MutationObserver, Node, NodeFilter, NodeIterator, OffscreenCanvas, ReadableByteStreamController, ReadableStream, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableStreamDefaultController, ReadableStreamDefaultReader, Request, requestAnimationFrame, requestIdleCallback, ResizeObserver, Response, setTimeout, ShadowRoot, structuredClone, StyleSheet, Text, TransformStream, TreeWalker, URLPattern, Window, WritableStream, WritableStreamDefaultController, WritableStreamDefaultWriter, } from './mod.js';
export declare const polyfill: {
(target: any, options?: PolyfillOptions): any;
internals(target: any, name: string): any;
};
interface PolyfillOptions {
exclude?: string | string[];
override?: Record<string, {
(...args: any[]): any;
}>;
}