Skip to content

Commit

Permalink
fix(plugin-legacy): force set build.target (#10072)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Sep 18, 2022
1 parent 414d2ef commit a13a7eb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/plugin-legacy/build.config.ts
Expand Up @@ -5,6 +5,7 @@ export default defineBuildConfig({
clean: true,
declaration: true,
rollup: {
emitCJS: true
emitCJS: true,
inlineDependencies: true
}
})
1 change: 1 addition & 0 deletions packages/plugin-legacy/package.json
Expand Up @@ -47,6 +47,7 @@
},
"devDependencies": {
"@babel/core": "^7.19.0",
"picocolors": "^1.0.0",
"vite": "workspace:*"
}
}
23 changes: 23 additions & 0 deletions packages/plugin-legacy/src/index.ts
Expand Up @@ -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
Expand Down Expand Up @@ -156,6 +157,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
})
}

let overriddenBuildTarget = false
const legacyConfigPlugin: Plugin = {
name: 'vite:legacy-config',

Expand All @@ -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 {
Expand All @@ -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.`
)
)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a13a7eb

Please sign in to comment.