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

feat: filter chunk assets #655

Closed
wants to merge 3 commits into from
Closed

Conversation

cahnory
Copy link

@cahnory cahnory commented Nov 2, 2020

Summary

In development, when doing SSR with HMR you don't want the hmr related files to be linked by the served html. The filter option in addition to the new info property added to chunk assets make it possible.

I favored this approach rather than an "excludeHmrFiles" type option as it could serve other purposes and remain relevant even if hmr is abandoned in the future.

Test plan

Using webpack with hmr enabled, instanciate your ChunkExtractor like :

import { ChunkExtractor } from '@loadable/server'

const extractor = new ChunkExtractor({
  statsFile: '../dist/loadable-stats.json',
  filter: ({ info }) => !info.hotModuleReplacement,
});

All files relative to hmr (suffixed with "-wps-hmr" by default) should be excluded from the results of getScriptTags, getScriptElements,…

@theKashey
Copy link
Collaborator

Fixes #652 ?

@@ -187,8 +188,13 @@ class ChunkExtractor {
this.outputPath = outputPath || this.stats.outputPath
this.statsFile = statsFile
this.entrypoints = Array.isArray(entrypoints) ? entrypoints : [entrypoints]
this.filter = filter || Boolean
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's use a less generic name on this scope. For example filterChunk

Copy link
Author

Choose a reason for hiding this comment

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

You're right, I made the change.

Copy link
Author

@cahnory cahnory Nov 2, 2020

Choose a reason for hiding this comment

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

I also renamed the option as it makes its purpose more explicit.

@cahnory
Copy link
Author

cahnory commented Nov 2, 2020

Fixes #652 ?

I don't think so.

I didn't want to mess with loadable-stats.json as my intent was only to prevent serving hmr chunks but the stats could potentially serve other purposes.
So this PR allows ChunkExtractor to apply a filter on the assets it returns via its getter methods while #652 would requires the webpack plugin to apply a filter on the chunks it add to the loadable-stats.json file.

@cahnory cahnory requested a review from theKashey November 2, 2020 21:10
@cahnory
Copy link
Author

cahnory commented Nov 3, 2020

Don't know why the ci failed on the last commit.

@cahnory
Copy link
Author

cahnory commented Nov 9, 2020

@theKashey if you prefer that the filter occurs in the webpack plugin (exclude chunks from stats file) let me know.

@stale
Copy link

stale bot commented Jan 8, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jan 8, 2021
@stale stale bot closed this Jan 16, 2021
@theKashey theKashey reopened this Jan 19, 2021
@stale stale bot removed the wontfix label Jan 19, 2021
@cahnory
Copy link
Author

cahnory commented Jan 29, 2021

@theKashey I'm having a problem with stats.json.

When generated, it does not contain hmr chunks (obviously) and the assets.*.info property is always empty (but maybe the hotModuleReplacement property is only set when true).
I can manipulate stats object in my test to match my needs but I think it's not the way I'm supposed to do this.

@theKashey
Copy link
Collaborator

Yeah, probably you should take the original stats (as it's now generated from a real project) and add some "extra" fields in runtime to test, or filter some "normal" chunks, as it does not have test hot case, only the filtering

@cahnory
Copy link
Author

cahnory commented Jan 29, 2021

or filter some "normal" chunks, as it does not have test hot case, only the filtering

I had my initial usecase so much on my mind that I didn't think about it!

@theKashey
Copy link
Collaborator

👀 .....

@cahnory
Copy link
Author

cahnory commented Jan 30, 2021

😅 I don't know what's wrong. I run the commands ci, test and even build locally without any problem. It seems to me that it has something to do with a deployment step that I don't master. In netlify logs I just see :

failed Building production JavaScript and CSS bundles

Any idea ?

@theKashey
Copy link
Collaborator

@cahnory - it's just dead a little. Strangely there is no travis here, and these tests are the only really important thing.

Base automatically changed from master to main February 7, 2021 21:56
@eliseumds
Copy link
Contributor

Anyway I can help here?

@theKashey
Copy link
Collaborator

The real problem here is a "new" undocumented feature, which implementation is still on the user.
If HMR temporal files should not be a part of webpack builds - then they should not. Full stop and no extra conditions.

@cahnory
Copy link
Author

cahnory commented Apr 6, 2021

The real problem here is a "new" undocumented feature, which implementation is still on the user.
If HMR temporal files should not be a part of webpack builds - then they should not. Full stop and no extra conditions.

This is not what I understood from our previous exchanges. In this case, if I understand correctly, it seems unsolvable and maybe we should close this PR.

@theKashey
Copy link
Collaborator

This is not what I understood from our previous exchanges.

Decisions change in time. That's ok.

if I understand correctly, it seems unsolvable

The internal changes are legit and I would like to have them. I am just thinking that we should, at least for now, put filter: ({ info }) => !info.hotModuleReplacement, inside loadable logic, keep external API intact as and I am not sure is there any other use case for filter. If you have one - feel free to change my mind.

The lazy maintainer rule is simple - if you can not expose something - do not, or you will have to maintain it forever (and you are lazy by definition).

@cahnory
Copy link
Author

cahnory commented Apr 7, 2021

I understand, no problem. Just that by doing this way this PR should be closed in favor of a new one which maybe just add a cookbook recipe to the doc.

The way I do it right now is just by filtering extractor.getXElements results with this function:

const filterHmr = (list) =>
  list.filter(
    (element) =>
      !element.props.src?.match(/wps-hmr\.[^.\\/]+$/) &&
      !element.props.href?.match(/wps-hmr\.[^.\\/]+$/),
  );

I just wasn't satisfied with having to check the file names rather than their stats (and extra loops but they are negligible).

@theKashey
Copy link
Collaborator

That's why it's still good to have the majority of this PR built-in into loadable with the correct behavior working out of the box

@cahnory
Copy link
Author

cahnory commented Apr 19, 2021

That's why it's still good to have the majority of this PR built-in into loadable with the correct behavior working out of the box

Oh I see, I completely misunderstood what you were saying. Sorry.
I'll try to find a moment to make the changes.

@stale
Copy link

stale bot commented Jun 18, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 18, 2021
@stale stale bot closed this Jun 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants