-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade esbuild and remove dynamic import polyfill
BREAKING CHANGE: update modules target to avoid mixing `import` and `require()` BREAKING CHANGE: remove dynamic import polyfill docs: update browser support docs
- Loading branch information
1 parent
b1e7395
commit 532e2ed
Showing
15 changed files
with
50 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 3 additions & 142 deletions
145
packages/vite/src/node/plugins/dynamicImportPolyfill.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,142 +1,3 @@ | ||
import { ResolvedConfig } from '..' | ||
import { Plugin } from '../plugin' | ||
import { isModernFlag } from './importAnalysisBuild' | ||
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(resolveModulePath(config))});` | ||
|
||
return { | ||
name: 'vite:dynamic-import-polyfill', | ||
resolveId(id) { | ||
if (id === polyfillId) { | ||
return id | ||
} | ||
}, | ||
load(id) { | ||
if (id === polyfillId) { | ||
if (skip) { | ||
return '' | ||
} | ||
polyfillLoaded = true | ||
// return a placeholder here and defer the injection to renderChunk | ||
// so that we can selectively skip the injection based on output format | ||
return polyfillString | ||
} | ||
}, | ||
|
||
renderDynamicImport({ format }) { | ||
if (skip || format !== 'es') { | ||
return null | ||
} | ||
if (!polyfillLoaded) { | ||
throw new Error( | ||
`Vite's dynamic import polyfill is enabled but was never imported. This ` + | ||
`should only happen when using custom non-html rollup inputs. Make ` + | ||
`sure to add \`import "${polyfillId}"\` as the first statement in ` + | ||
`your custom entry.` | ||
) | ||
} | ||
// we do not actually return anything here because rewriting here would | ||
// make it impossible to use es-module-lexer on the rendered chunks, which | ||
// we need for import graph optimization in ./importAnalysisBuild. | ||
} | ||
} | ||
} | ||
|
||
/** | ||
The following polyfill function is meant to run in the browser and adapted from | ||
https://github.com/GoogleChromeLabs/dynamic-import-polyfill | ||
MIT License | ||
Copyright (c) 2018 uupaa and 2019 Google LLC | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
*/ | ||
|
||
declare const self: any | ||
declare const location: any | ||
declare const document: any | ||
declare const URL: any | ||
declare const Blob: any | ||
|
||
function polyfill(modulePath = '.', importFunctionName = '__import__') { | ||
try { | ||
self[importFunctionName] = new Function('u', `return import(u)`) | ||
} catch (error) { | ||
const baseURL = new URL(modulePath, location) | ||
const cleanup = (script: any) => { | ||
URL.revokeObjectURL(script.src) | ||
script.remove() | ||
} | ||
|
||
self[importFunctionName] = (url: string) => | ||
new Promise((resolve, reject) => { | ||
const absURL = new URL(url, baseURL) | ||
|
||
// If the module has already been imported, resolve immediately. | ||
if (self[importFunctionName].moduleMap[absURL]) { | ||
return resolve(self[importFunctionName].moduleMap[absURL]) | ||
} | ||
|
||
const moduleBlob = new Blob( | ||
[ | ||
`import * as m from '${absURL}';`, | ||
`${importFunctionName}.moduleMap['${absURL}']=m;` | ||
], | ||
{ type: 'text/javascript' } | ||
) | ||
|
||
const script = Object.assign(document.createElement('script'), { | ||
type: 'module', | ||
src: URL.createObjectURL(moduleBlob), | ||
onerror() { | ||
reject(new Error(`Failed to import: ${url}`)) | ||
cleanup(script) | ||
}, | ||
onload() { | ||
resolve(self[importFunctionName].moduleMap[absURL]) | ||
cleanup(script) | ||
} | ||
}) | ||
|
||
document.head.appendChild(script) | ||
}) | ||
|
||
self[importFunctionName].moduleMap = {} | ||
} | ||
} | ||
throw new Error( | ||
`Vite's dynamic import polyfill has been removed. Please install and import https://github.com/GoogleChromeLabs/dynamic-import-polyfill instead.` | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.