diff --git a/.changeset/tidy-peaches-sit.md b/.changeset/tidy-peaches-sit.md new file mode 100644 index 0000000000..66a971251f --- /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 431097200c..8e0410bd74 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 0a5c1b2f92..77a2a8fd1e 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 78908cba30..30a9ab177a 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'),