Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: json plugin add judgment and filtering of BOM tags #8628

Merged
merged 6 commits into from Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/vite/src/node/plugins/importAnalysis.ts
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
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
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
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
@@ -0,0 +1,4 @@
{
"description": "This file is marked with BOM.",
"message": "If the parsing is successful, the BOM tag has been removed."
}