Pass through plugin context

This commit is contained in:
unknown 2022-06-17 16:23:32 -04:00
parent e1712020d4
commit ebfd5cff9c
3 changed files with 13 additions and 6 deletions

View file

@ -1,5 +1,5 @@
import type { TransformResult } from '@astrojs/compiler';
import type { SourceMapInput } from 'rollup';
import type { PluginContext, SourceMapInput } from 'rollup';
import type { AstroConfig } from '../@types/astro';
import type { TransformHook } from './styles';
@ -33,13 +33,14 @@ function safelyReplaceImportPlaceholder(code: string) {
const configCache = new WeakMap<AstroConfig, CompilationCache>();
interface CompileProps {
export interface CompileProps {
config: AstroConfig;
filename: string;
moduleId: string;
source: string;
ssr: boolean;
viteTransform: TransformHook;
pluginContext: PluginContext;
}
async function compile({
@ -49,6 +50,7 @@ async function compile({
source,
ssr,
viteTransform,
pluginContext,
}: CompileProps): Promise<CompileResult> {
const filenameURL = new URL(`file://${filename}`);
const normalizedID = fileURLToPath(filenameURL);
@ -98,6 +100,7 @@ async function compile({
id: normalizedID,
transformHook: viteTransform,
ssr,
pluginContext,
});
let map: SourceMapInput | undefined;

View file

@ -13,7 +13,7 @@ import { isRelativePath, startsWithForwardSlash } from '../core/path.js';
import { resolvePages } from '../core/util.js';
import { PAGE_SCRIPT_ID, PAGE_SSR_SCRIPT_ID } from '../vite-plugin-scripts/index.js';
import { getFileInfo } from '../vite-plugin-utils/index.js';
import { cachedCompilation } from './compile.js';
import { cachedCompilation, CompileProps } from './compile.js';
import { handleHotUpdate, trackCSSDependencies } from './hmr.js';
import { parseAstroRequest } from './query.js';
import { getViteTransform, TransformHook } from './styles.js';
@ -106,13 +106,14 @@ export default function astro({ config, logging }: AstroPluginOptions): vite.Plu
if (isPage && config._ctx.scripts.some((s) => s.stage === 'page')) {
source += `\n<script src="${PAGE_SCRIPT_ID}" />`;
}
const compileProps = {
const compileProps: CompileProps = {
config,
filename,
moduleId: id,
source,
ssr: Boolean(opts?.ssr),
viteTransform,
pluginContext: this
};
if (query.astro) {
if (query.type === 'style') {

View file

@ -1,3 +1,4 @@
import type { PluginContext } from 'rollup';
import type * as vite from 'vite';
import { STYLE_EXTENSIONS } from '../core/render/util.js';
@ -13,7 +14,7 @@ export function getViteTransform(viteConfig: vite.ResolvedConfig): TransformHook
const viteCSSPlugin = viteConfig.plugins.find(({ name }) => name === 'vite:css');
if (!viteCSSPlugin) throw new Error(`vite:css plugin couldnt be found`);
if (!viteCSSPlugin.transform) throw new Error(`vite:css has no transform() hook`);
return viteCSSPlugin.transform.bind(null as any) as any;
return viteCSSPlugin.transform as any;
}
interface TransformWithViteOptions {
@ -21,6 +22,7 @@ interface TransformWithViteOptions {
lang: string;
id: string;
transformHook: TransformHook;
pluginContext: PluginContext;
ssr?: boolean;
}
@ -31,9 +33,10 @@ export async function transformWithVite({
transformHook,
id,
ssr,
pluginContext,
}: TransformWithViteOptions): Promise<vite.TransformResult | null> {
if (!STYLE_EXTENSIONS.has(lang)) {
return null; // only preprocess langs supported by Vite
}
return transformHook(value, id + `?astro&type=style&lang${lang}`, ssr);
return transformHook.call(pluginContext, value, id + `?astro&type=style&lang${lang}`, ssr);
}