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

use case feedback: mix server and web code in the same JavaScript file #14

Open
Lxxyx opened this issue Jul 23, 2021 · 0 comments
Open

Comments

@Lxxyx
Copy link
Member

Lxxyx commented Jul 23, 2021

use case: mix server and web code in the same JavaScript file.

Feedback from @midwayjs/hooks

In https://github.com/midwayjs/hooks, I implemented the concept of "zero" Api, importing functions from the backend and automatically converting them to API requests at build time.

Example:

image

If I use module fragments, I will be able to support a mix of server code and web code in the same JavaScript file.

image

Benefits:

  • More productive, enabling faster development of full-stack applications
  • More intuitive for developers to understand if they are currently writing server code or web code
  • Compiler friendly, easy to analyze and modify

Similar example: server-side rendering for https://github.com/vercel/next.js

Next.js is a very popular front-end framework in the community, with 70.3K Stars and 1.5 million monthly downloads.

Next.js implements server-side rendering by mixing server and web code in the same file, The server-side code is removed at build time with the Babel plugin to provide a browser-ready bundle.

image

Live Example: Next.js | Code Elimination

image

Since JS is a dynamic language, the build tool will not recognize all cases.
For example, when I refer to MySQL outside of getServerSideProps, the generated client bundle will also contain MySQL, which is a buggy behavior.

image

Using module-fragments would be a good solution to this problem.

image

Reference:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant