-
Notifications
You must be signed in to change notification settings - Fork 57
refact: JS size optimization #97
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
Open
alexrails
wants to merge
1
commit into
hardcode-dev:master
Choose a base branch
from
alexrails:homework_6
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Case-study оптимизации | ||
|
||
## Актуальная проблема | ||
В проекте `dev.to` есть определенная проблема. Анализ показывает, что на всех страницах загружается файл `vendor.js`. | ||
|
||
Этот файл содержит библиотеку moment.js, печально известную своим большим размером. | ||
|
||
У нас уже была программа на `ruby`, которая умела делать нужную обработку. | ||
|
||
## Формирование метрики | ||
В качестве метрики берем объём `js` на главной странице. | ||
|
||
В файле `homeBudget.json` устанавливаем бюджет в 460000 байт. | ||
|
||
## Анализ | ||
С помощью `sitespeed.io` убеждаемся, что бюджет пока не соблюдается: | ||
`docker run --privileged --rm -v "$(pwd)":/sitespeed.io sitespeedio/sitespeed.io http://host.docker.internal:3000/ -n 1 --budget.configPath homeBudget.json` | ||
|
||
 | ||
|
||
С помощью `webpack-bundle-analyzer` убедился, что `moment.js` входит в сборку `vendor`: | ||
|
||
 | ||
|
||
Закомментировал содержимое файла `proCharts.js`. `web-bundle-analyzer` показал что `moment` пропал из `vendor`: | ||
|
||
 | ||
|
||
|
||
## Оптимизация | ||
В первую очередь я раскомментировал содержимое файла `proCharts.js`. | ||
|
||
Проанализировав отчеты и код проекта я выяснил что `proCharts.js` использует `chart.js`, которая, в свою очередь, внутри использует `moment.js`. | ||
Поэтому `moment` попадает в `vendor` вместе с `chart.js`, и они оказываются связанными. | ||
|
||
В качестве оптимизации отредактировал `CommonsChunkPlugin` в `environment.js` путём исключения `chart.js` и `moment.js` из `vendor` сборки. | ||
Этим мы можем гарантировать что теперь они будут подключаться только там, где непосредственно используются. | ||
``` | ||
!module.context.includes('chart.js') && !module.context.includes('moment') | ||
``` | ||
С помощью `web-bundle-analyzer` убедился, что это сработало: | ||
|
||
 | ||
|
||
Однако `sitespeed.io` показал что бюджет еще не выполнен: | ||
 | ||
|
||
В `webpack-bundle-analyzer` заметил, что в `vendor` остались 2 зависимости `chartjs-color` и `chartjs-color-string`. Поэтому их также исключил из `vendor`. | ||
|
||
Теперь бюджет выполняется: | ||
 | ||
|
||
## CI | ||
Защитил оптимизацию, настроив CI Github Actions: | ||
https://github.com/alexrails/rails-optimization-task6/actions/runs/13750720227 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"budget": { | ||
"transferSize": { | ||
"javascript": 460000 | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: sitespeedio | ||
on: [push] | ||
jobs: | ||
build: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше check_js_size или что-то такое |
||
runs-on: ubuntu-latest | ||
steps: | ||
- name: checkout | ||
uses: actions/checkout@v4 | ||
- name: run sitespeed.io | ||
run: docker run --privileged --rm -v "$(pwd):/sitespeed.io" sitespeedio/sitespeed.io https://03d1-2a0b-6204-12ef-c100-6424-8e72-d575-70d0.ngrok-free.app -n 1 --budget.configPath .dev_to/sitespeed/homeBudget.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
👍