Skip to content

Commit 209caf2

Browse files
committed
refactor(modulePreloadPolyfill): move polyfill code to own file
1 parent 35ee848 commit 209caf2

File tree

2 files changed

+44
-44
lines changed

2 files changed

+44
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { ResolvedConfig } from '../..'
2+
import type { Plugin } from '../../plugin'
3+
import { isModernFlag } from '../importAnalysisBuild'
4+
import { modulePreloadPolyfill } from './modulePreloadPolyfill'
5+
6+
export const modulePreloadPolyfillId = 'vite/modulepreload-polyfill'
7+
const resolvedModulePreloadPolyfillId = '\0' + modulePreloadPolyfillId + '.js'
8+
9+
export function modulePreloadPolyfillPlugin(config: ResolvedConfig): Plugin {
10+
let polyfillString: string | undefined
11+
12+
return {
13+
name: 'vite:modulepreload-polyfill',
14+
resolveId: {
15+
handler(id) {
16+
if (id === modulePreloadPolyfillId) {
17+
return resolvedModulePreloadPolyfillId
18+
}
19+
},
20+
},
21+
load: {
22+
handler(id) {
23+
if (id === resolvedModulePreloadPolyfillId) {
24+
// `isModernFlag` is only available during build since it is resolved by `vite:build-import-analysis`
25+
if (
26+
config.command !== 'build' ||
27+
this.environment.config.consumer !== 'client'
28+
) {
29+
return ''
30+
}
31+
if (!polyfillString) {
32+
polyfillString = `${isModernFlag}&&(${modulePreloadPolyfill.toString()}());`
33+
}
34+
return { code: polyfillString, moduleSideEffects: true }
35+
}
36+
},
37+
},
38+
}
39+
}

packages/vite/src/node/plugins/modulePreloadPolyfill.ts renamed to packages/vite/src/node/plugins/modulePreloadPolyfill/modulePreloadPolyfill.ts

+5-44
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,4 @@
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" />
392

403
/**
414
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,
5821
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
5922
*/
6023

61-
declare const document: any
62-
declare const MutationObserver: any
63-
declare const fetch: any
64-
65-
function polyfill() {
24+
export function modulePreloadPolyfill(): void {
6625
const relList = document.createElement('link').relList
6726
if (relList && relList.supports && relList.supports('modulepreload')) {
6827
return
6928
}
7029

71-
for (const link of document.querySelectorAll('link[rel="modulepreload"]')) {
30+
for (const link of Array.from(
31+
document.querySelectorAll('link[rel="modulepreload"]'),
32+
)) {
7233
processPreload(link)
7334
}
7435

0 commit comments

Comments
 (0)