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

Built JS code not fully minified when dynamic import is used #15300

Closed
7 tasks done
mdickopp opened this issue Dec 10, 2023 · 4 comments
Closed
7 tasks done

Built JS code not fully minified when dynamic import is used #15300

mdickopp opened this issue Dec 10, 2023 · 4 comments

Comments

@mdickopp
Copy link

Describe the bug

Generated JS files that use dynamic imports are not fully minified. For example, when building the reproduction repository, a file dist/assets/index-q-SMFOy6.js is generated that contains some minified code, followed by the following code that is not minified:

function __vite__mapDeps(indexes) {
  if (!__vite__mapDeps.viteFileDeps) {
    __vite__mapDeps.viteFileDeps = []
  }
  return indexes.map((i) => __vite__mapDeps.viteFileDeps[i])
}

I would have expected that everything in this file is minified, including the __vite__mapDeps function.

Reproduction

https://github.com/mdickopp/vite-minification-issue

Steps to reproduce

  1. Clone the reproduction repository
  2. Change into the cloned directory
  3. Run npm install
  4. Run npm run build

System Info

System:
    OS: Linux 6.5 Debian GNU/Linux trixie/sid
    CPU: (16) x64 Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz
    Memory: 27.93 GB / 31.28 GB
    Container: Yes
    Shell: 5.2.21 - /bin/bash
  Binaries:
    Node: 18.19.0 - /usr/bin/node
    npm: 10.2.5 - ~/.npm/bin/npm
  Browsers:
    Chromium: 120.0.6099.71
  npmPackages:
    vite: ^5.0.7 => 5.0.7

Used Package Manager

npm

Logs

No response

Validations

@bluwy
Copy link
Member

bluwy commented Dec 11, 2023

At the moment, this is intentional. The __vite__mapDeps code can only be injected post minification

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Dec 11, 2023
@acid-chicken
Copy link

On the other hand, at the moment there still seems to be room for optimization. For example, __vite__mapDeps([]) can safely be replaced by []. And, it is possible to minify the declaration body of __vite__mapDeps in advance.

@bluwy
Copy link
Member

bluwy commented Dec 20, 2023

I think circling back on this, I'm definitely open to minifying the injected util code a bit so it's shorter (though complete minification is still not the goal). Fixing the empty array case would be nice as you mentioned. About the __vite__mapDeps function name, it should probably still be kept still as a marker, though it currently appears thrice and I'm not sure of any other tricks to avoid that.

@DiFuks
Copy link

DiFuks commented Dec 22, 2023

With this, there is another issue: I use the plugin @rollup/plugin-babel for transpiling to legacy browsers using the getBabelOutputPlugin mode. The code that Vite adds (__vite__mapDeps) is the only thing that doesn't get transpiled by this plugin because the addition occurs after the execution of the output plugins in Rollup.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 6, 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

4 participants