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

Add rule for chunk-download-size-limits #203

Open
icy0307 opened this issue Feb 28, 2023 · 3 comments
Open

Add rule for chunk-download-size-limits #203

icy0307 opened this issue Feb 28, 2023 · 3 comments

Comments

@icy0307
Copy link

icy0307 commented Feb 28, 2023

Is your feature request related to a problem? Please describe.
I try to limit the size of the critical path our application needs on ci.
However, our application is running in a host application(SPA), that serves several other 'apps' on the same page.
So @statoscope/webpack/entry-download-size-limits no longer suits us, cause we are no longer initial chunks.
Is there any other way to provide this kind of rule, that limits the size of the chunks that critical rendering path need?

Describe the solution you'd like
A clear and concise description of what you want to happen.
One way to achieve this is to specify the chunk that the rendering codes are in (like a designated webpack chunks name), then find all its parent chunks.

Describe alternatives you've considered
Another way of doing it is just to find all the chunks that the critical rendering path needs, simply just by its module name.
However, a module might be duplicated in different chunks, I am not very clear on how to implement this way(But isn't that rendering code should only appear once in the final builds?).

Additional context
I tried to implement a simple version of it by myself, following the custom rule guide.
But get stuck in compilations API in WebpackRule<Params> = (ruleParams, data, api): void of date object.
Although in my stats.json, the parents property exists for every chunk, I can't get it from 'data.compilations[0].chunks[someIndex].parents'.
Looking into the source code,
I found in the source code, the chunk's parent has been set to empty and relink by chunks' origin, which is not in ours stats.json

  1. What is your idea on implementing this properly?
  2. Is it ok we use just js to query the original compilation object instead of jora? I and many of my teams find it quite hard to learn jora by simply looking into the demo. (Is there any tutorial on this subject?) .Would you accept the rule's pr without jora?
  3. why is chunks origin is needed for this situation?
@smelukov
Copy link
Member

smelukov commented Mar 1, 2023

Thanks for the issue!

What is your idea on implementing this properly?

Let's start from rule parameters:

export type Params = {
  byName: Array<{name: string | RegExp; limit: number; useCompressed: boolean}>
};

So, here is a pseudo-code of the rule implementation:

const chunkDownloadSizeLimits: WebpackRule<Params> = (params, data, api): void => {
  api.setRuleDescriptor(...);
  
  for (const item of params.byName) {
    const targetChunks = getTargetChunksFromDataWithParams(data, params); // collect chunks by name or regexp (you could use jora or pure js here)
    for (const chunk of targetChunks) {
      const diff = isChunkSizeOk(chunk, item); // check chunk size by the item props
      if (diff) {
        api.message('Oh, no!', {
          filename: data.files[0].name,
          compilation: chunk.compilation.hash,
          related: [{ type: 'chunk', id: chunk.id }],
        });
      }
    }
  }
};

export default chunkDownloadSizeLimits;

Is it ok we use just js to query the original compilation object instead of jora?

Yep

why is chunks origin is needed for this situation?

Chunks origins is more accuracy to get chunk relations from webpack stats

@icy0307
Copy link
Author

icy0307 commented Mar 6, 2023

Thanks for your detailed answer.

  1. Can I find all the files by module name? But isn't that one module could belong to several chunks?
  2. Could you elaborate on why Chunks origins is more accuracy to get chunk relations from webpack stats?

One more thing, the stats.json our project generated is quite large (around 1.5GB before zipped), is there anything I could do about it?

@smelukov
Copy link
Member

smelukov commented Mar 8, 2023

Can I find all the files by module name?

Yep. Every module includes chunks-property. You could find all the modules you need (e.g. by module name) and then collect chunks from the chunks-property.

But isn't that one module could belong to several chunks?

Yes, one module could belong to several chunks. That's why chunks-property is an array

Could you elaborate on why Chunks origins is more accuracy to get chunk relations from webpack stats?

Seems like there is a bug in webpack stats (see #168 for more info). Do you have concerns with using origins to build chunk graph?

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

No branches or pull requests

2 participants