You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除Vite构建输出中的polyfill?(已解决)

移除Vite构建输出中的modulepreload Polyfill

问题背景

我的项目构建输出开头包含以下这段polyfill代码:

(function polyfill() {
  const relList = document.createElement("link").relList;
  if (relList && relList.supports && relList.supports("modulepreload")) {
    return;
  }
  for (const link of document.querySelectorAll('link[rel="modulepreload"]')) {
    processPreload(link);

...

    return fetchOpts;
  }
  function processPreload(link) {
    if (link.ep)
      return;
    link.ep = true;
    const fetchOpts = getFetchOpts(link);
    fetch(link.href, fetchOpts);
  }
})();

当前我的vite.config配置如下:

export default {
    esbuild: {
        ignoreAnnotations: true
    },
    plugins: [
        jmxplugin()
    ],
    build: {
        minify: false,
        rollupOptions: {
            output: {
                entryFileNames: `app.js`,
                assetFileNames: `[name].[ext]`
            }
        },
        plugins:[]
    },
}

核心问题:这段polyfill来自哪里?如何在构建输出中移除它?

无效的尝试

我曾尝试在build配置中设置modulePreload: false,修改后的配置如下:

export default {
    esbuild: {
        ignoreAnnotations: true
    },
    plugins: [
        jmxplugin()
    ],
    build: {
        minify: false,
        rollupOptions: {
            output: {
                entryFileNames: `app.js`,
                assetFileNames: `[name].[ext]`
            }
        },
        plugins:[],
        modulePreload: false
    },
}

但这个配置并未生效,构建输出中仍然存在该polyfill。

正确解决方案

最终通过在build配置中添加polyfillModulePreload: false解决了问题,修改后的完整配置如下:

export default {
    esbuild: {
        ignoreAnnotations: true
    },
    plugins: [
        jmxplugin()
    ],
    build: {
        minify: false,
        rollupOptions: {
            output: {
                entryFileNames: `app.js`,
                assetFileNames: `[name].[ext]`
            }
        },
        plugins:[],
        polyfillModulePreload: false
    },
}

这个配置项的作用是完全禁用Vite自动注入的modulepreload polyfill,从而彻底移除构建输出中的这段代码。

内容的提问来源于stack exchange,提问作者citykid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:57:13