Skip to content

Commit

Permalink
fix: dymamic import polyfill path when base is a URL (#3132)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjxenjoy committed Apr 25, 2021
1 parent 0e7125a commit 02ba4ba
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/vite/src/node/plugins/dynamicImportPolyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ import path from 'path'

export const polyfillId = 'vite/dynamic-import-polyfill'

function resolveModulePath(config: ResolvedConfig) {
const {
base,
build: { assetsDir }
} = config
// #2918 path.posix.join returns a wrong path when config.base is a URL
if (/^(https?:)?(\/\/)/i.test(base)) {
return `${base.replace(/\/$/, '')}/${assetsDir}/`
}
return path.posix.join(base, assetsDir, '/')
}

export function dynamicImportPolyfillPlugin(config: ResolvedConfig): Plugin {
const skip = config.command === 'serve' || config.build.ssr
let polyfillLoaded = false
const polyfillString =
`const p = ${polyfill.toString()};` +
`${isModernFlag}&&p(${JSON.stringify(
path.posix.join(config.base, config.build.assetsDir, '/')
)});`
`${isModernFlag}&&p(${JSON.stringify(resolveModulePath(config))});`

return {
name: 'vite:dynamic-import-polyfill',
Expand Down

0 comments on commit 02ba4ba

Please sign in to comment.