fix: filter of BOM tags in json plugin (#8628)

This commit is contained in:
zhenzhenChange 2022-06-17 15:20:57 +08:00 committed by GitHub
parent a32c4bad5f
commit e10530ba2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 4 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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
}

View File

@ -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
}

View File

@ -0,0 +1,4 @@
{
"description": "This file is marked with BOM.",
"message": "If the parsing is successful, the BOM tag has been removed."
}