wip: add ?astro-asset-ssr flag

This commit is contained in:
bholmesdev 2022-10-19 11:39:26 -04:00
parent 6c4ba5e8b5
commit ff93d376d3
4 changed files with 17 additions and 4 deletions

View file

@ -4,6 +4,7 @@ import npath from 'path';
import { normalizePath } from 'vite';
import { resolveJsToTs } from '../core/util.js';
import { HydrationDirectiveProps } from '../runtime/server/hydration.js';
import { FLAG } from '../vite-plugin-asset-ssr/index.js';
import type { PluginMetadata } from '../vite-plugin-astro/types';
const ClientOnlyPlaceholder = 'astro-client-only';
@ -184,7 +185,7 @@ export default function astroJSX(): PluginObj {
const node = path.node;
// Skip automatic `_components` in MDX files
if (
state.filename?.endsWith('.mdx') &&
(state.filename?.endsWith('.mdx') || state.filename?.endsWith('.mdx' + FLAG)) &&
t.isJSXIdentifier(node.object) &&
node.object.name === '_components'
) {

View file

@ -0,0 +1 @@
export const FLAG = '?astro-asset-ssr';

View file

@ -13,6 +13,7 @@ import path from 'path';
import { error } from '../core/logger/core.js';
import { parseNpmName } from '../core/util.js';
import tagExportsPlugin from './tag.js';
import { FLAG } from '../vite-plugin-asset-ssr/index.js';
type FixedCompilerOptions = TsConfigJson.CompilerOptions & {
jsxImportSource?: string;
@ -185,7 +186,9 @@ export default function jsx({ settings, logging }: AstroPluginJSXOptions): Plugi
}
defaultJSXRendererEntry = [...jsxRenderersIntegrationOnly.entries()][0];
},
async transform(code, id, opts) {
async transform(code, unresolvedId, opts) {
let id = unresolvedId.endsWith(`.mdx${FLAG}`) ? unresolvedId.replace(FLAG, '') : unresolvedId;
const ssr = Boolean(opts?.ssr);
if (!JSX_EXTENSIONS.has(path.extname(id))) {
return null;

View file

@ -15,6 +15,8 @@ import {
} from './plugins.js';
import { getFileInfo, handleExtendsNotSupported, parseFrontmatter } from './utils.js';
const FLAG = '?astro-asset-ssr';
const RAW_CONTENT_ERROR =
'MDX does not support rawContent()! If you need to read the Markdown contents to calculate values (ex. reading time), we suggest injecting frontmatter via remark plugins. Learn more on our docs: https://docs.astro.build/en/guides/integrations-guide/mdx/#inject-frontmatter-via-remark-or-rehype-plugins';
@ -86,7 +88,10 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
},
// Override transform to alter code before MDX compilation
// ex. inject layouts
async transform(_, id) {
async transform(_, unresolvedId) {
let id = unresolvedId.endsWith(`.mdx${FLAG}`)
? unresolvedId.replace(FLAG, '')
: unresolvedId;
if (!id.endsWith('mdx')) return;
// Read code from file manually to prevent Vite from parsing `import.meta.env` expressions
@ -112,7 +117,10 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
{
name: '@astrojs/mdx-postprocess',
// These transforms must happen *after* JSX runtime transformations
transform(code, id) {
transform(code, unresolvedId) {
let id = unresolvedId.endsWith(`.mdx${FLAG}`)
? unresolvedId.replace(FLAG, '')
: unresolvedId;
if (!id.endsWith('.mdx')) return;
// Ensures styles and scripts are injected into a `<head>`