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

Webpack, dev. env: Fix configuration export #10631

Conversation

jibees
Copy link
Contributor

@jibees jibees commented Mar 29, 2023

What? Why?

Since #10568 we changed a bit the configuration options for webpack in development environement. The PR was incomplete, since it does not export the right value. Change that.

What should we test?

  • Locally, remove public/packs folder
  • Start your server (via foreman start) and check that after requesting a page, folder public/packs is not empty and you don't have any errors in your browser console
  • Everything should be fine (can navigate, ...)

Release notes

Changelog Category: Technical changes

@jibees jibees self-assigned this Mar 29, 2023
@jibees jibees force-pushed the fix-webpack-configuration-for-dev-env branch from 341b3ba to 456fc3e Compare March 29, 2023 15:09
@dacook
Copy link
Member

dacook commented Mar 29, 2023

Oh, sorry I missed that. In fact, apparently there is a lot of release notes that I missed.

Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Changes look good, although I have to admit I'm not familiar with webpack configuration.

Dev test also good (just some errors from missing active_storage images which I think is unrelated):
Screen Shot 2023-03-30 at 9 35 57 am


module.exports = environment.toWebpackConfig()
module.exports = config;
Copy link
Member

Choose a reason for hiding this comment

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

I see that we were exporting the original config instead of the changed config, so this change makes sense 👍

@jibees
Copy link
Contributor Author

jibees commented Mar 30, 2023

