Skip to content

refactor(modulePreloadPolyfill): move polyfill code to own file #19737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ exports[`load > doesn't load modulepreload polyfill when format is cjs 1`] = `
`;

exports[`load > loads modulepreload polyfill 1`] = `
"(function polyfill() {
"(function modulePreloadPolyfill() {
const relList = document.createElement("link").relList;
if (relList && relList.supports && relList.supports("modulepreload")) {
return;
}
for (const link of document.querySelectorAll('link[rel="modulepreload"]')) {
for (const link of Array.from(
document.querySelectorAll('link[rel="modulepreload"]')
)) {
processPreload(link);
}
new MutationObserver((mutations) => {
Expand Down
39 changes: 39 additions & 0 deletions packages/vite/src/node/plugins/modulePreloadPolyfill/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { ResolvedConfig } from '../..'
import type { Plugin } from '../../plugin'
import { isModernFlag } from '../importAnalysisBuild'
import { modulePreloadPolyfill } from './modulePreloadPolyfill'

export const modulePreloadPolyfillId = 'vite/modulepreload-polyfill'
const resolvedModulePreloadPolyfillId = '\0' + modulePreloadPolyfillId + '.js'

export function modulePreloadPolyfillPlugin(config: ResolvedConfig): Plugin {
let polyfillString: string | undefined

return {
name: 'vite:modulepreload-polyfill',
resolveId: {
handler(id) {
if (id === modulePreloadPolyfillId) {
return resolvedModulePreloadPolyfillId
}
},
},
load: {
handler(id) {
if (id === resolvedModulePreloadPolyfillId) {
// `isModernFlag` is only available during build since it is resolved by `vite:build-import-analysis`
if (
config.command !== 'build' ||
this.environment.config.consumer !== 'client'
) {
return ''
}
if (!polyfillString) {
polyfillString = `${isModernFlag}&&(${modulePreloadPolyfill.toString()}());`
}
return { code: polyfillString, moduleSideEffects: true }
}
},
},
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,4 @@
import type { ResolvedConfig } from '..'
import type { Plugin } from '../plugin'
import { isModernFlag } from './importAnalysisBuild'

export const modulePreloadPolyfillId = 'vite/modulepreload-polyfill'
const resolvedModulePreloadPolyfillId = '\0' + modulePreloadPolyfillId + '.js'

export function modulePreloadPolyfillPlugin(config: ResolvedConfig): Plugin {
let polyfillString: string | undefined

return {
name: 'vite:modulepreload-polyfill',
resolveId: {
handler(id) {
if (id === modulePreloadPolyfillId) {
return resolvedModulePreloadPolyfillId
}
},
},
load: {
handler(id) {
if (id === resolvedModulePreloadPolyfillId) {
// `isModernFlag` is only available during build since it is resolved by `vite:build-import-analysis`
if (
config.command !== 'build' ||
this.environment.config.consumer !== 'client'
) {
return ''
}
if (!polyfillString) {
polyfillString = `${isModernFlag}&&(${polyfill.toString()}());`
}
return { code: polyfillString, moduleSideEffects: true }
}
},
},
}
}
/// <reference lib="dom" />

/**
The following polyfill function is meant to run in the browser and adapted from
Expand All @@ -58,17 +21,15 @@ 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 document: any
declare const MutationObserver: any
declare const fetch: any

function polyfill() {
export function modulePreloadPolyfill(): void {
const relList = document.createElement('link').relList
if (relList && relList.supports && relList.supports('modulepreload')) {
return
}

for (const link of document.querySelectorAll('link[rel="modulepreload"]')) {
for (const link of Array.from(
document.querySelectorAll('link[rel="modulepreload"]'),
)) {
processPreload(link)
}

Expand Down