Skip to content

Commit

Permalink
fix(html): support import.meta.env define replacement without quotes (
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Jul 24, 2023
1 parent 4848e41 commit 883089c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
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 @@ -963,11 +963,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
} 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 @@ -294,6 +294,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 @@ -36,6 +36,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

0 comments on commit 883089c

Please sign in to comment.