Skip to content
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

Merged
merged 12 commits into from
Sep 26, 2022

Conversation

bholmesdev
Copy link
Contributor

@bholmesdev bholmesdev commented Sep 23, 2022

Changes

Testing

Test import.meta.env.* processed in expressions only, and not transformed in plain text.

Docs

N/A

@changeset-bot
Copy link

changeset-bot bot commented Sep 23, 2022

🦋 Changeset detected

Latest 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

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Sep 23, 2022
@matthewp
Copy link
Contributor

clever solution 👍

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not envious that you had to deal with this one again!

@bholmesdev
Copy link
Contributor Author

Bumping back to draft. Breaking existing import.meta.env support in JSX expressions doesn't feel right to me. Definitely want things like this to work:

export const canonical = import.meta.env.SITE + '/path'

@bholmesdev bholmesdev marked this pull request as draft September 26, 2022 16:09
@bholmesdev bholmesdev changed the title Fix: avoid transforming import.meta.env.* in MDX Fix: correctly transform import.meta.env.* in MDX Sep 26, 2022
@bholmesdev bholmesdev marked this pull request as ready for review September 26, 2022 20:44
Comment on lines +18 to +43
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);
}
}
});
};
}
Copy link
Contributor Author

@bholmesdev bholmesdev Sep 26, 2022

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.

Copy link
Contributor

@matthewp matthewp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Code blocks in MDX render environmental variables
3 participants