mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 14:48:41 +00:00
fix: filter of BOM tags in json plugin (#8628)
This commit is contained in:
parent
a32c4bad5f
commit
e10530ba2c
@ -36,6 +36,7 @@ import {
|
||||
normalizePath,
|
||||
prettifyUrl,
|
||||
removeImportQuery,
|
||||
stripBomTag,
|
||||
timeFrom,
|
||||
transformResult,
|
||||
unwrapId
|
||||
@ -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) {
|
||||
|
@ -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 {
|
||||
/**
|
||||
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -22,6 +22,9 @@
|
||||
<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'
|
||||
@ -52,6 +55,9 @@
|
||||
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
|
||||
}
|
||||
|
4
playground/json/json-bom/has-bom.json
Normal file
4
playground/json/json-bom/has-bom.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"description": "This file is marked with BOM.",
|
||||
"message": "If the parsing is successful, the BOM tag has been removed."
|
||||
}
|
Loading…
Reference in New Issue
Block a user