diff --git a/.changeset/sharp-socks-kick.md b/.changeset/sharp-socks-kick.md new file mode 100644 index 0000000000..41c71c50e9 --- /dev/null +++ b/.changeset/sharp-socks-kick.md @@ -0,0 +1,5 @@ +--- +"@astrojs/mdx": patch +--- + +Removes unnecessary internal `recmaInjectImportMetaEnv` plugin diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index adbe49e488..964b66eaba 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -86,7 +86,6 @@ export default function mdx(partialMdxOptions: Partial = {}): AstroI configResolved(resolved) { processor = createMdxProcessor(mdxOptions, { 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 diff --git a/packages/integrations/mdx/src/plugins.ts b/packages/integrations/mdx/src/plugins.ts index 5bc7ca982c..99d0c70b27 100644 --- a/packages/integrations/mdx/src/plugins.ts +++ b/packages/integrations/mdx/src/plugins.ts @@ -11,7 +11,6 @@ import remarkSmartypants from 'remark-smartypants'; import { SourceMapGenerator } from 'source-map'; import type { PluggableList } from 'unified'; import type { MdxOptions } from './index.js'; -import { recmaInjectImportMetaEnv } from './recma-inject-import-meta-env.js'; import { rehypeApplyFrontmatterExport } from './rehype-apply-frontmatter-export.js'; import { rehypeInjectHeadingsExport } from './rehype-collect-headings.js'; import rehypeMetaString from './rehype-meta-string.js'; @@ -23,14 +22,13 @@ const isPerformanceBenchmark = Boolean(process.env.ASTRO_PERFORMANCE_BENCHMARK); interface MdxProcessorExtraOptions { sourcemap: boolean; - importMetaEnv: Record; } export function createMdxProcessor(mdxOptions: MdxOptions, extraOptions: MdxProcessorExtraOptions) { return createProcessor({ remarkPlugins: getRemarkPlugins(mdxOptions), rehypePlugins: getRehypePlugins(mdxOptions), - recmaPlugins: getRecmaPlugins(mdxOptions, extraOptions.importMetaEnv), + recmaPlugins: mdxOptions.recmaPlugins, remarkRehypeOptions: mdxOptions.remarkRehype, jsx: true, jsxImportSource: 'astro', @@ -95,10 +93,3 @@ function getRehypePlugins(mdxOptions: MdxOptions): PluggableList { return rehypePlugins; } - -function getRecmaPlugins( - mdxOptions: MdxOptions, - importMetaEnv: Record -): PluggableList { - return [...(mdxOptions.recmaPlugins ?? []), [recmaInjectImportMetaEnv, { importMetaEnv }]]; -} diff --git a/packages/integrations/mdx/src/recma-inject-import-meta-env.ts b/packages/integrations/mdx/src/recma-inject-import-meta-env.ts deleted file mode 100644 index df753ef4c1..0000000000 --- a/packages/integrations/mdx/src/recma-inject-import-meta-env.ts +++ /dev/null @@ -1,65 +0,0 @@ -import type { Literal, MemberExpression } from 'estree'; -import { visit as estreeVisit } from 'estree-util-visit'; - -export function recmaInjectImportMetaEnv({ - importMetaEnv, -}: { - importMetaEnv: Record; -}) { - 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'); - } -}