question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Preload code not passing through rollup

See original GitHub issue

Describe the bug

I added a rollup plugin to vite config, but it turned out that it does not affect the preload code. The bundle has mixed code - passed through rollup and not passed through rollup. Could you make preload code go through rollup too?

See the bundle:

dist/assets/index.02199377.js
// this code is not passed through rollup. const is not converted to var by rollup/babel plugin
const p = 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);
  }
  new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      if (mutation.type !== "childList") {
        continue;
      }
      for (const node of mutation.addedNodes) {
        if (node.tagName === "LINK" && node.rel === "modulepreload")
          processPreload(node);
      }
    }
  }).observe(document, { childList: true, subtree: true });
  function getFetchOpts(script) {
    const fetchOpts = {};
    if (script.integrity)
      fetchOpts.integrity = script.integrity;
    if (script.referrerpolicy)
      fetchOpts.referrerPolicy = script.referrerpolicy;
    if (script.crossorigin === "use-credentials")
      fetchOpts.credentials = "include";
    else if (script.crossorigin === "anonymous")
      fetchOpts.credentials = "omit";
    else
      fetchOpts.credentials = "same-origin";
    return fetchOpts;
  }
  function processPreload(link) {
    if (link.ep)
      return;
    link.ep = true;
    const fetchOpts = getFetchOpts(link);
    fetch(link.href, fetchOpts);
  }
};
p();
var style = "";

// this code is passed through rollup. const is converted to var by rollup/babel plugin
var html = "<h1>Hello Vite!</h1>";
document.querySelector("#app").innerHTML = html;

Additional: If this issue will resolved, then this pull request is no longer needed.

Reproduction

see my repo here

npm create vite@latest
√ Select a framework: » vanilla
√ Select a variant: » vanilla
cd <vite project>
pnpm install
  • add babel dependencies
  • add babelrc config
  • add vite config
  • add rollup babel to the vite config
vite build --debug
  • see the dist/assets/index.[hash].js file

System Info

System:
    OS: Windows 10 10.0.19042
    CPU: (8) x64 Intel(R) Core(TM) i7-3610QM CPU @ 2.30GHz
    Memory: 5.14 GB / 15.89 GB
  Binaries:
    Node: 16.15.1 - E:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.11.0 - E:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1023.0), Chromium (103.0.1264.49)
    Internet Explorer: 11.0.19041.906
  npmPackages:
    vite: ^2.9.9 => 2.9.14

Used Package Manager

pnpm

Logs

Logs
C:\Users\Admin\AppData\Roaming\npm\pnpm.cmd run build

> vite-issues@0.0.0 build D:\projects\-temp\vite\vite-issues
> vite build --debug

  vite:config native esm config loaded in 261.08ms URL {
  href: 'file:///D:/projects/-temp/vite/vite-issues/vite.config.mjs',
  origin: 'null',
  protocol: 'file:',
  username: '',
  password: '',
  host: '',
  hostname: '',
  port: '',
  pathname: '/D:/projects/-temp/vite/vite-issues/vite.config.mjs',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
} +0ms
  vite:config using resolved config: {
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: { plugins: [Array] },
  vite:config     minify: false,
  vite:config     terserOptions: {
  vite:config       module: true,
  vite:config       ecma: 5,
  vite:config       safari10: true,
  vite:config       mangle: false,
  vite:config       format: [Object]
  vite:config     },
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: 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   },
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'alias',
  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',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite-plugin-babel',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:watch-package-data',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'babel',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   configFile: 'D:/projects/-temp/vite/vite-issues/vite.config.mjs',
  vite:config   configFileDependencies: [ 'D:/projects/-temp/vite/vite-issues/vite.config.mjs' ],
  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: 'D:/projects/-temp/vite/vite-issues',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: 'D:\\projects\\-temp\\vite\\vite-issues\\public',
  vite:config   cacheDir: 'D:\\projects\\-temp\\vite\\vite-issues\\node_modules\\.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isWorker: false,
  vite:config   isProduction: true,
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  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   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(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   },
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object]
  vite:config     ],
  vite:config     rollupOptions: {}
  vite:config   }
  vite:config } +22ms
build.terserOptions is specified but build.minify is not set to use Terser. Note Vite now defaults to use esbuild for minification. If you still prefer Terser, set build.minify to "
terser".
vite v2.9.14 building for production...
✓ 4 modules transformed.
dist/assets/favicon.17e50649.svg   1.49 KiB
dist/index.html                    0.46 KiB
dist/assets/index.02199377.js      1.38 KiB / gzip: 0.57 KiB
dist/assets/index.38b0c734.css     0.19 KiB / gzip: 0.16 KiB

Process finished with exit code 0

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
bluwycommented, Jul 11, 2022

I’ve checked rollup/plugins repo. It seems most of the virtual modules has a extension.

Interesting. I think it’s still risky to expect virtual modules in general to have an extension though, but thanks for the pointer. Maybe we could consider adding extensions for Vite too.

plugin-legacy does transpile the code using babel and skips esbuild transpilation.

You’re right, I forgot that plugin-legacy skips esbuild. In that case babel would be handling syntax transform then via plugin-legacy’s target option.

0reactions
sapphi-redcommented, Jul 11, 2022

That’s because plugin-legacy now only injects polyfills. Syntax transformation are handled by esbuild and build.target, down to es2015.

plugin-legacy does transpile the code using babel and skips esbuild transpilation. https://github.com/vitejs/vite/blob/519f7debe176d99f464a96240842c303d8ec5e57/packages/plugin-legacy/src/index.ts#L353-L402 And yes, plugin-legacy does not have an option to control babel finely.

Read more comments on GitHub >

github_iconTop Results From Across the Web

rollup-plugin-modulepreload - npm
Rollup plugin to add modulepreload links from generated chunks. Users may customize which chunks are preloaded using the shouldPreload option.
Read more >
rollup.js
When using the JavaScript API, the configuration passed to rollup.rollup must be an object and cannot be wrapped in a Promise or a...
Read more >
How to use preload.js properly in Electron - Stack Overflow
js I have access to Node modules) and then merely call the functions in each renderer.js file (for example here, main_window.js )? What...
Read more >
Build Options | Vite (main branch)
Note the build will fail if the code contains features that cannot be safely ... The list of chunks to preload for each...
Read more >
vite build - patak-dev
JS sources go through the normal rollup bundling process ... Vite performs static analysis of imports to inject module preload link tags for ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found