1
- import type { ResolvedConfig } from '..'
2
- import type { Plugin } from '../plugin'
3
- import { isModernFlag } from './importAnalysisBuild'
4
-
5
- export const modulePreloadPolyfillId = 'vite/modulepreload-polyfill'
6
- const resolvedModulePreloadPolyfillId = '\0' + modulePreloadPolyfillId + '.js'
7
-
8
- export function modulePreloadPolyfillPlugin ( config : ResolvedConfig ) : Plugin {
9
- let polyfillString : string | undefined
10
-
11
- return {
12
- name : 'vite:modulepreload-polyfill' ,
13
- resolveId : {
14
- handler ( id ) {
15
- if ( id === modulePreloadPolyfillId ) {
16
- return resolvedModulePreloadPolyfillId
17
- }
18
- } ,
19
- } ,
20
- load : {
21
- handler ( id ) {
22
- if ( id === resolvedModulePreloadPolyfillId ) {
23
- // `isModernFlag` is only available during build since it is resolved by `vite:build-import-analysis`
24
- if (
25
- config . command !== 'build' ||
26
- this . environment . config . consumer !== 'client'
27
- ) {
28
- return ''
29
- }
30
- if ( ! polyfillString ) {
31
- polyfillString = `${ isModernFlag } &&(${ polyfill . toString ( ) } ());`
32
- }
33
- return { code : polyfillString , moduleSideEffects : true }
34
- }
35
- } ,
36
- } ,
37
- }
38
- }
1
+ /// <reference lib="dom" />
39
2
40
3
/**
41
4
The following polyfill function is meant to run in the browser and adapted from
@@ -58,17 +21,15 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
59
22
*/
60
23
61
- declare const document : any
62
- declare const MutationObserver : any
63
- declare const fetch : any
64
-
65
- function polyfill ( ) {
24
+ export function modulePreloadPolyfill ( ) : void {
66
25
const relList = document . createElement ( 'link' ) . relList
67
26
if ( relList && relList . supports && relList . supports ( 'modulepreload' ) ) {
68
27
return
69
28
}
70
29
71
- for ( const link of document . querySelectorAll ( 'link[rel="modulepreload"]' ) ) {
30
+ for ( const link of Array . from (
31
+ document . querySelectorAll ( 'link[rel="modulepreload"]' ) ,
32
+ ) ) {
72
33
processPreload ( link )
73
34
}
74
35
0 commit comments