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(dev): strip utf-8 bom (#3162) #3171

Merged
merged 2 commits into from May 1, 2021
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
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