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

Use esbuild for env replacement #9652

Merged
merged 11 commits into from
Jan 11, 2024
Merged

Use esbuild for env replacement #9652

merged 11 commits into from
Jan 11, 2024

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Jan 9, 2024

Changes

fixes #7503
fixes #9012

In dev, we can use a fast path to assign all private env vars to import.meta.env on the server-side runtime, which is a lot faster and is what Vite does too.

In build, we use esbuild to perform the replacements so that we don't accidentally replace env vars inside strings, same as Vite.

Testing

Existing tests should pass. I also added test for the MDX issue, but not for the Vue one. It's a bit harder to test that, but I can confirm it's fixed with the repro manually.

NOTE: The changes in astro and @astrojs/mdx are mutually exclusive, which means the user can use the latest version of astro and older version of @astrojs/mdx at the same time just fine. (and vice versa)

Docs

n/a. Ideally this should be transparent to users.

Copy link

changeset-bot bot commented Jan 9, 2024

🦋 Changeset detected

Latest commit: fe16773

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 pkg: integration Related to any renderer integration (scope) pkg: astro Related to the core `astro` package (scope) labels Jan 9, 2024
code: escapeViteEnvReferences(String(compiled.value)),
code: String(compiled.value),
Copy link
Member Author

Choose a reason for hiding this comment

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

We can safely remove this in a non breaking way as the plugin calls fs.readFile, bypassing Vite's transform. The only continuing env replacement left after this transform is Vite's define, which we know is fixed in Vite 5.

Luckily Astro's env replacements (which didn't work well in plain strings) happen before the fs.readFile, so the broken code was thrown away.

@@ -29,7 +33,7 @@ function getPrivateEnv(
const privateEnv: Record<string, string> = {};
for (const key in fullEnv) {
// Ignore public env var
if (envPrefixes.every((prefix) => !key.startsWith(prefix))) {
if (isValidIdentifierRe.test(key) && envPrefixes.every((prefix) => !key.startsWith(prefix))) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Added a valid identifier check because keys could be ProgramFiles(x64) which wouldn't work as import.meta.env.ProgramFiles(x64). This worked before because we do plain string replacements, but I don't think people were relying on this to work.

configResolved(resolvedConfig) {
viteConfig = resolvedConfig;

// HACK: move ourselves before Vite's define plugin to apply replacements at the right time (before Vite normal plugins)
Copy link
Member Author

Choose a reason for hiding this comment

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

Copied this hack from @astrojs/mdx.

@@ -11,7 +11,7 @@ export function recmaInjectImportMetaEnv({
if (node.type === 'MemberExpression') {
// attempt to get "import.meta.env" variable name
const envVarName = getImportMetaEnvVariableName(node);
if (typeof envVarName === 'string') {
if (typeof envVarName === 'string' && importMetaEnv[envVarName] != null) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This check was the main reason for the error in #9012, since if it's undefined, we accidentally generate a broken AST.

After fixing this, it relies on other fixes in this PR so that it works correctly in runtime.

@bluwy bluwy merged commit e72efd6 into main Jan 11, 2024
13 checks passed
@bluwy bluwy deleted the define-replace branch January 11, 2024 04:06
@astrobot-houston astrobot-houston mentioned this pull request Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)
Projects
None yet
3 participants