Skip to content

Commit

Permalink
refactor: use JSON.parse instead of extractStringLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Jul 24, 2023
1 parent 53cf4e5 commit 233e2bf
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 43 deletions.
44 changes: 2 additions & 42 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import MagicString from 'magic-string'
import colors from 'picocolors'
import type { DefaultTreeAdapterMap, ParserError, Token } from 'parse5'
import { stripLiteral } from 'strip-literal'
import { parse as parseJS } from 'acorn'
import type { Plugin } from '../plugin'
import type { ViteDevServer } from '../server'
import {
Expand Down Expand Up @@ -963,7 +962,8 @@ export function htmlEnvHook(config: ResolvedConfig): IndexHtmlTransformHook {
const val = config.define[key]
if (typeof val === 'string') {
try {
env[key.slice(16)] = extractStringLiteral(val)
const parsed = JSON.parse(val)
env[key.slice(16)] = typeof parsed === 'string' ? parsed : val
} catch {
env[key.slice(16)] = val
}
Expand Down Expand Up @@ -1246,43 +1246,3 @@ function incrementIndent(indent: string = '') {
export function getAttrKey(attr: Token.Attribute): string {
return attr.prefix === undefined ? attr.name : `${attr.prefix}:${attr.name}`
}

function extractStringLiteral(source: string): string {
if (source === '') {
throw new Error(`expected non empty string.`)
}

const node = (
parseJS(source, {
ecmaVersion: 'latest',
sourceType: 'module',
}) as any
).body[0]
if (node.type !== 'ExpressionStatement') {
throw new Error(`expected ExpressionStatement. got ${node.type}`)
}

const exp = node.expression
if (exp.type === 'Literal') {
if (typeof exp.value !== 'string') {
throw new Error(
`expected string literal expession. got ${typeof exp.value} literal expession.`,
)
}
return exp.value
} else if (exp.type === 'TemplateLiteral') {
if (exp.quasis.length !== 1) {
throw new Error(
`expected template literal expession without inline expressions.`,
)
}
const element = exp.quasis[0]
if (element.type !== 'TemplateElement') {
throw new Error(`expected TemplateElement. got ${element.type}`)
}
return element.value.cooked
}
throw new Error(
`expected literal expession or template literal expression. got ${exp.type}`,
)
}
5 changes: 4 additions & 1 deletion playground/html/__tests__/html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,11 @@ describe('env', () => {
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',
'`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()
Expand Down
1 change: 1 addition & 0 deletions playground/html/env.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<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>
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 @@ -36,6 +36,7 @@ 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',
},
Expand Down

0 comments on commit 233e2bf

Please sign in to comment.