From 729eac4ed84d9007e80e37b66799a812930ec4f9 Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 2 Apr 2024 11:44:11 +0800 Subject: [PATCH 1/2] Remove unneeded recmaInjectImportMetaEnv 1.27s of work --- packages/integrations/mdx/src/index.ts | 1 - packages/integrations/mdx/src/plugins.ts | 11 +--- .../mdx/src/recma-inject-import-meta-env.ts | 65 ------------------- 3 files changed, 1 insertion(+), 76 deletions(-) delete mode 100644 packages/integrations/mdx/src/recma-inject-import-meta-env.ts diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index adbe49e488d1..964b66eabafd 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 5bc7ca982c6c..99d0c70b2756 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 df753ef4c194..000000000000 --- 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'); - } -} From f8e86ed5d5798acb7ed34282afb285bd84f58805 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 4 Apr 2024 10:51:56 +0800 Subject: [PATCH 2/2] Add changeset --- .changeset/sharp-socks-kick.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sharp-socks-kick.md diff --git a/.changeset/sharp-socks-kick.md b/.changeset/sharp-socks-kick.md new file mode 100644 index 000000000000..41c71c50e9e9 --- /dev/null +++ b/.changeset/sharp-socks-kick.md @@ -0,0 +1,5 @@ +--- +"@astrojs/mdx": patch +--- + +Removes unnecessary internal `recmaInjectImportMetaEnv` plugin