-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes from 9 commits
afa2d51
e403ff8
080dccd
c0421c8
dc391f9
adc6f24
588ad63
b5f20cf
d26dfa1
849cc78
fe16773
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@astrojs/mdx": patch | ||
--- | ||
|
||
Removes environment variables workaround that broke project builds with sourcemaps |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"astro": patch | ||
--- | ||
|
||
Improves environment variables handling by using esbuild to perform replacements |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,7 +129,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI | |
const compiled = await processor.process(vfile); | ||
|
||
return { | ||
code: escapeViteEnvReferences(String(compiled.value)), | ||
code: String(compiled.value), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Luckily Astro's env replacements (which didn't work well in plain strings) happen before the |
||
map: compiled.map, | ||
}; | ||
} catch (e: any) { | ||
|
@@ -215,7 +215,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI | |
import.meta.hot.decline(); | ||
}`; | ||
} | ||
return { code: escapeViteEnvReferences(code), map: null }; | ||
return { code, map: null }; | ||
}, | ||
}, | ||
] as VitePlugin[], | ||
|
@@ -262,10 +262,3 @@ function applyDefaultOptions({ | |
optimize: options.optimize ?? defaults.optimize, | ||
}; | ||
} | ||
|
||
// Converts the first dot in `import.meta.env` to its Unicode escape sequence, | ||
// which prevents Vite from replacing strings like `import.meta.env.SITE` | ||
// in our JS representation of loaded Markdown files | ||
function escapeViteEnvReferences(code: string) { | ||
return code.replace(/import\.meta\.env/g, 'import\\u002Emeta.env'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
// clear object keys to replace with envVarLiteral | ||
for (const key in node) { | ||
delete (node as any)[key]; | ||
|
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.
Copied this hack from
@astrojs/mdx
.