Actually, this seems not solve the original issue: I can still observe the bug with the configuration.
Steps to reproduce (?):

  • rm -rf public/packs
  • foreman start
  • refresh browser
  • modify app/webpacker/controllers/tom_select_controller.js (I guess it's the same for all js files ...) ; to activate hotmodulereload
  • refresh the browser
  • Observe the 404 in the browser console:
GET http://localhost:3000/packs/packs/js/application-e08cb490b1e7d7ef4c24.js 404 (Not Found)
:3000/packs/css/darkswarm-028d6116.css:1
GET http://localhost:3000/packs/css/darkswarm-028d6116.css 404 (Not Found)

or in the rails console:

ActionController::RoutingError (No route matches [GET] "/packs/packs/js/application-9fbe5618e90966ec3586.js"):

@jibees
Copy link
Contributor Author

jibees commented Mar 30, 2023

I spend a lot of time, trying to solve what's happening here, without any success ; actually I'm even not sure on how to reproduce the issue! I think it's great if we could start working on Rails Assets Management pipeline to remove webpack(er) and having a up-to-date assets management system.

Note: from V3 to V4 migrating guide of webpack-dev-server they recommend to use webpack >= 5.

but we recommend using webpack >= v5.0.0

@abdellani
Copy link
Member

Actually, this seems not solve the original issue: I can still observe the bug with the configuration. Steps to reproduce (?):

  • rm -rf public/packs
  • foreman start
  • refresh browser
  • modify app/webpacker/controllers/tom_select_controller.js (I guess it's the same for all js files ...) ; to activate hotmodulereload
  • refresh the browser
  • Observe the 404 in the browser console:
GET http://localhost:3000/packs/packs/js/application-e08cb490b1e7d7ef4c24.js 404 (Not Found)
:3000/packs/css/darkswarm-028d6116.css:1
GET http://localhost:3000/packs/css/darkswarm-028d6116.css 404 (Not Found)

or in the rails console:

ActionController::RoutingError (No route matches [GET] "/packs/packs/js/application-9fbe5618e90966ec3586.js"):

Hi @jibees
I followed the same steps but I didn't get any errors on the browser's console.

@jibees
Copy link
Contributor Author

jibees commented Apr 3, 2023

Yep, you're right, this does not solve anything. Sorry, that's why this was back to In Dev

@jibees jibees force-pushed the fix-webpack-configuration-for-dev-env branch from 456fc3e to b9bacba Compare April 3, 2023 08:00
@jibees
Copy link
Contributor Author

jibees commented Apr 3, 2023

  • Remove public/packs
  • Starts ./bin/webpack-dev-server
    Compiled with ddf7a90f84980a3795b1 as hash, manifest.json file is created referencing (it's just an example):
"application": {
      "js": [
        "/packs/packs/js/application-ddf7a90f84980a3795b1.js"
      ],

with the hash ddf7a90f84980a3795b1.

  • Start the rails server
  • Request a shopfront
  • Webpack starts compiling (through the rails app) files with a different hash, but that's fine because manifest.json now reference that new hash:
    File created:
public/packs/packs/js/application-18f09577984a69a22fe2.js

manifest.json:

    "application": {
      "js": [
        "/packs/packs/js/application-18f09577984a69a22fe2.js"
      ],
  • Modify app/webpacker/controllers/tom_select_controller.js but without any modification. Hash is now c82464ad1118ed74b959, as read in the manifest.json file:
    "application": {
      "js": [
        "/packs/packs/js/application-c82464ad1118ed74b959.js"
      ],

No files are created with this hash since compilation has not started through webpack.

  • Then request the shopfront: no webpack compilation is started, and requesting files fails because of that de-synchronization between webpack-dev-server and webpack hash
ActionController::RoutingError (No route matches [GET] "/packs/packs/js/application-c82464ad1118ed74b959.js"):

(c82464ad1118ed74b959 is the hash in the manifest.json file, but no file had been created with that hash)

My (partial) conclusion is: there seems to have a de-synchronization between webpack-dev-server and webpack around that hash (between manifest.json and files that are actually created).

@mkllnk
Copy link
Member

mkllnk commented Apr 3, 2023

My (partial) conclusion is: there seems to have a de-synchronization between webpack-dev-server and webpack around that hash (between manifest.json and files that are actually created).

A hash is usually unique to some input. If you don't change the input then the hash stays the same. So it seems like webpack-dev-server has some changing input which webpack doesn't have. Does it somehow include some already compiled files again? Or is it including a timestamp even though it shouldn't? It could be a bug but these are just theories. I haven't looked into this.

Shall we just abandon webpack-dev-server? If we move to another tool, we won't have that feature anyway, right? It just means that Rails has to recompile with webpack on a new request after changing assets and the browser won't reload automatically.

@rioug
Copy link
Collaborator

rioug commented Apr 4, 2023

This causing issue for the docker environment as well. Just noting my observation so far :

  • currently js files are generated in public/packs/packs/js/
  • the webpage is requesting js files from packs/js but with the wrong hash 😕

If I apply @jibees fix then webpack generate a manifest.json but nothing else 😕

Edit : I think the following fix is working at least in a docker environment, config/webpack/development.js should look like this :

const environment = require('./environment')

const config = environment.toWebpackConfig();
config.output.filename = "js/[name]-[hash].js";

module.exports = config;

@jibees jibees force-pushed the fix-webpack-configuration-for-dev-env branch from b9bacba to 17285a8 Compare April 4, 2023 12:35
@jibees
Copy link
Contributor Author

jibees commented Apr 4, 2023

thanks @rioug for spotting that. I was pretty sure that I already removed that test in my commit, but indeed no. I tried with your suggested solution (which I think I already tested): it does not work for me as expected. I always have some Routing errors and de-synchronization between webpack, rails, ...

@rioug
Copy link
Collaborator

rioug commented Apr 6, 2023

I played around with this a little bit more with a docker setup. I can see webpack-dev-server recompiling file and updating manifest.json when a file is updated. But it doesn't trigger an auto reload. If I then refresh/load the page webpack will recompile (through the rails app) and pick up the change.
We don't get the benefit of the webpack-dev-server then but at least it's not creating errors.

I realise this is not supper helpful ...

@jibees
Copy link
Contributor Author

jibees commented Apr 6, 2023

I can see webpack-dev-server recompiling file and updating manifest.json when a file is updated. But it doesn't trigger an auto reload. If I then refresh/load the page webpack will recompile (through the rails app) and pick up the change.

Same here (not using docker)

@rioug rioug mentioned this pull request Apr 30, 2023
@rioug
Copy link
Collaborator

rioug commented Apr 30, 2023

Looks like the issue is coming from the upgrade of webpack-dev-server to 4.x and should be fixed by : #10765

@rioug rioug self-assigned this May 2, 2023
@rioug rioug force-pushed the fix-webpack-configuration-for-dev-env branch from 17285a8 to 6e34f61 Compare May 2, 2023 06:06
@rioug rioug requested a review from dacook May 2, 2023 06:38
@rioug
Copy link
Collaborator

rioug commented May 2, 2023

I rebased and removed the minimum version as we are still on webpack-dev-server 3 it's not applicable to us.

@jibees
Copy link
Contributor Author

jibees commented May 2, 2023

Thanks @rioug ! I've tested, and seems to work well. I think this is ready to be reviewed and/or tested.

(oops, sorry, I didn't see that you've put this in Code review and request a review from @dacook . Thanks!)

@dacook dacook added the pr-staged-au staging.openfoodnetwork.org.au label May 3, 2023
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Thanks, I can see this is now just a one-line config change, which looks good to me.

It looks like it has already been dev tested, but I'll give it another go.

✅ Looks good, I can see public/packs is created, and local server works. I see a couple of image load errors, but they are active_storage images and probably due to my local data.

Deployed to staging, no console errors.

Ready to merge?

@dacook dacook removed the pr-staged-au staging.openfoodnetwork.org.au label May 3, 2023
Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

I think that we are trying to fix a workaround which was only introduced by the incompatibility of webpacker versions. We should restore the original file:

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

const environment = require('./environment')

module.exports = environment.toWebpackConfig()

Investigating this I found that we are changing the filename generation to use hash instead of contenthash. But contenthash is recommended and in use by default. I guess that the now reverted version bump changed the naming which brought up this incompatibility. We can now use the default config again.

Sources:

@jibees
Copy link
Contributor Author

jibees commented May 3, 2023

@mkllnk
You'r right. Will close this PR (too many comments) and open a new one to bring those changes. Thanks for your attention! 🙏

@jibees jibees closed this May 3, 2023
jibees added a commit to jibees/openfoodnetwork that referenced this pull request May 3, 2023
We used to override the output filename but this was a misunderstanding of an error (due to webpacke(r) incompatibles versions)

https://medium.com/@web_developer/hash-vs-chunkhash-vs-contenthash-e94d38a32208

webpack/webpack.js.org#2096

Context: openfoodfoundation#10631 (review)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants