Skip to content

Commit

Permalink
feat: fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jun 13, 2023
1 parent 0c0eca9 commit 53cf4e5
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 15 deletions.
17 changes: 2 additions & 15 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,6 @@ export function htmlEnvHook(config: ResolvedConfig): IndexHtmlTransformHook {
const pattern = /%(\S+?)%/g
const envPrefix = resolveEnvPrefix({ envPrefix: config.envPrefix })
const env: Record<string, any> = { ...config.env }
const ignoredImportMetaEnvKeys = new Set()

// account for user env defines
for (const key in config.define) {
Expand All @@ -966,7 +965,7 @@ export function htmlEnvHook(config: ResolvedConfig): IndexHtmlTransformHook {
try {
env[key.slice(16)] = extractStringLiteral(val)
} catch {
ignoredImportMetaEnvKeys.add(key.slice(16))
env[key.slice(16)] = val
}
} else {
env[key.slice(16)] = JSON.stringify(val)
Expand All @@ -978,19 +977,7 @@ export function htmlEnvHook(config: ResolvedConfig): IndexHtmlTransformHook {
if (key in env) {
return env[key]
} else {
if (ignoredImportMetaEnvKeys.has(key)) {
const relativeHtml = normalizePath(
path.relative(config.root, ctx.filename),
)
config.logger.warn(
colors.yellow(
colors.bold(
`(!) ${text} was found in /${relativeHtml} but was not replaced. ` +
`Only quoted strings and non-string values can be used for HTML Env Replacement.`,
),
),
)
} else if (envPrefix.some((prefix) => key.startsWith(prefix))) {
if (envPrefix.some((prefix) => key.startsWith(prefix))) {
const relativeHtml = normalizePath(
path.relative(config.root, ctx.filename),
)
Expand Down
1 change: 1 addition & 0 deletions playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ describe('env', () => {
expect(await page.textContent('.env-define-template-literal')).toBe(
'template literal',
)
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
1 change: 1 addition & 0 deletions playground/html/env.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<p class="env-define">%VITE_NUMBER%</p>
<p class="env-define-string">%VITE_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
1 change: 1 addition & 0 deletions playground/html/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default defineConfig({
'import.meta.env.VITE_NUMBER': 5173,
'import.meta.env.VITE_STRING': JSON.stringify('string'),
'import.meta.env.VITE_TEMPLATE_LITERAL': '`template literal`',
'import.meta.env.VITE_NULL_STRING': 'null',
},

plugins: [
Expand Down

0 comments on commit 53cf4e5

Please sign in to comment.