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

#987 optimizing and compressing webpack assets #996

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

pushyamig
Copy link
Contributor

@pushyamig pushyamig commented Jul 28, 2020

Solves #996 , #648
Resolves #987
Resolves #648

I was able to split the single JS file to source code + vendor assets and Was able to compress it as. I see issues with compression is implemented. django-webpack-loader depends on webpack-bundle-tracker

  1. index.html simply changes to
{% render_bundle 'main' 'js' %}
    {% render_bundle 'vendors' 'js' %}
  1. webpack-stats.json content is
{
  "status": "done",
  "chunks": {
    "main": [
      {
        "name": "main-e5185de39171058cfd22.js",
        "path": "/usr/src/app/assets/dist/main-e5185de39171058cfd22.js"
      }
    ],
    "vendors": [
      {
        "name": "vendors-e5185de39171058cfd22.js",
        "path": "/usr/src/app/assets/dist/vendors-e5185de39171058cfd22.js"
      }
    ]
  }
}
  1. When webpack is set on prod mode I can see that browser is loading the gzip content.
    Screen Shot 2020-07-28 at 2 48 17 PM
webpack_watcher    |       main-e5185de39171058cfd22.js   384 KiB     main  [emitted]  main
webpack_watcher    |    main-e5185de39171058cfd22.js.gz  47.1 KiB           [emitted]
webpack_watcher    |    vendors-e5185de39171058cfd22.js  16.1 MiB  vendors  [emitted]  vendors
webpack_watcher    | vendors-e5185de39171058cfd22.js.gz  1.49 MiB           [emitted]

The below screenshot show the download is based on the zipped file size
Screen Shot 2020-07-28 at 12 04 39 PM

  1. Webpack in watch mode a change on react code updates both main and vendor mode but compression of the vendor packages doesn't work. I am really not sure why this is the case

    1. With regards to webpack-bundle-tracker a new update is available but it has some breaking webpack-stats.json structure that python loader fails to load the json. Either I have to write my own Python loader class to support this as started here

With version "webpack-bundle-tracker": "^0.4.3", and django-webpack-loader==0.7.0 things kind of works but upgrading to webpack-bundle-tracker": "1.0.0-alpha.1 " breaks stuff. Need to spend sometime as their documentation is very basic and i have been looking at github issues to understand what exactly going on

@pushyamig
Copy link
Contributor Author

@justin0022 @jonespm Thoughts???

@jonespm
Copy link
Member

jonespm commented Jul 29, 2020

Thanks for the work. I wouldn't install any alpha modules, 0.4.3 looks like the last released version non-alpha or beta. Maybe create this for a future issue to look at.

Good idea on the split, Maybe we can also just ignore changes to the vendors since those files aren't going to change unless we do a full rebuild anyway. This should make the watch run a little faster too.

https://webpack.js.org/configuration/watch/#watchoptionsignored

@pushyamig
Copy link
Contributor Author

pushyamig commented Jul 29, 2020

Thanks for the work. I wouldn't install any alpha modules, 0.4.3 looks like the last released version non-alpha or beta. Maybe create this for a future issue to look at.

That's what going on in my mind. The guy who developed django-webpack-watcher is the sole maintainer and he is busy too and that kind of scares me relying on his package.

SO in prod mode .gz.js are served when a request to js file is made. This Post here states that Server usually doing that. I suppose Gunicorn server is doing by defaults. The WSGI server also does this when '--nostatic' flag set: ./manage.py runserver --nostatic

https://webpack.js.org/configuration/watch/#watchoptionsignored

That did not work

The problem I am trying to solve is to load the compressed files in the watch mode, this work only for first couple of times. In watch mode after few JS files changes vendor.js.gz file is not generated but main.js.gz , main.js and vendor.js does. Somehow browser load the uncompressed version of even main.js file when it see vendor.js.gz is not available. This will increase the load time in development. May be something in compression-webpack-pluginto look at?

@jonespm
Copy link
Member

jonespm commented Jul 29, 2020

Yeah, I suppose gunicorn is doing that.

I'd was planning to to remove the runserver and when PTVSD is enabled it can start up with gunicorn as long as there's only one worker. That would make this a little more consistent.

Yeah, I was thinking of some watchOptions like this, but might be related?

+  watchOptions: {
+    ignored: /node_modules/,
+    aggregateTimeout: 600
+  },

@pushyamig
Copy link
Contributor Author

pushyamig commented Aug 6, 2020

This post suggest that not to do hashing for generating the js/css etc in development since it will slows down the build.

When I don't generate js with hashing it solves 2 problem

  1. The dist folder is not cluttered with lost of hash file after changes to React code
  2. This kind of solves the problem that I stated here(look in the last paragraph), since it will always finds the verdor.js.gz and browser always load the gz file

The annoyance part every time we need to do a browser cache clean before browser reload.

Fact: When pkg's are updated in package.json watch mode don't pick up those changes any way and we need a docker build

@pushyamig
Copy link
Contributor Author

I could achieve optimization for Prod build, but for development on watch mode don't generated hashed zip file after couple of time. I really did not find a solution for this and I don't know if there is solution. More on this issue here

I am opening up this for a review, I appreciate any feedback on this

@pushyamig pushyamig marked this pull request as ready for review August 6, 2020 13:50
@jonespm
Copy link
Member

jonespm commented Aug 12, 2020

Codacy Here is an overview of what got changed by this pull request:

Issues
======
- Added 3
           

See the complete overview on Codacy

Copy link
Member

@lsloan lsloan left a comment

Choose a reason for hiding this comment

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

I have a minor suggestion and a question, see below…

@@ -12,4 +12,5 @@
<div id="root"></div>
<!-- Webpack rendered JS -->
{% render_bundle 'main' 'js' %}
{% render_bundle 'vendors' 'js' %}
{% endblock %}
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Add newline to end of file. I know it's not a change made for this PR, but it's an easy thing to catch and correct.

start.sh Show resolved Hide resolved
@pushyamig

This comment has been minimized.

@jonespm
Copy link
Member

jonespm commented Nov 5, 2020

I like this change and don't see too many other reviews on this. Can you update the start script conflicts @pushyamig ?

@pushyamig
Copy link
Contributor Author

pushyamig commented Nov 5, 2020

I forgot what I did with this PR since it has been a long time :) I will update and check back

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
MyLA-Fall-2020.03.03
Awaiting triage
Development

Successfully merging this pull request may close these issues.

Optimize Webpack size Create sourcemaps for front-end development
3 participants