-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: correctly transform import.meta.env.*
in MDX
#4858
Conversation
Co-authored-by: Jackie Macharia <jackiewmacharia>
🦋 Changeset detectedLatest commit: 454f5c4 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
clever solution 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not env
ious that you had to deal with this one again!
Bumping back to draft. Breaking existing export const canonical = import.meta.env.SITE + '/path' |
import.meta.env.*
in MDXimport.meta.env.*
in MDX
export function recmaInjectImportMetaEnvPlugin({ | ||
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 as MemberExpression); | ||
if (typeof envVarName === 'string') { | ||
// 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); | ||
} | ||
} | ||
}); | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I renamed data-astro-utils.ts
to plugins.ts
, so you can safely ignore the rest of the file! This function and that new getImportMetaEnvVariableName
helper is the new part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Changes
import.meta.env
in MDX plain text and frontmatter.import.meta.env
in{JSX expressions}
, variableexport
s, and HTML attributes.Testing
Test
import.meta.env.*
processed in expressions only, and not transformed in plain text.Docs
N/A