简体   繁体   English

为每个 html(表单)生成单独的 bundle.js

[英]Generate separate bundle.js for each html (form)

I'm currently working on a react SPA project, where I have to make several forms with logic.我目前正在做一个 react SPA 项目,在那里我必须用逻辑制作几个表单。 Every form has same components and some unique as well.每个表单都有相同的组件,也有一些独特的。 Now: When the client pick one of the forms, it will use the big common bundle.js.现在:当客户端选择其中一种形式时,它将使用大的通用 bundle.js。

Question: Is there a solution for wrapping bundle.js each (webpage) form?问题:是否有包装 bundle.js 每个(网页)表单的解决方案?

Because there are several common components I don't want to make a project for each forms, but I'm thinking of thiner bundle.js files.因为有几个通用组件我不想为每个表单都做一个项目,但我正在考虑更薄的 bundle.js 文件。

Thanks for the advises in advance!感谢您提前提供建议!

It is indeed possible to generate separate bundles for each page by using the dynamic import function and Webpack.确实可以通过使用动态导入功能和 Webpack 为每个页面生成单独的包。

I would highly recommend using react-loadable ( https://github.com/jamiebuilds/react-loadable ) to deal with all the logic around that because there are a bunch of gotchas that you'll have to be aware of (like making sure you have all the bundles ready and loaded before rendering on the client. Or making sure you even know what those modules are)我强烈建议使用react-loadable ( https://github.com/jamiebuilds/react-loadable ) 来处理所有围绕它的逻辑,因为有一堆你必须注意的问题(比如确保在客户端渲染之前已准备好并加载所有包。或者确保您甚至知道这些模块是什么)

The next problem you might have though is that even though the bundles are now smaller, the common bits are in every single bundle.但是,您可能遇到的下一个问题是,即使包现在更小,但每个包中都有公共位。 So if users go to multiple URLs they have to download that multiple times.因此,如果用户访问多个 URL,他们必须多次下载。

Here you have 2 options:在这里你有 2 个选择:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM