From 8a2a740f437703a95ff69cec7c64e85f24a3de5d Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 14 Jul 2022 16:46:41 +0800 Subject: [PATCH 1/3] fix(esbuild): always support dynamic import and import meta --- packages/vite/src/node/plugins/esbuild.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 42e5bbac76316a..4953d73566effc 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -298,10 +298,19 @@ export function resolveEsbuildTranspileOptions( // pure annotations and break tree-shaking // https://github.com/vuejs/core/issues/2860#issuecomment-926882793 const isEsLibBuild = config.build.lib && format === 'es' + const esbuildOptions = config.esbuild || {} const options: TransformOptions = { - ...config.esbuild, + ...esbuildOptions, target: target || undefined, - format: rollupToEsbuildFormatMap[format] + format: rollupToEsbuildFormatMap[format], + // the final build should always support dynamic import and import.meta. + // if they need to be polyfilled, plugin-legacy should be used. + // plugin-legacy detects these two features when checking for modern code. + supported: { + 'dynamic-import': true, + 'import-meta': true, + ...esbuildOptions.supported + } } // If no minify, disable all minify options From e70679cd6574fc04d15675815096795aa8b4bbab Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 14 Jul 2022 17:01:25 +0800 Subject: [PATCH 2/3] chore: add tests --- .../build-old/__tests__/build-old.spec.ts | 11 +++++++++++ playground/build-old/dynamic.js | 1 + playground/build-old/index.html | 19 +++++++++++++++++++ playground/build-old/package.json | 11 +++++++++++ playground/build-old/vite.config.js | 8 ++++++++ 5 files changed, 50 insertions(+) create mode 100644 playground/build-old/__tests__/build-old.spec.ts create mode 100644 playground/build-old/dynamic.js create mode 100644 playground/build-old/index.html create mode 100644 playground/build-old/package.json create mode 100644 playground/build-old/vite.config.js diff --git a/playground/build-old/__tests__/build-old.spec.ts b/playground/build-old/__tests__/build-old.spec.ts new file mode 100644 index 00000000000000..085d1bbe75b986 --- /dev/null +++ b/playground/build-old/__tests__/build-old.spec.ts @@ -0,0 +1,11 @@ +import { describe, test } from 'vitest' +import { page } from '~utils' + +describe('syntax preserve', () => { + test('import.meta.url', async () => { + expect(await page.textContent('.import-meta-url')).toBe('string') + }) + test('dynamic import', async () => { + expect(await page.textContent('.dynamic-import')).toBe('success') + }) +}) diff --git a/playground/build-old/dynamic.js b/playground/build-old/dynamic.js new file mode 100644 index 00000000000000..739bb26e01b765 --- /dev/null +++ b/playground/build-old/dynamic.js @@ -0,0 +1 @@ +export default 'success' diff --git a/playground/build-old/index.html b/playground/build-old/index.html new file mode 100644 index 00000000000000..118332ac0a42fc --- /dev/null +++ b/playground/build-old/index.html @@ -0,0 +1,19 @@ +

Build Old

+ +

import meta url

+

+ +

dynamic import

+

+ + diff --git a/playground/build-old/package.json b/playground/build-old/package.json new file mode 100644 index 00000000000000..bb7f1b9b861538 --- /dev/null +++ b/playground/build-old/package.json @@ -0,0 +1,11 @@ +{ + "name": "test-build-old", + "private": true, + "version": "0.0.0", + "scripts": { + "dev": "vite", + "build": "vite build", + "debug": "node --inspect-brk ../../packages/vite/bin/vite", + "preview": "vite preview" + } +} diff --git a/playground/build-old/vite.config.js b/playground/build-old/vite.config.js new file mode 100644 index 00000000000000..aa85af55972eb1 --- /dev/null +++ b/playground/build-old/vite.config.js @@ -0,0 +1,8 @@ +import { defineConfig } from 'vite' + +export default defineConfig({ + build: { + // old browsers only + target: ['chrome60'] + } +}) From a658fe2d4bc0ae002c213f860322fe857dfa8fd7 Mon Sep 17 00:00:00 2001 From: bluwy Date: Thu, 14 Jul 2022 17:09:17 +0800 Subject: [PATCH 3/3] chore: fix unit test --- .../node/__tests__/plugins/esbuild.spec.ts | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts index 5f063301799cff..d1c98348c5c453 100644 --- a/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts +++ b/packages/vite/src/node/__tests__/plugins/esbuild.spec.ts @@ -21,7 +21,11 @@ describe('resolveEsbuildTranspileOptions', () => { format: 'esm', keepNames: true, minify: true, - treeShaking: true + treeShaking: true, + supported: { + 'dynamic-import': true, + 'import-meta': true + } }) }) @@ -62,7 +66,11 @@ describe('resolveEsbuildTranspileOptions', () => { minifyIdentifiers: false, minifySyntax: true, minifyWhitespace: true, - treeShaking: true + treeShaking: true, + supported: { + 'dynamic-import': true, + 'import-meta': true + } }) }) @@ -87,7 +95,11 @@ describe('resolveEsbuildTranspileOptions', () => { minifyIdentifiers: false, minifySyntax: false, minifyWhitespace: false, - treeShaking: false + treeShaking: false, + supported: { + 'dynamic-import': true, + 'import-meta': true + } }) }) @@ -114,7 +126,11 @@ describe('resolveEsbuildTranspileOptions', () => { minifyIdentifiers: true, minifySyntax: true, minifyWhitespace: false, - treeShaking: true + treeShaking: true, + supported: { + 'dynamic-import': true, + 'import-meta': true + } }) }) @@ -138,7 +154,11 @@ describe('resolveEsbuildTranspileOptions', () => { format: 'cjs', keepNames: true, minify: true, - treeShaking: true + treeShaking: true, + supported: { + 'dynamic-import': true, + 'import-meta': true + } }) }) @@ -167,7 +187,11 @@ describe('resolveEsbuildTranspileOptions', () => { minifyIdentifiers: true, minifySyntax: true, minifyWhitespace: false, - treeShaking: true + treeShaking: true, + supported: { + 'dynamic-import': true, + 'import-meta': true + } }) }) @@ -197,7 +221,11 @@ describe('resolveEsbuildTranspileOptions', () => { minifyIdentifiers: true, minifySyntax: false, minifyWhitespace: true, - treeShaking: true + treeShaking: true, + supported: { + 'dynamic-import': true, + 'import-meta': true + } }) }) })