Fix import.meta.env also without trailing dot (#3675)

Co-authored-by: Matthew Phillips <matthew@skypack.dev>
This commit is contained in:
hippotastic 2022-06-22 14:58:20 +02:00 committed by GitHub
parent 46d1ea941b
commit ef6282d5d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix `import.meta.env` also without trailing dot

View file

@ -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');
}

View file

@ -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 () => {

View file

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