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:
parent
0899d342cd
commit
db7f9c8703
4 changed files with 6 additions and 76 deletions
5
.changeset/sharp-socks-kick.md
Normal file
5
.changeset/sharp-socks-kick.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@astrojs/mdx": patch
|
||||
---
|
||||
|
||||
Removes unnecessary internal `recmaInjectImportMetaEnv` plugin
|
|
@ -86,7 +86,6 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): 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
|
||||
|
|
|
@ -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<string, any>;
|
||||
}
|
||||
|
||||
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<string, any>
|
||||
): PluggableList {
|
||||
return [...(mdxOptions.recmaPlugins ?? []), [recmaInjectImportMetaEnv, { importMetaEnv }]];
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue