summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar hippotastic <6137925+hippotastic@users.noreply.github.com> 2022-06-22 14:58:20 +0200
committerGravatar GitHub <noreply@github.com> 2022-06-22 08:58:20 -0400
commitef6282d5d99a428f7084f7174c9290cb5ad0fa31 (patch)
tree3ade7f1ed32c4be4340b99081f9c6d73da8c06f8
parent46d1ea941b865154732874518858ab3c62112a7b (diff)
downloadastro-ef6282d5d99a428f7084f7174c9290cb5ad0fa31.tar.gz
astro-ef6282d5d99a428f7084f7174c9290cb5ad0fa31.tar.zst
astro-ef6282d5d99a428f7084f7174c9290cb5ad0fa31.zip
Fix `import.meta.env` also without trailing dot (#3675)
Co-authored-by: Matthew Phillips <matthew@skypack.dev>
-rw-r--r--.changeset/tidy-peaches-sit.md5
-rw-r--r--packages/astro/src/vite-plugin-markdown/index.ts4
-rw-r--r--packages/astro/test/astro-markdown.test.js10
-rw-r--r--packages/astro/test/fixtures/astro-markdown/src/pages/vite-env-vars.md6
4 files changed, 20 insertions, 5 deletions
diff --git a/.changeset/tidy-peaches-sit.md b/.changeset/tidy-peaches-sit.md
new file mode 100644
index 000000000..66a971251
--- /dev/null
+++ b/.changeset/tidy-peaches-sit.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fix `import.meta.env` also without trailing dot
diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts
index 431097200..8e0410bd7 100644
--- a/packages/astro/src/vite-plugin-markdown/index.ts
+++ b/packages/astro/src/vite-plugin-markdown/index.ts
@@ -231,9 +231,9 @@ ${tsResult}`;
};
}
-// Converts the first dot in `import.meta.env.` to its Unicode escape sequence,
+// 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.');
+ return code.replace(/import\.meta\.env/g, 'import\\u002Emeta.env');
}
diff --git a/packages/astro/test/astro-markdown.test.js b/packages/astro/test/astro-markdown.test.js
index 0a5c1b2f9..77a2a8fd1 100644
--- a/packages/astro/test/astro-markdown.test.js
+++ b/packages/astro/test/astro-markdown.test.js
@@ -277,14 +277,20 @@ describe('Astro Markdown', () => {
// test 1: referencing an existing var name
expect($('code').eq(0).text()).to.equal('import.meta.env.SITE');
expect($('li').eq(0).text()).to.equal('import.meta.env.SITE');
- expect($('code').eq(2).text()).to.contain('site: import.meta.env.SITE');
+ expect($('code').eq(3).text()).to.contain('site: import.meta.env.SITE');
expect($('blockquote').text()).to.contain('import.meta.env.SITE');
// test 2: referencing a non-existing var name
expect($('code').eq(1).text()).to.equal('import.meta.env.TITLE');
expect($('li').eq(1).text()).to.equal('import.meta.env.TITLE');
- expect($('code').eq(2).text()).to.contain('title: import.meta.env.TITLE');
+ expect($('code').eq(3).text()).to.contain('title: import.meta.env.TITLE');
expect($('blockquote').text()).to.contain('import.meta.env.TITLE');
+
+ // test 3: referencing `import.meta.env` itself (without any var name)
+ expect($('code').eq(2).text()).to.equal('import.meta.env');
+ expect($('li').eq(2).text()).to.equal('import.meta.env');
+ expect($('code').eq(3).text()).to.contain('// Use Vite env vars with import.meta.env');
+ expect($('blockquote').text()).to.match(/import\.meta\.env\s*$/);
});
it('Escapes HTML tags in code blocks', async () => {
diff --git a/packages/astro/test/fixtures/astro-markdown/src/pages/vite-env-vars.md b/packages/astro/test/fixtures/astro-markdown/src/pages/vite-env-vars.md
index 78908cba3..30a9ab177 100644
--- a/packages/astro/test/fixtures/astro-markdown/src/pages/vite-env-vars.md
+++ b/packages/astro/test/fixtures/astro-markdown/src/pages/vite-env-vars.md
@@ -1,5 +1,5 @@
---
-title: Referencing Vite Env Vars like import.meta.env.SITE and import.meta.env.TITLE
+title: Referencing Vite Env Vars like import.meta.env.SITE, import.meta.env.TITLE and import.meta.env
layout: ../layouts/content.astro
---
@@ -9,9 +9,12 @@ You can get the configured site URL with `import.meta.env.SITE`.
The variable `import.meta.env.TITLE` is not configured.
+You can reference all env vars through `import.meta.env`.
+
This should also work outside of code blocks:
- import.meta.env.SITE
- import.meta.env.TITLE
+- import.meta.env
## Usage in fenced code blocks with syntax highlighting
@@ -20,6 +23,7 @@ This should also work outside of code blocks:
import rss from '@astrojs/rss';
export const get = () => rss({
+ // Use Vite env vars with import.meta.env
site: import.meta.env.SITE,
title: import.meta.env.TITLE,
items: import.meta.glob('./**/*.md'),