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

refactor: share code with vite runtime #15907

Merged
merged 13 commits into from
Feb 24, 2024

Conversation

sapphi-red
Copy link
Member

@sapphi-red sapphi-red commented Feb 14, 2024

Description

This PR dedupes some code by changing the rollup config and sharing the code with the main code base and the runtime code base.

Sharing code between the main bundle and the runtime bundle is challenging because the main code base should not be included in the runtime bundle. Therefore, this pull request accepts the same code to be included in both the main bundle and the runtime bundle. Since the same code is already included in the main bundle and runtime bundle, the file size will not be larger than before.

There's more code that can be shared but I left them for now to keep this PR smaller.

The new directory structure

  • packages/vite
    • src
      • node: files that will be bundled in the main bundle
        • ssr/runtime: files that uses runtime bundle from the main bundle. To import the runtime bundle, use import 'vite/runtime'.
      • runtime: files that will be bundled in the runtime bundle
      • shared: files shared among client/node/runtime

What I did

  • made a separate bundle for runtime rather than a separate entry point
    • I changed rollup.config.ts, rollup.dts.config.ts, src/node/tsconfig.json, vitest.config.ts
  • removed the copied code from @jridgewell/trace-mapping and used the original package with patch + replace
  • moved the runtime related code to src/runtime
  • made src/shared/utils.ts and src/shared/constants.ts; moved some functions that are used from both src/node and src/runtime

Additional context

  • How much should be make the runtime bundle lightweight? I remember that it should be lightweight for perf.
    • @sheremet-va Is it the file size that affects the performance? or is it the number of statements? I mean does including a large comment affect performance?
    • What I did in this PR to keep the file size:
      • I added rollup-plugin-esbuild to remove comments and reduce the number of statements.
      • replaced bias === LEAST_UPPER_BOUND with true so that some code from @jridgewell/trace-mapping will be tree shaken
      • patched @jridgewell/resolve-uri to make some code tree-shaken the change is now merged
    • Should we completely minify the runtime bundle?

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines, especially the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Update the corresponding documentation if needed.
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added the p1-chore Doesn't change code behavior (priority) label Feb 14, 2024
Copy link

stackblitz bot commented Feb 14, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@sheremet-va
Copy link
Member

  • Is it the file size that affects the performance? or is it the number of statements? I mean does including a large comment affect performance?

I think the idea is that it should only include code that can actually be running. The reason why we are doing it as a separate entry point is to not drag rollup/esbuild and all other dependencies where they are not used.

Should we completely minify the runtime bundle?

I don't think that's necessary.


From the bundled code point of view, I am very satisfied with the result 👍🏻 And I also like separating runtime code into vite/src/runtime folder 👍🏻

sheremet-va
sheremet-va previously approved these changes Feb 14, 2024
@patak-dev patak-dev added the feat: environment API Vite Environment API label Feb 14, 2024
bluwy
bluwy previously approved these changes Feb 21, 2024
Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change! Awesome work refactoring this Sapphi.

@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 77361bb: Open

suite result latest scheduled
analogjs success success
astro success success
histoire success success
ladle success success
laravel success success
marko success success
nuxt success success
nx failure failure
previewjs success success
qwik success success
rakkas success success
sveltekit success success
unocss success success
vike success success
vite-plugin-pwa success success
vite-plugin-react success success
vite-plugin-react-pages success success
vite-plugin-react-swc success success
vite-plugin-svelte success success
vite-plugin-vue success success
vite-setup-catalogue success success
vitepress success success
vitest success success

Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome ❤️

@patak-dev patak-dev merged commit b20d542 into vitejs:main Feb 24, 2024
10 checks passed
@sapphi-red sapphi-red deleted the refactor/bundle-vite-runtime branch March 14, 2024 06:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: environment API Vite Environment API p1-chore Doesn't change code behavior (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants