Skip to content
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

fix(html): support import.meta.env define replacement without quotes #13425

Merged
merged 6 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,11 +955,21 @@ export function htmlEnvHook(config: ResolvedConfig): IndexHtmlTransformHook {
const pattern = /%(\S+?)%/g
const envPrefix = resolveEnvPrefix({ envPrefix: config.envPrefix })
const env: Record<string, any> = { ...config.env }

// account for user env defines
for (const key in config.define) {
if (key.startsWith(`import.meta.env.`)) {
const val = config.define[key]
env[key.slice(16)] = typeof val === 'string' ? val : JSON.stringify(val)
if (typeof val === 'string') {
try {
const parsed = JSON.parse(val)
env[key.slice(16)] = typeof parsed === 'string' ? parsed : val
Comment on lines +965 to +966
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally forgot about this PR 😅
I replaced extractStringLiteral with this one.
The typeof parsed === 'string' thing is to avoid a breaking change when define has something like 'import.meta.env.VITE_OBJECT_STRING': '{ "foo": "bar" }'.

} catch {
env[key.slice(16)] = val
}
} else {
env[key.slice(16)] = JSON.stringify(val)
}
}
}
return (html, ctx) => {
Expand Down
8 changes: 8 additions & 0 deletions playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ describe('env', () => {
test('env works', async () => {
expect(await page.textContent('.env')).toBe('bar')
expect(await page.textContent('.env-define')).toBe('5173')
expect(await page.textContent('.env-define-string')).toBe('string')
expect(await page.textContent('.env-define-object-string')).toBe(
'{ "foo": "bar" }',
)
expect(await page.textContent('.env-define-template-literal')).toBe(
'`template literal`', // only double quotes will be unquoted
)
expect(await page.textContent('.env-define-null-string')).toBe('null')
expect(await page.textContent('.env-bar')).toBeTruthy()
expect(await page.textContent('.env-prod')).toBe(isBuild + '')
expect(await page.textContent('.env-dev')).toBe(isServe + '')
Expand Down
4 changes: 4 additions & 0 deletions playground/html/env.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<p class="env">%VITE_FOO%</p>
<p class="env-define">%VITE_NUMBER%</p>
<p class="env-define-string">%VITE_STRING%</p>
<p class="env-define-object-string">%VITE_OBJECT_STRING%</p>
<p class="env-define-template-literal">%VITE_TEMPLATE_LITERAL%</p>
<p class="env-define-null-string">%VITE_NULL_STRING%</p>
<p class="env-%VITE_FOO%">class name should be env-bar</p>
<p class="env-prod">%PROD%</p>
<p class="env-dev">%DEV%</p>
Expand Down
4 changes: 4 additions & 0 deletions playground/html/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export default defineConfig({

define: {
'import.meta.env.VITE_NUMBER': 5173,
'import.meta.env.VITE_STRING': JSON.stringify('string'),
'import.meta.env.VITE_OBJECT_STRING': '{ "foo": "bar" }',
'import.meta.env.VITE_TEMPLATE_LITERAL': '`template literal`',
'import.meta.env.VITE_NULL_STRING': 'null',
},

plugins: [
Expand Down