Skip to content

Commit

Permalink
fix(dev): strip utf-8 bom (vitejs#3162) (vitejs#3171)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 authored and TobiasMelen committed May 3, 2021
1 parent c2b7a64 commit f5ec42e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/playground/resolve/__tests__/resolve.spec.ts
@@ -1,5 +1,9 @@
import { isBuild } from '../../testUtils'

test('bom import', async () => {
expect(await page.textContent('.utf8-bom')).toMatch('[success]')
})

test('deep import', async () => {
expect(await page.textContent('.deep-import')).toMatch('[2,4]')
})
Expand Down
7 changes: 7 additions & 0 deletions packages/playground/resolve/index.html
@@ -1,5 +1,8 @@
<h1>Resolve</h1>

<h2>Utf8-bom import</h2>
<p class="utf8-bom">fail</p>

<h2>Deep import</h2>
<p>Should show [2,4]:<span class="pre deep-import">fail</span></p>

Expand Down Expand Up @@ -62,6 +65,10 @@ <h2>resolve.conditions</h2>
document.querySelector(selector).textContent = text
}

// import from a utf-8 bom file
import { msg as bomMsg } from './utf8-bom/main.js'
text('.utf8-bom', bomMsg)

// deep import
import slicedToArray from '@babel/runtime/helpers/esm/slicedToArray'

Expand Down
3 changes: 3 additions & 0 deletions packages/playground/resolve/utf8-bom/main.js
@@ -0,0 +1,3 @@
import '@babel/runtime/helpers/esm/slicedToArray'

export const msg = '[success]'
4 changes: 4 additions & 0 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -112,6 +112,10 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const rewriteStart = Date.now()
await init
let imports: readonly ImportSpecifier[] = []
// strip UTF-8 BOM
if (source.charCodeAt(0) === 0xfeff) {
source = source.slice(1)
}
try {
imports = parseImports(source)[0]
} catch (e) {
Expand Down

0 comments on commit f5ec42e

Please sign in to comment.