Skip to content

Commit

Permalink
refactor: hoist regex
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Nov 1, 2024
1 parent 4f3770e commit 0036f23
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface JsonOptions {
// Custom json filter for vite
const jsonExtRE = /\.json(?:$|\?)(?!commonjs-(?:proxy|external))/

const jsonObjRE = /^\s*\{/

const jsonLangs = `\\.(?:json|json5)(?:$|\\?)`
const jsonLangRE = new RegExp(jsonLangs)
export const isJSONRequest = (request: string): boolean =>
Expand All @@ -49,7 +51,7 @@ export function jsonPlugin(

try {
if (options.stringify !== false) {
if (options.namedExports && /^\s*\{/.test(json)) {
if (options.namedExports && jsonObjRE.test(json)) {
const parsed = JSON.parse(json)
const keys = Object.keys(parsed)

Expand Down

0 comments on commit 0036f23

Please sign in to comment.