Skip to content

Commit

Permalink
fix: filter of BOM tags in json plugin (#8628)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenzhenChange authored Jun 17, 2022
1 parent a32c4ba commit e10530b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
6 changes: 2 additions & 4 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
normalizePath,
prettifyUrl,
removeImportQuery,
stripBomTag,
timeFrom,
transformResult,
unwrapId
Expand Down Expand Up @@ -142,10 +143,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const start = performance.now()
await init
let imports: readonly ImportSpecifier[] = []
// strip UTF-8 BOM
if (source.charCodeAt(0) === 0xfeff) {
source = source.slice(1)
}
source = stripBomTag(source)
try {
imports = parseImports(source)[0]
} catch (e: any) {
Expand Down
3 changes: 3 additions & 0 deletions packages/vite/src/node/plugins/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { dataToEsm } from '@rollup/pluginutils'
import { SPECIAL_QUERY_RE } from '../constants'
import type { Plugin } from '../plugin'
import { stripBomTag } from '../utils'

export interface JsonOptions {
/**
Expand Down Expand Up @@ -43,6 +44,8 @@ export function jsonPlugin(
if (!jsonExtRE.test(id)) return null
if (SPECIAL_QUERY_RE.test(id)) return null

json = stripBomTag(json)

try {
if (options.stringify) {
if (isBuild) {
Expand Down
9 changes: 9 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1014,3 +1014,12 @@ export function transformResult(
map: needSourceMap ? s.generateMap({ hires: true, source: id }) : null
}
}

// strip UTF-8 BOM
export function stripBomTag(content: string): string {
if (content.charCodeAt(0) === 0xfeff) {
return content.slice(1)
}

return content
}
6 changes: 6 additions & 0 deletions playground/json/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ <h2>Raw Import</h2>
<h2>JSON Module</h2>
<pre class="module"></pre>

<h2>Has BOM Tag</h2>
<pre class="bom"></pre>

<script type="module">
import json, { hello } from './test.json'
import deepJson, { name } from 'vue/package.json'
Expand Down Expand Up @@ -52,6 +55,9 @@ <h2>JSON Module</h2>
import moduleJSON from 'json-module'
text('.module', JSON.stringify(moduleJSON))

import hasBomJson from './json-bom/has-bom.json'
text('.bom', JSON.stringify(hasBomJson))

function text(sel, text) {
document.querySelector(sel).textContent = text
}
Expand Down
4 changes: 4 additions & 0 deletions playground/json/json-bom/has-bom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "This file is marked with BOM.",
"message": "If the parsing is successful, the BOM tag has been removed."
}

0 comments on commit e10530b

Please sign in to comment.