Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-legacy): force set build.target #10072

Merged
merged 3 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/plugin-legacy/build.config.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.