From 182795d75ebd767fe67c2585d8537ba1d6c34d7c Mon Sep 17 00:00:00 2001 From: ygj6 Date: Tue, 27 Apr 2021 14:11:02 +0800 Subject: [PATCH 1/2] fix(dev): strip utf-8 bom (#3162) --- packages/vite/src/node/plugins/importAnalysis.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 040558b9f79645..cf83bdb6419e2d 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -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) { From 68788f061037db084e5ca3f923d23f0f7faf8680 Mon Sep 17 00:00:00 2001 From: ygj6 Date: Thu, 29 Apr 2021 16:04:49 +0800 Subject: [PATCH 2/2] fix(dev): add utf8-bom test case --- packages/playground/resolve/__tests__/resolve.spec.ts | 4 ++++ packages/playground/resolve/index.html | 7 +++++++ packages/playground/resolve/utf8-bom/main.js | 3 +++ 3 files changed, 14 insertions(+) create mode 100644 packages/playground/resolve/utf8-bom/main.js diff --git a/packages/playground/resolve/__tests__/resolve.spec.ts b/packages/playground/resolve/__tests__/resolve.spec.ts index 0bb7c238e77893..0b0f1c7fdc8896 100644 --- a/packages/playground/resolve/__tests__/resolve.spec.ts +++ b/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]') }) diff --git a/packages/playground/resolve/index.html b/packages/playground/resolve/index.html index 722d66de5340ff..aada0bd1bdd972 100644 --- a/packages/playground/resolve/index.html +++ b/packages/playground/resolve/index.html @@ -1,5 +1,8 @@

Resolve

+

Utf8-bom import

+

fail

+

Deep import

Should show [2,4]:fail

@@ -62,6 +65,10 @@

resolve.conditions

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' diff --git a/packages/playground/resolve/utf8-bom/main.js b/packages/playground/resolve/utf8-bom/main.js new file mode 100644 index 00000000000000..6273e5ac04e016 --- /dev/null +++ b/packages/playground/resolve/utf8-bom/main.js @@ -0,0 +1,3 @@ +import '@babel/runtime/helpers/esm/slicedToArray' + +export const msg = '[success]'