[ci] format
This commit is contained in:
parent
63e49c3b64
commit
e81292c4ab
11 changed files with 66 additions and 58 deletions
|
@ -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<string, CompileResult>;
|
||||
|
@ -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];
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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<string>, errors: Error[]): PreprocessStyle {
|
||||
export function createStylePreprocessor(
|
||||
transformStyle: TransformStyle,
|
||||
cssDeps: Set<string>,
|
||||
errors: Error[]
|
||||
): PreprocessStyle {
|
||||
const preprocessStyle: PreprocessStyle = async (value: string, attrs: Record<string, string>) => {
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
import type { SourceMap } from 'rollup';
|
||||
|
||||
export type TransformStyleResult = null | {
|
||||
export type TransformStyleResult = null | {
|
||||
code: string;
|
||||
map: SourceMap | null;
|
||||
deps: Set<string>;
|
||||
}
|
||||
};
|
||||
|
||||
export type TransformStyle = (source: string, lang: string) => TransformStyleResult | Promise<TransformStyleResult>;
|
||||
export type TransformStyle = (
|
||||
source: string,
|
||||
lang: string
|
||||
) => TransformStyleResult | Promise<TransformStyleResult>;
|
||||
|
|
|
@ -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<any> = [];
|
||||
constructor( errors: Iterable<any>, message?: string | undefined) {
|
||||
super(message);
|
||||
this.errors = Array.from(errors);
|
||||
}
|
||||
}
|
||||
export const AggregateError =
|
||||
typeof globalThis.AggregateError !== 'undefined'
|
||||
? globalThis.AggregateError
|
||||
: class extends Error {
|
||||
errors: Array<any> = [];
|
||||
constructor(errors: Iterable<any>, message?: string | undefined) {
|
||||
super(message);
|
||||
this.errors = Array.from(errors);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue