Skip to content
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

modulepreload polyfill ends up in vendor chunk #16429

Closed
7 tasks done
stefan-schweiger opened this issue Apr 15, 2024 · 4 comments
Closed
7 tasks done

modulepreload polyfill ends up in vendor chunk #16429

stefan-schweiger opened this issue Apr 15, 2024 · 4 comments

Comments

@stefan-schweiger
Copy link

Describe the bug

I'm using vite with react and I want to extract a vendor library in a separate chunk. For whatever reason the modulepreload polyfill always ends up in the vendor chunk and the chunk itself is always preloaded in index.html.

I have created a very small reproduction which only has the following vite config:

export default defineConfig({
  plugins: [react()],
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          jspdf: ['jspdf']
        }
      }
    }
  }
})

My code does not even import jspdf in any way directly, but as soon as I put it as a manual chunk the following output is created:

image

As you can see there really only is the modulepreload code but no vendor code. I have also tried to disable the polyfill altogether with build.modulePreload.polyfill set to false, but this does still not disable the code generation.

Reproduction

https://stackblitz.com/edit/vitejs-vite-evfakv?file=src%2FApp.tsx,vite.config.ts,dist%2Fassets%2Fjspdf-PNuX44LO.js&terminal=dev

Steps to reproduce

  1. npm run build
  2. dist/assets/jspdf-*.js contains the module preload polyfill code
  3. index.html always preloads dist/assets/jspdf-*.js

System Info

System:
    OS: macOS 14.4.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 174.28 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.7.2 - /opt/homebrew/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.5.0 - /opt/homebrew/bin/npm
    Watchman: 2024.04.01.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 122.0.6261.112
    Safari: 17.4.1
  npmPackages:
    @vitejs/plugin-react: 4.2.1 => 4.2.1 
    vite: 5.2.8 => 5.2.8

Used Package Manager

npm

Logs

Click to expand!
  vite:config bundled config file loaded in 424.28ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'vite:watch-package-data',
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:react-babel',
  vite:config     'vite:react-refresh',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm-helper',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:wasm-fallback',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:force-systemjs-wrap-complete',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'vite:dynamic-import-vars',
  vite:config     'vite:import-glob',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   build: {
  vite:config     target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     sourcemap: false,
  vite:config     rollupOptions: { output: [Object], onwarn: [Function: onwarn] },
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     copyPublicDir: true,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     ssrEmitAssets: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
  vite:config     modulePreload: { polyfill: true },
  vite:config     cssMinify: true
  vite:config   },
  vite:config   esbuild: { jsxDev: false, jsx: 'automatic', jsxImportSource: undefined },
  vite:config   optimizeDeps: {
  vite:config     holdUntilCrawlEnd: true,
  vite:config     esbuildOptions: { preserveSymlinks: false, jsx: 'automatic' },
  vite:config     include: [ 'react', 'react/jsx-dev-runtime', 'react/jsx-runtime' ]
  vite:config   },
  vite:config   resolve: {
  vite:config     mainFields: [ 'browser', 'module', 'jsnext:main', 'jsnext' ],
  vite:config     conditions: [],
  vite:config     extensions: [
  vite:config       '.mjs',  '.js',
  vite:config       '.mts',  '.ts',
  vite:config       '.jsx',  '.tsx',
  vite:config       '.json'
  vite:config     ],
  vite:config     dedupe: [ 'react', 'react-dom' ],
  vite:config     preserveSymlinks: false,
  vite:config     alias: [ [Object], [Object] ]
  vite:config   },
  vite:config   configFile: '/home/projects/vitejs-vite-evfakv/vite.config.ts',
  vite:config   configFileDependencies: [ '/home/projects/vitejs-vite-evfakv/vite.config.ts' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/home/projects/vitejs-vite-evfakv',
  vite:config   base: '/',
  vite:config   rawBase: '/',
  vite:config   publicDir: '/home/projects/vitejs-vite-evfakv/public',
  vite:config   cacheDir: '/home/projects/vitejs-vite-evfakv/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   ssr: {
  vite:config     target: 'node',
  vite:config     optimizeDeps: { noDiscovery: true, esbuildOptions: [Object] }
  vite:config   },
  vite:config   isWorker: false,
  vite:config   mainConfig: null,
  vite:config   bundleChain: [],
  vite:config   isProduction: true,
  vite:config   css: { lightningcss: undefined },
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     sourcemapIgnoreList: [Function: isInNodeModules$1],
  vite:config     middlewareMode: false,
  vite:config     fs: {
  vite:config       strict: true,
  vite:config       allow: [Array],
  vite:config       deny: [Array],
  vite:config       cachedChecks: undefined
  vite:config     }
  vite:config   },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   envDir: '/home/projects/vitejs-vite-evfakv',
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(1) {
  vite:config     'fnpd_/home/projects/vitejs-vite-evfakv' => {
  vite:config       dir: '/home/projects/vitejs-vite-evfakv',
  vite:config       data: [Object],
  vite:config       hasSideEffects: [Function: hasSideEffects],
  vite:config       webResolvedImports: {},
  vite:config       nodeResolvedImports: {},
  vite:config       setResolvedCache: [Function: setResolvedCache],
  vite:config       getResolvedCache: [Function: getResolvedCache]
  vite:config     },
  vite:config     set: [Function (anonymous)]
  vite:config   },
  vite:config   createResolver: [Function: createResolver],
  vite:config   worker: { format: 'iife', plugins: '() => plugins', rollupOptions: {} },
  vite:config   appType: 'spa',
  vite:config   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false },
  vite:config   getSortedPlugins: [Function: getSortedPlugins],
  vite:config   getSortedPluginHooks: [Function: getSortedPluginHooks]
  vite:config } +6ms
vite v5.2.8 building for production...
✓ 33 modules transformed.
dist/index.html                   0.47 kB │ gzip:  0.30 kB
dist/assets/Albums-BzKCOirt.js    0.14 kB │ gzip:  0.14 kB
dist/assets/jspdf-PNuX44LO.js     1.04 kB │ gzip:  0.63 kB
dist/assets/index-C34womNo.js   143.33 kB │ gzip: 46.16 kB
✓ built in 1.60s

Validations

Copy link

stackblitz bot commented Apr 15, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@umairkhalid-devsinc
Copy link

Still not fixed

@bluwy
Copy link
Member

bluwy commented Apr 29, 2024

If you'd like to separate out the preload script from the jspdf chunk, you can configure something like this instead:

export default defineConfig({
  plugins: [react()],
  build: {
    rollupOptions: {
      output: {
        manualChunks: {
          jspdf: ['jspdf'],
          preload: ['\0vite/preload-helper.js'] // <-- put in its own chunk
        }
      }
    }
  }
})

Rollup's chunking behaviour doesn't guarantee that only files in jspdf will be included in the chunk. Some imports within jspdf requires preloading, so Rollup includes the preloading script together because it sees that it's part of jspdf too.

About build.modulePreload.polyfill: false not working, I think it's related to #13952. In short, we can't know if preloading may be needed for CSS, so we always inject it. We may need to update the docs about this at the meantime, but we can track this in the issue there.

Closing this one for now as its how Rollup chunks things.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2024
@stefan-schweiger
Copy link
Author

thanks for the hint how to manually chunk the preload code!

@github-actions github-actions bot locked and limited conversation to collaborators May 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants