[ci] format

This commit is contained in:
matthewp 2022-09-09 13:04:29 +00:00 committed by fredkbot
parent 63e49c3b64
commit e81292c4ab
11 changed files with 66 additions and 58 deletions

View file

@ -5,7 +5,7 @@ import type { TransformStyle } from './types';
import { transform } from '@astrojs/compiler'; import { transform } from '@astrojs/compiler';
import { AstroErrorCodes } from '../errors.js'; import { AstroErrorCodes } from '../errors.js';
import { prependForwardSlash } from '../path.js'; import { prependForwardSlash } from '../path.js';
import { viteID, AggregateError } from '../util.js'; import { AggregateError, viteID } from '../util.js';
import { createStylePreprocessor } from './style.js'; import { createStylePreprocessor } from './style.js';
type CompilationCache = Map<string, CompileResult>; type CompilationCache = Map<string, CompileResult>;
@ -57,11 +57,12 @@ async function compile({
throw err; throw err;
}) })
.then((result) => { .then((result) => {
switch(cssTransformErrors.length) { switch (cssTransformErrors.length) {
case 0: return result; case 0:
return result;
case 1: { case 1: {
let error = cssTransformErrors[0]; let error = cssTransformErrors[0];
if(!(error as any).code) { if (!(error as any).code) {
(error as any).code = AstroErrorCodes.UnknownCompilerCSSError; (error as any).code = AstroErrorCodes.UnknownCompilerCSSError;
} }
throw cssTransformErrors[0]; throw cssTransformErrors[0];

View file

@ -1,13 +1,3 @@
export type { export type { CompileProps } from './compile';
CompileProps export { cachedCompilation, getCachedSource, invalidateCompilation, isCached } from './compile.js';
} from './compile'; export type { TransformStyle } from './types';
export type {
TransformStyle
} from './types';
export {
cachedCompilation,
invalidateCompilation,
isCached,
getCachedSource
} from './compile.js';

View file

@ -1,10 +1,14 @@
import type { TransformOptions } from '@astrojs/compiler'; import type { TransformOptions } from '@astrojs/compiler';
import type { TransformStyle } from './types';
import type { SourceMapInput } from 'rollup'; import type { SourceMapInput } from 'rollup';
import type { TransformStyle } from './types';
type PreprocessStyle = TransformOptions['preprocessStyle']; 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 preprocessStyle: PreprocessStyle = async (value: string, attrs: Record<string, string>) => {
const lang = `.${attrs?.lang || 'css'}`.toLowerCase(); const lang = `.${attrs?.lang || 'css'}`.toLowerCase();
@ -30,10 +34,10 @@ export function createStylePreprocessor(transformStyle: TransformStyle, cssDeps:
} catch (err) { } catch (err) {
errors.push(err as unknown as Error); errors.push(err as unknown as Error);
return { return {
error: err + '' error: err + '',
}; };
} }
}; };
return preprocessStyle; return preprocessStyle;
}; }

View file

@ -1,9 +1,12 @@
import type { SourceMap } from 'rollup'; import type { SourceMap } from 'rollup';
export type TransformStyleResult = null | { export type TransformStyleResult = null | {
code: string; code: string;
map: SourceMap | null; map: SourceMap | null;
deps: Set<string>; deps: Set<string>;
} };
export type TransformStyle = (source: string, lang: string) => TransformStyleResult | Promise<TransformStyleResult>; export type TransformStyle = (
source: string,
lang: string
) => TransformStyleResult | Promise<TransformStyleResult>;

View file

@ -227,10 +227,13 @@ export async function resolveIdToUrl(viteServer: ViteDevServer, id: string) {
return VALID_ID_PREFIX + result.id; return VALID_ID_PREFIX + result.id;
} }
export const AggregateError = typeof globalThis.AggregateError !== 'undefined' ? globalThis.AggregateError : class extends Error { export const AggregateError =
errors: Array<any> = []; typeof globalThis.AggregateError !== 'undefined'
constructor( errors: Iterable<any>, message?: string | undefined) { ? globalThis.AggregateError
super(message); : class extends Error {
this.errors = Array.from(errors); errors: Array<any> = [];
} constructor(errors: Iterable<any>, message?: string | undefined) {
} super(message);
this.errors = Array.from(errors);
}
};

View file

@ -1,10 +1,10 @@
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import type { HmrContext, ModuleNode } from 'vite'; import type { HmrContext, ModuleNode } from 'vite';
import type { AstroConfig } from '../@types/astro'; import type { AstroConfig } from '../@types/astro';
import { cachedCompilation, invalidateCompilation, isCached } from '../core/compile/index.js';
import type { LogOptions } from '../core/logger/core.js'; import type { LogOptions } from '../core/logger/core.js';
import { info } from '../core/logger/core.js'; import { info } from '../core/logger/core.js';
import * as msg from '../core/messages.js'; import * as msg from '../core/messages.js';
import { cachedCompilation, invalidateCompilation, isCached } from '../core/compile/index.js';
import { isAstroScript } from './query.js'; import { isAstroScript } from './query.js';
const PKG_PREFIX = new URL('../../', import.meta.url); const PKG_PREFIX = new URL('../../', import.meta.url);

View file

@ -2,23 +2,22 @@ import type { PluginContext, SourceDescription } from 'rollup';
import type * as vite from 'vite'; import type * as vite from 'vite';
import type { AstroConfig } from '../@types/astro'; import type { AstroConfig } from '../@types/astro';
import type { LogOptions } from '../core/logger/core.js'; import type { LogOptions } from '../core/logger/core.js';
import type { PluginMetadata as AstroPluginMetadata } from './types';
import type { ViteStyleTransformer } from '../vite-style-transform'; import type { ViteStyleTransformer } from '../vite-style-transform';
import type { PluginMetadata as AstroPluginMetadata } from './types';
import ancestor from 'common-ancestor-path'; import ancestor from 'common-ancestor-path';
import esbuild from 'esbuild'; import esbuild from 'esbuild';
import slash from 'slash'; import slash from 'slash';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { cachedCompilation, CompileProps, getCachedSource } from '../core/compile/index.js';
import { isRelativePath, startsWithForwardSlash } from '../core/path.js'; import { isRelativePath, startsWithForwardSlash } from '../core/path.js';
import { getFileInfo } from '../vite-plugin-utils/index.js'; import { getFileInfo } from '../vite-plugin-utils/index.js';
import { import {
cachedCompilation, createTransformStyles,
CompileProps, createViteStyleTransformer,
getCachedSource } from '../vite-style-transform/index.js';
} from '../core/compile/index.js';
import { handleHotUpdate } from './hmr.js'; import { handleHotUpdate } from './hmr.js';
import { parseAstroRequest, ParsedRequestResult } from './query.js'; import { parseAstroRequest, ParsedRequestResult } from './query.js';
import { createViteStyleTransformer, createTransformStyles } from '../vite-style-transform/index.js';
const FRONTMATTER_PARSE_REGEXP = /^\-\-\-(.*)^\-\-\-/ms; const FRONTMATTER_PARSE_REGEXP = /^\-\-\-(.*)^\-\-\-/ms;
interface AstroPluginOptions { interface AstroPluginOptions {

View file

@ -7,12 +7,16 @@ import { fileURLToPath } from 'url';
import type { Plugin, ViteDevServer } from 'vite'; import type { Plugin, ViteDevServer } from 'vite';
import type { AstroConfig } from '../@types/astro'; import type { AstroConfig } from '../@types/astro';
import { pagesVirtualModuleId } from '../core/app/index.js'; import { pagesVirtualModuleId } from '../core/app/index.js';
import { cachedCompilation, CompileProps } from '../core/compile/index.js';
import { collectErrorMetadata } from '../core/errors.js'; import { collectErrorMetadata } from '../core/errors.js';
import type { LogOptions } from '../core/logger/core.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 type { PluginMetadata as AstroPluginMetadata } from '../vite-plugin-astro/types';
import { getFileInfo } from '../vite-plugin-utils/index.js'; import { getFileInfo } from '../vite-plugin-utils/index.js';
import {
createTransformStyles,
createViteStyleTransformer,
ViteStyleTransformer,
} from '../vite-style-transform/index.js';
interface AstroPluginOptions { interface AstroPluginOptions {
config: AstroConfig; config: AstroConfig;
@ -208,7 +212,12 @@ ${setup}`.trim();
filename, filename,
moduleId: id, moduleId: id,
source: astroResult, source: astroResult,
transformStyle: createTransformStyles(styleTransformer, filename, Boolean(opts?.ssr), this), transformStyle: createTransformStyles(
styleTransformer,
filename,
Boolean(opts?.ssr),
this
),
}; };
let transformResult = await cachedCompilation(compileProps); let transformResult = await cachedCompilation(compileProps);

View file

@ -1,7 +1,2 @@
export type { export type { ViteStyleTransformer } from './style-transform';
ViteStyleTransformer export { createTransformStyles, createViteStyleTransformer } from './style-transform.js';
} from './style-transform';
export {
createViteStyleTransformer,
createTransformStyles
} from './style-transform.js';

View file

@ -1,14 +1,14 @@
import type { PluginContext } from 'rollup'; 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 { fileURLToPath } from 'url';
import type { TransformStyle } from '../core/compile/index';
import { createTransformStyleWithViteFn, TransformStyleWithVite } from './transform-with-vite.js';
import type * as vite from 'vite'; import type * as vite from 'vite';
export type ViteStyleTransformer = { export type ViteStyleTransformer = {
viteDevServer?: vite.ViteDevServer; viteDevServer?: vite.ViteDevServer;
transformStyleWithVite: TransformStyleWithVite; transformStyleWithVite: TransformStyleWithVite;
} };
export function createViteStyleTransformer(viteConfig: vite.ResolvedConfig): ViteStyleTransformer { export function createViteStyleTransformer(viteConfig: vite.ResolvedConfig): ViteStyleTransformer {
return { 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. // handleHotUpdate doesn't have `addWatchFile` used by transformStyleWithVite.
// TODO, refactor, why is this happening *here* ? // TODO, refactor, why is this happening *here* ?
if (!pluginContext.addWatchFile) { if (!pluginContext.addWatchFile) {
@ -35,7 +40,7 @@ export function createTransformStyles(viteStyleTransformer: ViteStyleTransformer
const normalizedID = getNormalizedIDForPostCSS(filename); const normalizedID = getNormalizedIDForPostCSS(filename);
return async function(styleSource, lang) { return async function (styleSource, lang) {
const result = await viteStyleTransformer.transformStyleWithVite.call(pluginContext, { const result = await viteStyleTransformer.transformStyleWithVite.call(pluginContext, {
id: normalizedID, id: normalizedID,
source: styleSource, source: styleSource,

View file

@ -1,4 +1,3 @@
import { expect } from 'chai'; import { expect } from 'chai';
import { cachedCompilation } from '../../../dist/core/compile/index.js'; import { cachedCompilation } from '../../../dist/core/compile/index.js';
import { AggregateError } from '../../../dist/core/util.js'; import { AggregateError } from '../../../dist/core/util.js';
@ -9,8 +8,8 @@ describe('astro/src/core/compile', () => {
let error; let error;
try { try {
let r = await cachedCompilation({ let r = await cachedCompilation({
config: /** @type {any} */({ config: /** @type {any} */ ({
root: '/' root: '/',
}), }),
filename: '/src/pages/index.astro', filename: '/src/pages/index.astro',
moduleId: '/src/pages/index.astro', moduleId: '/src/pages/index.astro',
@ -30,9 +29,9 @@ describe('astro/src/core/compile', () => {
`, `,
transformStyle(source, lang) { transformStyle(source, lang) {
throw new Error('Invalid css'); throw new Error('Invalid css');
} },
}); });
} catch(err) { } catch (err) {
error = err; error = err;
} }