-
Notifications
You must be signed in to change notification settings - Fork 10
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 generated assets to main compiler #59
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- link the issue to the PR
- write a test :)
Other than what Simone said, rest LGTM! Nice job! |
OK.
|
|
Actually, the used hook is wrong, sometimes never get called... |
@ShogunPanda what test can we add for this feature? |
Well, since the original problem was that the pino assets disappeared, maybe we can just check they don't when used in conjunction of the web pack cleaning plugin. |
The test would be to check if the assets property of returned I had to leave the computer, but I'll work on that by the end of the day... (my day, in Europe 😉) |
Clean plugin was just the plug-in who led to find this issue, but other plugins may need to have accurate assets from compilation.... |
I'm actually stuck. When I add an asset to the main compilation assets, two files are generated with different hashes (one before adding the footer code, and one after)... I have to spend more time on it, it was a bit too soon to send the PR. 😅 |
Child compilation assets are generated in memory Assets are now addded to the main compilation allowing other plugins to be aware of those additional files ...but cache test doesn't pass anymore, don't know where to handle it
I finally had time to spend on this... @gdorsi : as you wrote the PR to handle cache in this plugin, could you have a look at my code? I really don't understand how to handle the cache... and if you could explain to me how it works, I would really appreciate! 😇 |
@lalexdotcom meanwhile it might be worth looking into why CI fails |
You're right, it's linting issues: the variables used by cache handling were still there, and sometimes I prefer having no brackets for a simple |
@@ -2,7 +2,7 @@ const webpack = require('webpack') | |||
const CommonJsRequireDependency = require('webpack/lib/dependencies/CommonJsRequireDependency') | |||
const { createFsFromVolume, Volume } = require('memfs') | |||
const { sep, dirname, relative } = require('path') | |||
const { cache } = require('webpack') | |||
// const { cache } = require('webpack') // Cache handling missing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this commented out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said in my commit, I couldn't find where to handle the cache in the refactoring: #59 (comment)
To be able to emit assets from the child compilation, I had to use different webpack hooks...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it's nearly a complete refactoring, I thought it would be more readable this way:
- I got into the (too light) Webpack hooks documentation, and used more appropriate hooks to handle additional assets
- I thought that having functions declared further in code but used only once doesn't help readability. I used the function body directly in the webpack hooks
Please let me know if you think I should have kept the previous code "style"...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to help!
Well done @lalexdotcom, switching to webpack.EntryPlugin
is a great improvement!
WIth webpack.EntryPlugin
the new files are no more considered external so the cache is already handled by webpack (TBH I don't understand 100% the referenced code, but since the tests are passing I think there is no need to go deeper)
As general advice for your future contributions, try to keep the style changes in a separate PR because they make the review process way harder.
childCompiler.inputFileSystem = compiler.inputFileSystem | ||
// Generate files in memory | ||
// It will be written to output path by main compilation | ||
childCompiler.outputFileSystem = fs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mmmhh, I'm not sure that overriding the default fs is a good thing.
Why did you made this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @gdorsi
I thought that avoiding writing intermediate assets to the file system and keeping them in memory would be a good idea...
Actually, not all the running tests are passing: the cached build test doesn't pass (this is why I tagged you in my comment). The second build is generating new files...
Noted for changing style in PRs. Actually, it helped me to understand the process of the plugin, so for me it was kinda part of the feature...
Could you take a look on the cache handling part? And if you could explain to me how it works...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the EntryPlugin was already there as I remember (addExternalFile
and trackInclusions
functions)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, not all the running tests are passing: the cached build test doesn't pass (this is why I tagged you in my comment). The second build is generating new files...
Weird, I tried to run the tests locally and they are passing:
test/cached-build.test.js
it should correctly generate all required pino files when the cache is enabled on Webpack
✓ should not error
✓ should not error
✓ expect falsey value
✓ expect truthy value
✓ expect truthy value
✓ expect truthy value
✓ expect truthy value
✓ should match pattern provided
✓ should match pattern provided
could you share the errors you are getting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still not for me:
test/cached-build.test.js
it should correctly generate all required pino files when the cache is enabled on Webpack
✓ should not error
✓ should not error
✓ expect falsey value
✓ expect truthy value
1) expect truthy value
✓ expect truthy value
✓ expect truthy value
✓ should match pattern provided
✓ should match pattern provided
Maybe there's a cache folder I did'nt clear, but I don't see any...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, I missed this so now I understand why the memory fs is useful (would be worth a comment in the code) and why the tests fail if it is disabled.
Good call!
Have you find out why the tests are not passing on your local?
If not I could try to help.
Could you share your node version and the lockfile?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests are now passing on my local computer BUT SHOULDN'T: two 'first-xxxx.js' and 'abc/cde/second-xxxxx.js' files are generated (when using cache)...
As readdirSync
is returning filenames in alphabetical order, depending on the hash, tests could succeed when it shouldn't... for example they should fail if there's several file starting with 'first-'...
A way to help me would be to test with no temp dir, look at the output and see why the file generated on the second build with cache enabled has a different name: as I said, I don't really understand how the webpack cache works and there's poor documentation on that... does the childCompiler use cache?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, I missed this so now I understand why the memory fs is useful (would be worth a comment in the code) and why the tests fail if it is disabled.
You're perfectly right, I added a comment on that in a new commit, let me know if it's clear enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
look at the output and see why the file generated on the second build with cache enabled has a different name
If the name is generated using the content hash it is normal for them to have different names.
I don't really understand how the webpack cache works and there's poor documentation on that... does the childCompiler use cache?
The poor docs is what make writing Webpack plugins so hard.
I did this one year ago and don't remember these parts, only that you need to check the Webpack source code to understand what's happening.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
look at the output and see why the file generated on the second build with cache enabled has a different name
If the name is generated using the content hash it is normal for them to have different names.
What I don't understand is that file content hasn't changed, so the hash shouldn't have either...
I'm gonna try to dive deeper in the cache handling, but don't know when I'll be able to fix this...
A more consistent variable name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are too many unrelated changes here making this PR is too hard to be reviewed properly.
Let's focus on what we need to fix the issue and keep the rest for future contributions.
The code refactoring looks good from what I can see. The refactoring was huge and thus I trust the CI. @lalexdotcom Thanks for the PR, this was huge. |
@gdorsi @lalexdotcom Since CI is already passing, I wait for the green light from you to land this. |
given the extent of the changes, as soon as we're happy with the PR it may be more cautious to release this as a prerelease. @lalexdotcom is this supposed to contain semver breaking changes? |
Hi @ShogunPanda, thanks for your feedback. This PR already solves #58, but in my local config AND in a local devcontainer (Node 18-bullseye for VSCode), cache tests don't pass... this is a regression and maybe it would not be wasted time for reliability to spend more time on this, maybe on different user machine, using isolated environment? For semver, can adding a new dependency be considered as a breaking change? If not, no breaking change... |
thanks for the clarification on this point. |
@ShogunPanda there is still the issue of a test failing on @lalexdotcom local. Once we have sorted that out the PR is good to go for me. |
This hook is called even if cache is enabled
I think I nailed it! 😎 When using cache, the I'm now using the |
As this PR was originally made to fix #58, i also updated the clean-plugin.test to perform 2 builds and be sure that cold start and restart have the same behavior, which is required for watch mode... |
@ShogunPanda PTAL |
@ShogunPanda @lalexdotcom are we happy with this PR then? I assume this needs to go in before #83 ? |
go for it! |
The assets generated by childCompilation (basically the JS files) are now added to the main compilation assets, allowing other Webpack plugins to be aware of it.
FIXES: #58