Pass through plugin context
This commit is contained in:
parent
e1712020d4
commit
ebfd5cff9c
3 changed files with 13 additions and 6 deletions
|
@ -1,5 +1,5 @@
|
||||||
import type { TransformResult } from '@astrojs/compiler';
|
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 { AstroConfig } from '../@types/astro';
|
||||||
import type { TransformHook } from './styles';
|
import type { TransformHook } from './styles';
|
||||||
|
|
||||||
|
@ -33,13 +33,14 @@ function safelyReplaceImportPlaceholder(code: string) {
|
||||||
|
|
||||||
const configCache = new WeakMap<AstroConfig, CompilationCache>();
|
const configCache = new WeakMap<AstroConfig, CompilationCache>();
|
||||||
|
|
||||||
interface CompileProps {
|
export interface CompileProps {
|
||||||
config: AstroConfig;
|
config: AstroConfig;
|
||||||
filename: string;
|
filename: string;
|
||||||
moduleId: string;
|
moduleId: string;
|
||||||
source: string;
|
source: string;
|
||||||
ssr: boolean;
|
ssr: boolean;
|
||||||
viteTransform: TransformHook;
|
viteTransform: TransformHook;
|
||||||
|
pluginContext: PluginContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function compile({
|
async function compile({
|
||||||
|
@ -49,6 +50,7 @@ async function compile({
|
||||||
source,
|
source,
|
||||||
ssr,
|
ssr,
|
||||||
viteTransform,
|
viteTransform,
|
||||||
|
pluginContext,
|
||||||
}: CompileProps): Promise<CompileResult> {
|
}: CompileProps): Promise<CompileResult> {
|
||||||
const filenameURL = new URL(`file://${filename}`);
|
const filenameURL = new URL(`file://${filename}`);
|
||||||
const normalizedID = fileURLToPath(filenameURL);
|
const normalizedID = fileURLToPath(filenameURL);
|
||||||
|
@ -98,6 +100,7 @@ async function compile({
|
||||||
id: normalizedID,
|
id: normalizedID,
|
||||||
transformHook: viteTransform,
|
transformHook: viteTransform,
|
||||||
ssr,
|
ssr,
|
||||||
|
pluginContext,
|
||||||
});
|
});
|
||||||
|
|
||||||
let map: SourceMapInput | undefined;
|
let map: SourceMapInput | undefined;
|
||||||
|
|
|
@ -13,7 +13,7 @@ import { isRelativePath, startsWithForwardSlash } from '../core/path.js';
|
||||||
import { resolvePages } from '../core/util.js';
|
import { resolvePages } from '../core/util.js';
|
||||||
import { PAGE_SCRIPT_ID, PAGE_SSR_SCRIPT_ID } from '../vite-plugin-scripts/index.js';
|
import { PAGE_SCRIPT_ID, PAGE_SSR_SCRIPT_ID } from '../vite-plugin-scripts/index.js';
|
||||||
import { getFileInfo } from '../vite-plugin-utils/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 { handleHotUpdate, trackCSSDependencies } from './hmr.js';
|
||||||
import { parseAstroRequest } from './query.js';
|
import { parseAstroRequest } from './query.js';
|
||||||
import { getViteTransform, TransformHook } from './styles.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')) {
|
if (isPage && config._ctx.scripts.some((s) => s.stage === 'page')) {
|
||||||
source += `\n<script src="${PAGE_SCRIPT_ID}" />`;
|
source += `\n<script src="${PAGE_SCRIPT_ID}" />`;
|
||||||
}
|
}
|
||||||
const compileProps = {
|
const compileProps: CompileProps = {
|
||||||
config,
|
config,
|
||||||
filename,
|
filename,
|
||||||
moduleId: id,
|
moduleId: id,
|
||||||
source,
|
source,
|
||||||
ssr: Boolean(opts?.ssr),
|
ssr: Boolean(opts?.ssr),
|
||||||
viteTransform,
|
viteTransform,
|
||||||
|
pluginContext: this
|
||||||
};
|
};
|
||||||
if (query.astro) {
|
if (query.astro) {
|
||||||
if (query.type === 'style') {
|
if (query.type === 'style') {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import type { PluginContext } from 'rollup';
|
||||||
import type * as vite from 'vite';
|
import type * as vite from 'vite';
|
||||||
|
|
||||||
import { STYLE_EXTENSIONS } from '../core/render/util.js';
|
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');
|
const viteCSSPlugin = viteConfig.plugins.find(({ name }) => name === 'vite:css');
|
||||||
if (!viteCSSPlugin) throw new Error(`vite:css plugin couldn’t be found`);
|
if (!viteCSSPlugin) throw new Error(`vite:css plugin couldn’t be found`);
|
||||||
if (!viteCSSPlugin.transform) throw new Error(`vite:css has no transform() hook`);
|
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 {
|
interface TransformWithViteOptions {
|
||||||
|
@ -21,6 +22,7 @@ interface TransformWithViteOptions {
|
||||||
lang: string;
|
lang: string;
|
||||||
id: string;
|
id: string;
|
||||||
transformHook: TransformHook;
|
transformHook: TransformHook;
|
||||||
|
pluginContext: PluginContext;
|
||||||
ssr?: boolean;
|
ssr?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,9 +33,10 @@ export async function transformWithVite({
|
||||||
transformHook,
|
transformHook,
|
||||||
id,
|
id,
|
||||||
ssr,
|
ssr,
|
||||||
|
pluginContext,
|
||||||
}: TransformWithViteOptions): Promise<vite.TransformResult | null> {
|
}: TransformWithViteOptions): Promise<vite.TransformResult | null> {
|
||||||
if (!STYLE_EXTENSIONS.has(lang)) {
|
if (!STYLE_EXTENSIONS.has(lang)) {
|
||||||
return null; // only preprocess langs supported by Vite
|
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue