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

The importing statements injected by Vite Plugin cause unnecessary dependencies resolving overhead #6677

Closed
7 tasks done
zheeeng opened this issue Jan 29, 2022 · 1 comment

Comments

@zheeeng
Copy link
Contributor

zheeeng commented Jan 29, 2022

Describe the bug

The importing statements injected by Vite Plugin cause unnecessary dependencies resolving overhead.

If a plugin injects importing statements into an application, there are two flaws:

  1. The pre-bunding discovery will skip the scanning of the content of importing statements at the startup phase.
  2. Dependencies discovery will be triggered asynchronously.

I'm developing a huge application that needs the dependencies injection.

I'm using a Vite plugin to scan the whole project and find out injectable content, using async importing statements injecting them into a provider container(IOC).

The plugin works well but the application startup speed is horrible at the first time (or without .vite bundle cache), almost 10+ mins(vs 1 min before the migrating from Webpack), I think it is mainly caused by the second flaw.

This demo plugin is as simple as

{
      name: 'vite-inject-importing-statements',
      load(id) {
        if (!id.includes('bootstrap')) {
          return
        }
        
        return fs.readFileSync(id, 'utf-8').replace(
          'Promise.all([])',
          `
          Promise.all([
            import('./pixi'),
          ])
          `
        )
      }
    }

replace the file content from const modules: any[] = await Promise.all([]) to const modules: any[] = await Promise.all([import('./pixi')]).

If without the plugin, write the replacement directly, console reports:

image

It's extremely fast.

Once the plugin worked, console reports:

image

At a large-scale application, the console reports many times for the newly founded dependencies and repeatedly reloads the page, it takes too long to start up an application without the building cache.

No matter whether it's using dynamic importing to static importing statements, the buggy case will be reproduced stably.

I tried another experiment for writing down import statements manually and enabling the plugin to rewrites the same statements, Vite handles the case very well without repeatedly re-discovery dependencies.

There is also a minimum reproduction below.

Happy lunar new year. 🎉🎉🎉

Reproduction

https://github.com/zheeeng/vite-inject-importing-statemetns

System Info

System:
    OS: macOS 11.5.2
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 77.42 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.18.1 - ~/.nvm/versions/node/v14.17.0/bin/node
    Yarn: 1.22.11 - ~/.nvm/versions/node/v14.17.0/bin/yarn
    npm: 6.14.15 - ~/.nvm/versions/node/v14.17.0/bin/npm
  Browsers:
    Chrome: 97.0.4692.99
    Safari: 14.1.2
  npmPackages:
    vite: ^2.7.13 => 2.7.13

Used Package Manager

pnpm

Logs

No response

Validations

@zheeeng
Copy link
Contributor Author

zheeeng commented Jan 30, 2022

Another buggy behavior is the sourcemap file resolving fail:

  1. Sourcemap for the third party modules. e.g. Sourcemap for "/Users/zheeeng/Workspace/vvfx-new/node_modules/.pnpm/inversify@5.1.1/node_modules/inversify/lib/inversify.js" points to missing source files
  2. Sourcemap for chunked js file under .vite. e.g. 下午10:54:23 [vite] Failed to load source map for /node_modules/.vite/chunk-WT4RPIXK.js?v=7b399ea9.
  3. Sourcemap for cached module file under .vite. e.g. 上午10:54:23 [vite] Failed to load source map for /node_modules/.vite/@vvfx_shared_node_modules_antd_es_message.js?v=8993e4f2.

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

1 participant