Compress and lazy load locales
Compress and lazy load locales
Pull Request Type
- [x] Feature Implementation
Description This pull request adds a custom webpack plugin to compress the language files with brotli compression for the electron build. I tested gzip, brotli, zip and deflate, as those are the compression algorithms supported by node's built-in zlib library and brotli resulted the best compression ratios. While testing I also noticed that I could get the files even smaller by converting them from YAML to JSON, so the build process takes care of that as well. I also made the relevant changes to decompress and parse the JSON files at runtime.
I also implemented lazy loading of the languages which means that only the ones that are needed are actually loaded. At startup the language that the user selected and en-US are loaded, as en-US is the fallback if other languages are missing translations. When you change language in the FreeTube settings, the newly selected language is only loaded into memory if it wasn't already loaded.
As that language names are all inside the language files the build step also extracts the language names, which are then passed on to the define plugin to replace the process.env.LOCALE_NAMES variable.
yarn dev will still behave the same as it did before, so that the development process isn't slowed down by preprocessing the language files.
The language files are intentionally not compressed for the web build, as the webserver or cdn will usually automatically work out what compression algorithms the user's web browser supports and compress them for transit to the user's web browser. GitHub pages for example automatically serves static files from a cdn.
@MarmadileManteater
For the web build the languages are loaded using fetch, this should mean that you don't need to include then in your zip file for the in-memory filesystem anymore. You probably can't get rid of the filesytem shim fully though as the country files are still loaded using the filesytem APIs.
For some reason webpack doesn't optimise out the js-yaml import in the release build, I'll look into this separately later on, as it probably happens with other stuff as well.
Screenshots (if appropriate)
before:

after:

Testing (for code that is not small enough to be easily understandable)
yarn dev and yarn build
Desktop (please complete the following information):
- OS: Windows
- OS Version: 10
- FreeTube version: 0.17.1
This pull request has conflicts, please resolve those before we can evaluate the pull request.
Conflicts have been resolved. A maintainer will review the pull request shortly.