Skip to content

Commit

Permalink
Fix import.meta.env also without trailing dot (withastro#3675)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Phillips <[email protected]>
  • Loading branch information
hippotastic and matthewp authored Jun 22, 2022
1 parent 7c00315 commit 530deaf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
10 changes: 8 additions & 2 deletions test/astro-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/astro-markdown/src/pages/vite-env-vars.md
Original file line number Diff line number Diff line change
@@ -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
---

Expand All @@ -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

Expand All @@ -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'),
Expand Down

0 comments on commit 530deaf

Please sign in to comment.