From a13a7eb4165d38ce0ab6eefd4e4d38104ce63699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sun, 18 Sep 2022 23:09:43 +0900 Subject: [PATCH] fix(plugin-legacy): force set `build.target` (#10072) --- 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 6ef18878d2e080..077e3604151b8d 100644 --- a/packages/plugin-legacy/package.json +++ b/packages/plugin-legacy/package.json @@ -47,6 +47,7 @@ }, "devDependencies": { "@babel/core": "^7.19.0", + "picocolors": "^1.0.0", "vite": "workspace:*" } } diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 4d51d1cecaf493..1e9850f8127b9f 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 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 + 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' as an option to this plugin with the list of legacy browsers to support instead.` + ) + ) + } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c13b7b003ad21e..837648b0bc6362 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -138,6 +138,7 @@ importers: '@babel/standalone': ^7.19.0 core-js: ^3.25.1 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.19.0 + picocolors: 1.0.0 vite: link:../vite packages/plugin-react: