/
dynamicImportPolyfill.ts
144 lines (130 loc) · 4.93 KB
/
dynamicImportPolyfill.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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 enabled = config.build.polyfillDynamicImport
const skip = !enabled || config.command === 'serve' || config.build.ssr
let polyfillString: string | undefined
return {
name: 'vite:dynamic-import-polyfill',
resolveId(id) {
if (id === polyfillId) {
return id
}
},
load(id) {
if (id === polyfillId) {
if (!enabled) {
config.logger.warnOnce(
`\n'vite/dynamic-import-polyfill' is no longer needed if you target modern browsers`
)
}
if (skip) {
return ''
}
// return a placeholder here and defer the injection to renderChunk
// so that we can selectively skip the injection based on output format
if (!polyfillString) {
polyfillString =
`const p = ${polyfill.toString()};` +
`${isModernFlag}&&p(${JSON.stringify(resolveModulePath(config))});`
}
return polyfillString
}
},
renderDynamicImport({ format }) {
if (skip || format !== 'es') {
return null
}
if (!polyfillString) {
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 = {}
}
}