diff --git a/packages/astro/src/core/compile/compile.ts b/packages/astro/src/core/compile/compile.ts index d1c5c2f4b..10411860e 100644 --- a/packages/astro/src/core/compile/compile.ts +++ b/packages/astro/src/core/compile/compile.ts @@ -5,7 +5,7 @@ import type { TransformStyle } from './types'; import { transform } from '@astrojs/compiler'; import { AstroErrorCodes } from '../errors.js'; import { prependForwardSlash } from '../path.js'; -import { viteID, AggregateError } from '../util.js'; +import { AggregateError, viteID } from '../util.js'; import { createStylePreprocessor } from './style.js'; type CompilationCache = Map; @@ -57,11 +57,12 @@ async function compile({ throw err; }) .then((result) => { - switch(cssTransformErrors.length) { - case 0: return result; + switch (cssTransformErrors.length) { + case 0: + return result; case 1: { let error = cssTransformErrors[0]; - if(!(error as any).code) { + if (!(error as any).code) { (error as any).code = AstroErrorCodes.UnknownCompilerCSSError; } throw cssTransformErrors[0]; diff --git a/packages/astro/src/core/compile/index.ts b/packages/astro/src/core/compile/index.ts index 7d3539c2e..1ee6af06a 100644 --- a/packages/astro/src/core/compile/index.ts +++ b/packages/astro/src/core/compile/index.ts @@ -1,13 +1,3 @@ -export type { - CompileProps -} from './compile'; -export type { - TransformStyle -} from './types'; - -export { - cachedCompilation, - invalidateCompilation, - isCached, - getCachedSource -} from './compile.js'; +export type { CompileProps } from './compile'; +export { cachedCompilation, getCachedSource, invalidateCompilation, isCached } from './compile.js'; +export type { TransformStyle } from './types'; diff --git a/packages/astro/src/core/compile/style.ts b/packages/astro/src/core/compile/style.ts index fde1e60bd..20079cede 100644 --- a/packages/astro/src/core/compile/style.ts +++ b/packages/astro/src/core/compile/style.ts @@ -1,10 +1,14 @@ import type { TransformOptions } from '@astrojs/compiler'; -import type { TransformStyle } from './types'; import type { SourceMapInput } from 'rollup'; +import type { TransformStyle } from './types'; type PreprocessStyle = TransformOptions['preprocessStyle']; -export function createStylePreprocessor(transformStyle: TransformStyle, cssDeps: Set, errors: Error[]): PreprocessStyle { +export function createStylePreprocessor( + transformStyle: TransformStyle, + cssDeps: Set, + errors: Error[] +): PreprocessStyle { const preprocessStyle: PreprocessStyle = async (value: string, attrs: Record) => { const lang = `.${attrs?.lang || 'css'}`.toLowerCase(); @@ -30,10 +34,10 @@ export function createStylePreprocessor(transformStyle: TransformStyle, cssDeps: } catch (err) { errors.push(err as unknown as Error); return { - error: err + '' + error: err + '', }; } }; return preprocessStyle; -}; +} diff --git a/packages/astro/src/core/compile/types.ts b/packages/astro/src/core/compile/types.ts index 0e7f79fb2..1ef4bdfdc 100644 --- a/packages/astro/src/core/compile/types.ts +++ b/packages/astro/src/core/compile/types.ts @@ -1,9 +1,12 @@ import type { SourceMap } from 'rollup'; -export type TransformStyleResult = null | { +export type TransformStyleResult = null | { code: string; map: SourceMap | null; deps: Set; -} +}; -export type TransformStyle = (source: string, lang: string) => TransformStyleResult | Promise; +export type TransformStyle = ( + source: string, + lang: string +) => TransformStyleResult | Promise; diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts index 3dc5a698e..7fb5a4f5b 100644 --- a/packages/astro/src/core/util.ts +++ b/packages/astro/src/core/util.ts @@ -227,10 +227,13 @@ export async function resolveIdToUrl(viteServer: ViteDevServer, id: string) { return VALID_ID_PREFIX + result.id; } -export const AggregateError = typeof globalThis.AggregateError !== 'undefined' ? globalThis.AggregateError : class extends Error { - errors: Array = []; - constructor( errors: Iterable, message?: string | undefined) { - super(message); - this.errors = Array.from(errors); - } -} +export const AggregateError = + typeof globalThis.AggregateError !== 'undefined' + ? globalThis.AggregateError + : class extends Error { + errors: Array = []; + constructor(errors: Iterable, message?: string | undefined) { + super(message); + this.errors = Array.from(errors); + } + }; diff --git a/packages/astro/src/vite-plugin-astro/hmr.ts b/packages/astro/src/vite-plugin-astro/hmr.ts index 9c729715d..330ae2e1a 100644 --- a/packages/astro/src/vite-plugin-astro/hmr.ts +++ b/packages/astro/src/vite-plugin-astro/hmr.ts @@ -1,10 +1,10 @@ import { fileURLToPath } from 'node:url'; import type { HmrContext, ModuleNode } from 'vite'; import type { AstroConfig } from '../@types/astro'; +import { cachedCompilation, invalidateCompilation, isCached } from '../core/compile/index.js'; import type { LogOptions } from '../core/logger/core.js'; import { info } from '../core/logger/core.js'; import * as msg from '../core/messages.js'; -import { cachedCompilation, invalidateCompilation, isCached } from '../core/compile/index.js'; import { isAstroScript } from './query.js'; const PKG_PREFIX = new URL('../../', import.meta.url); diff --git a/packages/astro/src/vite-plugin-astro/index.ts b/packages/astro/src/vite-plugin-astro/index.ts index f8b98a6a4..3b98b6bee 100644 --- a/packages/astro/src/vite-plugin-astro/index.ts +++ b/packages/astro/src/vite-plugin-astro/index.ts @@ -2,23 +2,22 @@ import type { PluginContext, SourceDescription } from 'rollup'; import type * as vite from 'vite'; import type { AstroConfig } from '../@types/astro'; import type { LogOptions } from '../core/logger/core.js'; -import type { PluginMetadata as AstroPluginMetadata } from './types'; import type { ViteStyleTransformer } from '../vite-style-transform'; +import type { PluginMetadata as AstroPluginMetadata } from './types'; import ancestor from 'common-ancestor-path'; import esbuild from 'esbuild'; import slash from 'slash'; import { fileURLToPath } from 'url'; +import { cachedCompilation, CompileProps, getCachedSource } from '../core/compile/index.js'; import { isRelativePath, startsWithForwardSlash } from '../core/path.js'; import { getFileInfo } from '../vite-plugin-utils/index.js'; import { - cachedCompilation, - CompileProps, - getCachedSource -} from '../core/compile/index.js'; + createTransformStyles, + createViteStyleTransformer, +} from '../vite-style-transform/index.js'; import { handleHotUpdate } from './hmr.js'; import { parseAstroRequest, ParsedRequestResult } from './query.js'; -import { createViteStyleTransformer, createTransformStyles } from '../vite-style-transform/index.js'; const FRONTMATTER_PARSE_REGEXP = /^\-\-\-(.*)^\-\-\-/ms; interface AstroPluginOptions { diff --git a/packages/astro/src/vite-plugin-markdown-legacy/index.ts b/packages/astro/src/vite-plugin-markdown-legacy/index.ts index cd26a7ba6..dd7259e2c 100644 --- a/packages/astro/src/vite-plugin-markdown-legacy/index.ts +++ b/packages/astro/src/vite-plugin-markdown-legacy/index.ts @@ -7,12 +7,16 @@ import { fileURLToPath } from 'url'; import type { Plugin, ViteDevServer } from 'vite'; import type { AstroConfig } from '../@types/astro'; import { pagesVirtualModuleId } from '../core/app/index.js'; +import { cachedCompilation, CompileProps } from '../core/compile/index.js'; import { collectErrorMetadata } from '../core/errors.js'; import type { LogOptions } from '../core/logger/core.js'; -import { cachedCompilation, CompileProps } from '../core/compile/index.js'; -import { ViteStyleTransformer, createViteStyleTransformer, createTransformStyles } from '../vite-style-transform/index.js'; import type { PluginMetadata as AstroPluginMetadata } from '../vite-plugin-astro/types'; import { getFileInfo } from '../vite-plugin-utils/index.js'; +import { + createTransformStyles, + createViteStyleTransformer, + ViteStyleTransformer, +} from '../vite-style-transform/index.js'; interface AstroPluginOptions { config: AstroConfig; @@ -208,7 +212,12 @@ ${setup}`.trim(); filename, moduleId: id, source: astroResult, - transformStyle: createTransformStyles(styleTransformer, filename, Boolean(opts?.ssr), this), + transformStyle: createTransformStyles( + styleTransformer, + filename, + Boolean(opts?.ssr), + this + ), }; let transformResult = await cachedCompilation(compileProps); diff --git a/packages/astro/src/vite-style-transform/index.ts b/packages/astro/src/vite-style-transform/index.ts index f3635cba7..8e03caca6 100644 --- a/packages/astro/src/vite-style-transform/index.ts +++ b/packages/astro/src/vite-style-transform/index.ts @@ -1,7 +1,2 @@ -export type { - ViteStyleTransformer -} from './style-transform'; -export { - createViteStyleTransformer, - createTransformStyles -} from './style-transform.js'; +export type { ViteStyleTransformer } from './style-transform'; +export { createTransformStyles, createViteStyleTransformer } from './style-transform.js'; diff --git a/packages/astro/src/vite-style-transform/style-transform.ts b/packages/astro/src/vite-style-transform/style-transform.ts index aebec2440..276a52246 100644 --- a/packages/astro/src/vite-style-transform/style-transform.ts +++ b/packages/astro/src/vite-style-transform/style-transform.ts @@ -1,14 +1,14 @@ import type { PluginContext } from 'rollup'; -import type { TransformStyle } from '../core/compile/index'; -import { TransformStyleWithVite, createTransformStyleWithViteFn } from './transform-with-vite.js'; import { fileURLToPath } from 'url'; +import type { TransformStyle } from '../core/compile/index'; +import { createTransformStyleWithViteFn, TransformStyleWithVite } from './transform-with-vite.js'; import type * as vite from 'vite'; export type ViteStyleTransformer = { viteDevServer?: vite.ViteDevServer; transformStyleWithVite: TransformStyleWithVite; -} +}; export function createViteStyleTransformer(viteConfig: vite.ResolvedConfig): ViteStyleTransformer { return { @@ -26,7 +26,12 @@ function getNormalizedIDForPostCSS(filename: string): string { } } -export function createTransformStyles(viteStyleTransformer: ViteStyleTransformer, filename: string, ssr: boolean, pluginContext: PluginContext): TransformStyle { +export function createTransformStyles( + viteStyleTransformer: ViteStyleTransformer, + filename: string, + ssr: boolean, + pluginContext: PluginContext +): TransformStyle { // handleHotUpdate doesn't have `addWatchFile` used by transformStyleWithVite. // TODO, refactor, why is this happening *here* ? if (!pluginContext.addWatchFile) { @@ -35,7 +40,7 @@ export function createTransformStyles(viteStyleTransformer: ViteStyleTransformer const normalizedID = getNormalizedIDForPostCSS(filename); - return async function(styleSource, lang) { + return async function (styleSource, lang) { const result = await viteStyleTransformer.transformStyleWithVite.call(pluginContext, { id: normalizedID, source: styleSource, diff --git a/packages/astro/test/units/compile/invalid-css.test.js b/packages/astro/test/units/compile/invalid-css.test.js index 72309c445..00d4fb7f6 100644 --- a/packages/astro/test/units/compile/invalid-css.test.js +++ b/packages/astro/test/units/compile/invalid-css.test.js @@ -1,4 +1,3 @@ - import { expect } from 'chai'; import { cachedCompilation } from '../../../dist/core/compile/index.js'; import { AggregateError } from '../../../dist/core/util.js'; @@ -9,8 +8,8 @@ describe('astro/src/core/compile', () => { let error; try { let r = await cachedCompilation({ - config: /** @type {any} */({ - root: '/' + config: /** @type {any} */ ({ + root: '/', }), filename: '/src/pages/index.astro', moduleId: '/src/pages/index.astro', @@ -30,9 +29,9 @@ describe('astro/src/core/compile', () => { `, transformStyle(source, lang) { throw new Error('Invalid css'); - } + }, }); - } catch(err) { + } catch (err) { error = err; }