简体   繁体   English

拆分我的 Webpack 包导致 JS 问题

[英]Splitting my Webpack bundle is causing JS issues

I'm using Webpack in a fairly simple, straightforward way that bundles together a few JS and TS files into one bundle, and it works well on my site.我以一种相当简单、直接的方式使用 Webpack,将一些 JS 和 TS 文件捆绑在一起,并且它在我的网站上运行良好。

However, I want to split the current bundle into smaller bundles, as I get both a warning when I build the bundle due to it's size, and I get warnings running Lighthouse audits in browser that I should reduce the file size of my bundle.js file.但是,我想将当前包拆分成更小的包,因为当我构建包时由于它的大小而收到警告,并且我在浏览器中运行 Lighthouse 审计时收到警告我应该减少我的 bundle.js 的文件大小文件。

The simplest solution in my mind is to split my current bundle into 4 parts, ie bundle1.min.js, bundle2.min.js, etc... Then I just serve the bundles consecutively.在我看来,最简单的解决方案是将我当前的 bundle 拆分为 4 个部分,即 bundle1.min.js、bundle2.min.js 等……然后我只需连续提供 bundle。

The problem is splitting and serving my bundle this way is breaking other JS on my page.问题是以这种方式拆分和服务我的包会破坏我页面上的其他 JS。 For example a function defined in bundle1 and called in a different JS file no longer works, unless I remove all the other bundle.js files.例如,在 bundle1 中定义并在不同的 JS 文件中调用的 function 不再有效,除非我删除所有其他 bundle.js 文件。 It seems that only the most recently loaded bundle file works.似乎只有最近加载的捆绑文件有效。

Is there a better approach to get smaller bundles, and make sure that all bundles work correctly?有没有更好的方法来获得更小的捆绑包,并确保所有捆绑包都能正常工作?

Route-based code splitting is quite popular because each page/route usually has a small subset of components on it.基于路由的代码拆分非常流行,因为每个页面/路由通常都有一小部分组件。

The guide can be found here (for React)该指南可以在这里找到(对于 React)

A little embarrassed, looks like this was just a scoping issue with a dependency in one bundle breaking code in another by being absent.有点尴尬,看起来这只是一个范围界定问题,一个包中的依赖项由于不存在而破坏了另一个包中的代码。 Reorganizing my bundles so dependencies are present where needed.重新组织我的包,以便在需要的地方出现依赖关系。 Ai ya.哎呀。

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

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