0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

Remove unnecessary recmaInjectImportMetaEnv plugin for MDX (#10673)

This commit is contained in:
Bjorn Lu 2024-04-04 15:07:40 +08:00 committed by GitHub
parent 0899d342cd
commit db7f9c8703
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 6 additions and 76 deletions

View file

@ -0,0 +1,5 @@
---
"@astrojs/mdx": patch
---
Removes unnecessary internal `recmaInjectImportMetaEnv` plugin

View file

@ -86,7 +86,6 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
configResolved(resolved) { configResolved(resolved) {
processor = createMdxProcessor(mdxOptions, { processor = createMdxProcessor(mdxOptions, {
sourcemap: !!resolved.build.sourcemap, sourcemap: !!resolved.build.sourcemap,
importMetaEnv: { SITE: config.site, ...resolved.env },
}); });
// HACK: move ourselves before Astro's JSX plugin to transform things in the right order // HACK: move ourselves before Astro's JSX plugin to transform things in the right order

View file

@ -11,7 +11,6 @@ import remarkSmartypants from 'remark-smartypants';
import { SourceMapGenerator } from 'source-map'; import { SourceMapGenerator } from 'source-map';
import type { PluggableList } from 'unified'; import type { PluggableList } from 'unified';
import type { MdxOptions } from './index.js'; import type { MdxOptions } from './index.js';
import { recmaInjectImportMetaEnv } from './recma-inject-import-meta-env.js';
import { rehypeApplyFrontmatterExport } from './rehype-apply-frontmatter-export.js'; import { rehypeApplyFrontmatterExport } from './rehype-apply-frontmatter-export.js';
import { rehypeInjectHeadingsExport } from './rehype-collect-headings.js'; import { rehypeInjectHeadingsExport } from './rehype-collect-headings.js';
import rehypeMetaString from './rehype-meta-string.js'; import rehypeMetaString from './rehype-meta-string.js';
@ -23,14 +22,13 @@ const isPerformanceBenchmark = Boolean(process.env.ASTRO_PERFORMANCE_BENCHMARK);
interface MdxProcessorExtraOptions { interface MdxProcessorExtraOptions {
sourcemap: boolean; sourcemap: boolean;
importMetaEnv: Record<string, any>;
} }
export function createMdxProcessor(mdxOptions: MdxOptions, extraOptions: MdxProcessorExtraOptions) { export function createMdxProcessor(mdxOptions: MdxOptions, extraOptions: MdxProcessorExtraOptions) {
return createProcessor({ return createProcessor({
remarkPlugins: getRemarkPlugins(mdxOptions), remarkPlugins: getRemarkPlugins(mdxOptions),
rehypePlugins: getRehypePlugins(mdxOptions), rehypePlugins: getRehypePlugins(mdxOptions),
recmaPlugins: getRecmaPlugins(mdxOptions, extraOptions.importMetaEnv), recmaPlugins: mdxOptions.recmaPlugins,
remarkRehypeOptions: mdxOptions.remarkRehype, remarkRehypeOptions: mdxOptions.remarkRehype,
jsx: true, jsx: true,
jsxImportSource: 'astro', jsxImportSource: 'astro',
@ -95,10 +93,3 @@ function getRehypePlugins(mdxOptions: MdxOptions): PluggableList {
return rehypePlugins; return rehypePlugins;
} }
function getRecmaPlugins(
mdxOptions: MdxOptions,
importMetaEnv: Record<string, any>
): PluggableList {
return [...(mdxOptions.recmaPlugins ?? []), [recmaInjectImportMetaEnv, { importMetaEnv }]];
}

View file

@ -1,65 +0,0 @@
import type { Literal, MemberExpression } from 'estree';
import { visit as estreeVisit } from 'estree-util-visit';
export function recmaInjectImportMetaEnv({
importMetaEnv,
}: {
importMetaEnv: Record<string, any>;
}) {
return (tree: any) => {
estreeVisit(tree, (node) => {
if (node.type === 'MemberExpression') {
// attempt to get "import.meta.env" variable name
const envVarName = getImportMetaEnvVariableName(node);
if (typeof envVarName === 'string' && importMetaEnv[envVarName] != null) {
// clear object keys to replace with envVarLiteral
for (const key in node) {
delete (node as any)[key];
}
const envVarLiteral: Literal = {
type: 'Literal',
value: importMetaEnv[envVarName],
raw: JSON.stringify(importMetaEnv[envVarName]),
};
Object.assign(node, envVarLiteral);
}
}
});
};
}
/**
* Check if estree entry is "import.meta.env.VARIABLE"
* If it is, return the variable name (i.e. "VARIABLE")
*/
function getImportMetaEnvVariableName(node: MemberExpression): string | Error {
try {
// check for ".[ANYTHING]"
if (node.object.type !== 'MemberExpression' || node.property.type !== 'Identifier')
return new Error();
const nestedExpression = node.object;
// check for ".env"
if (nestedExpression.property.type !== 'Identifier' || nestedExpression.property.name !== 'env')
return new Error();
const envExpression = nestedExpression.object;
// check for ".meta"
if (
envExpression.type !== 'MetaProperty' ||
envExpression.property.type !== 'Identifier' ||
envExpression.property.name !== 'meta'
)
return new Error();
// check for "import"
if (envExpression.meta.name !== 'import') return new Error();
return node.property.name;
} catch (e) {
if (e instanceof Error) {
return e;
}
return new Error('Unknown parsing error');
}
}