From 17fce4d8f418908a032c144f5e8ba8e583a85a49 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 11 Sep 2022 22:30:09 +0900 Subject: [PATCH 1/2] fix(plugin-legacy): force set `build.target` --- packages/plugin-legacy/build.config.ts | 3 ++- packages/plugin-legacy/package.json | 1 + packages/plugin-legacy/src/index.ts | 23 +++++++++++++++++++++++ pnpm-lock.yaml | 2 ++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/plugin-legacy/build.config.ts b/packages/plugin-legacy/build.config.ts index 6dcf1a5a2dc0b3..12afbbc54d1efd 100644 --- a/packages/plugin-legacy/build.config.ts +++ b/packages/plugin-legacy/build.config.ts @@ -5,6 +5,7 @@ export default defineBuildConfig({ clean: true, declaration: true, rollup: { - emitCJS: true + emitCJS: true, + inlineDependencies: true } }) diff --git a/packages/plugin-legacy/package.json b/packages/plugin-legacy/package.json index 0696a08e6d0075..81482b509a2c1e 100644 --- a/packages/plugin-legacy/package.json +++ b/packages/plugin-legacy/package.json @@ -47,6 +47,7 @@ }, "devDependencies": { "@babel/core": "^7.18.13", + "picocolors": "^1.0.0", "vite": "workspace:*" } } diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 4d51d1cecaf493..24a0ef4f50e8d4 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -19,6 +19,7 @@ import type { RenderedChunk } from 'rollup' import type { PluginItem as BabelPlugin } from '@babel/core' +import colors from 'picocolors' import type { Options } from './types' // lazy load babel since it's not used during dev @@ -156,6 +157,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { }) } + let overriddenBuildTarget = false const legacyConfigPlugin: Plugin = { name: 'vite:legacy-config', @@ -173,6 +175,18 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { // So targeting `chrome61` suffices to fix the compatibility issue. config.build.cssTarget = 'chrome61' } + + // Vite's default target browser is **not** the same. + // See https://github.com/vitejs/vite/pull/10052#issuecomment-1242076461 + overriddenBuildTarget = config.build.target !== undefined + // browsers supporting ESM + dynamic import + import.meta + config.build.target = [ + 'es2020', + 'edge79', + 'firefox67', + 'chrome64', + 'safari11.1' + ] } return { @@ -183,6 +197,15 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { : legacyEnvVarMarker } } + }, + configResolved(config) { + if (overriddenBuildTarget) { + config.logger.warn( + colors.yellow( + `plugin-legacy overrode 'build.target'. You should pass 'targets' option to legacy browser instead.` + ) + ) + } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ca8db4b26b25a4..067ce3335e4593 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -138,6 +138,7 @@ importers: '@babel/standalone': ^7.18.13 core-js: ^3.25.0 magic-string: ^0.26.3 + picocolors: ^1.0.0 regenerator-runtime: ^0.13.9 systemjs: ^6.12.6 vite: workspace:* @@ -149,6 +150,7 @@ importers: systemjs: 6.12.6 devDependencies: '@babel/core': 7.18.13 + picocolors: 1.0.0 vite: link:../vite packages/plugin-react: From 740fe11674917784fcc39104b56737e639bd5c6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Mon, 12 Sep 2022 11:53:11 +0900 Subject: [PATCH 2/2] chore: fix wordings Co-authored-by: patak --- packages/plugin-legacy/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 24a0ef4f50e8d4..1e9850f8127b9f 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -176,7 +176,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { config.build.cssTarget = 'chrome61' } - // Vite's default target browser is **not** the same. + // Vite's default target browsers are **not** the same. // See https://github.com/vitejs/vite/pull/10052#issuecomment-1242076461 overriddenBuildTarget = config.build.target !== undefined // browsers supporting ESM + dynamic import + import.meta @@ -202,7 +202,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { if (overriddenBuildTarget) { config.logger.warn( colors.yellow( - `plugin-legacy overrode 'build.target'. You should pass 'targets' option to legacy browser instead.` + `plugin-legacy overrode 'build.target'. You should pass 'targets' as an option to this plugin with the list of legacy browsers to support instead.` ) ) }