Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

magepack

magesuite13.9kOSL-2.02.11.2

Version 2.0.0 of Magepack released, with greater performance results and easier usage!

readme

Magepack 🚀

Version 2.0.0 of Magepack released, with greater performance results and easier usage!

Magepack is a bold attempt in making Magento 2 frontend as fast as never before. It builds on experiences gained with Advanced JavaScript bundling guide and Baler to provide the best of both worlds - ease of use and superior performance.

Top highlights*

  • Up to 91 points mobile score in Google Lighthouse.
  • Up to 98% reduction in JavaScript file requests (from 177 to only 3).
  • Up to 44% reduction in transferred JavaScript size.
  • Up to 75% reduction in total load time.
  • Works with Magento's JavaScript minification and merging enabled.
  • Uses custom solution (inspired by Baler) instead of RequireJs optimizer which is way more flexible, faster, produces smaller bundle and doesn't break on missing files.

* All data gathered on clean Magento 2 with sample data installed.

Installing

Here are the requirements for Magepack to work:

  1. You need Node.js version 10 or higher installed.
  2. If you are using Magento 2.3.5 or lower, you need to have mixins.js module patched (patch provided and explained here).
  3. If you are using Magento 2.3.3 or lower, you need jquery.cookie module shim (patch provided and explained here).
  4. Magepack Magento module installed.

Install with npm:

npm install -g magepack

Install with yarn:

yarn global add magepack

Usage

Usage: magepack [generate|bundle] <options...>

Options:
  -v, --version       Output the current version.
  -h, --help          Show this command summary.

Commands:
  generate [options]  Generate optimization configuration based on given page URLs.
  bundle [options]    Bundle JavaScript files using given configuration file.

Generating bundler configuration

First step is to run the generation against the existing, working shop. You can do it on any machine with access to the target shop, even your own computer. The goal here is to collect all of the RequireJS dependencies needed for a certain type of page layout. Currently, following bundles are prepared:

  • cms containing modules needed by CMS pages.
  • category containing modules needed by category pages.
  • product containing modules needed by product pages.
  • checkout containing modules needed by cart and checkout pages.

In addition, there is the common bundle created by extracting all modules needed by each of above and loaded on every page.

Running the generator

magepack generate --cms-url="{{CMS_PAGE_URL}}" --category-url="{{CATEGORY_PAGE_URL}}" --product-url="{{PRODUCT_PAGE_URL}}"

There are 3 required options you need to pass:

--cms-url - URL to one of CMS pages (e.g. homepage).

--category-url - URL to one of category pages.

--product-url - URL to one of product pages.

Note: By default, Magepack will use given product page, add this product to the cart and visit both cart and checkout pages to collect dependencies. To avoid this, use the --skip-checkout option.

Running the above command will generate magepack.config.js file, where you can find each of the prepared bundles with the list of modules that will be included in them.

Bundling

Once you have generated bundler configuration, the next step would be to trigger the actual optimization after static content deploy stage has finished by running the following in shop root directory:

magepack bundle

This command will iterate over each deployed locale (excluding Magento/blank) and prepare bundles for each of them.

There are multiple optional params you can set:

-c, --config - defining the configuration file path, in case you have multiple configuration files (e.g multiple themes with individual configuration files)

-g, --glob - defining where to look for locales to bundle.

-s, --sourcemap - enables sourcemap generation for bundled js.

-m, --minify - overrides Magento 2 JS minification setting, minifying the bundle using Terser (used by default if Magento 2 JS minification is enabled).

Sourcemaps

It is possible to enable sourcemaps for bundled JS files, using the -s, --sourcemap flag with magepack bundle command. However, there are couple of caveats:

  • It does not respect existing sourcemaps for individual JS files (possible future update)
  • For sourcemaps to be meaningful, Magento 2 JS minification must be turned off. This is because Magento 2 does not (and cannot with current PHP implementation) generate sourcemaps for each minified JS file. For this reason, a separate -m, --minify flag exists to minify the resulting bundle using Terser.

Enabling

Once you made sure Magepack Magento module is installed, what is left is to enable it via admin panel under Stores->Configuration->Advanced->Developer or CLI:

bin/magento config:set dev/js/enable_magepack_js_bundling 1

and clearing the cache:

bin/magento cache:clean

Now the shop should be way faster then before 🚀 You can (and should) even enable all Magento's performance optimizations (except JavaScript bundling of course) for even better results.

Results

Here are our tests results, testing homepage on local development environment with clean Magento 2.3.4, sample data, all caches enabled and following optimizations:

  • JavaScript:
    • Merge JavaScript Files - Yes.
    • Enable JavaScript Bundling - No.
    • Minify JavaScript Files - Yes.
    • Move JS code to the bottom of the page - Yes.
  • CSS:
    • Merge CSS Files - Yes.
    • Minify CSS Files - Yes.
    • Use CSS critical path - Yes.
  • Templates:
    • Minify Html - Yes.

No Bundling

Lighthouse report with 53 score

Bundling with Baler

Please note that Baler does not currently support Magento's JavaScript merging and minification.

Lighthouse report with 64 score

Bundling with Magepack

Lighthouse report with 91 score

Debugging

Before rising an issue please follow below guidelines:

  • Problems with bundling:
    • Make sure there are no JavaScript errors thrown by the shop which may prevent Magepack from collecting the dependencies.
  • Problems with generation:
    • Make sure all locales are properly deployed and no files are missing.
  • Try running the command with --debug or -d flag that makes Magepack output more information about the ongoing process which should make debugging easier.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

This project is licensed under the OSL-3.0 license - see the LICENSE.md file for details

Acknowledgments

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

2.11.1

25 October 2022

2.11.0

24 October 2022

  • Correct detection of anonymous AMD modules #142
  • Block magepack during 'generate' command #151
  • Run Jest test suite in GitHub Action #160
  • Update CHANGELOG 476e995
  • Release 2.11.0 772dde2

2.10.1

19 October 2022

2.10.0

19 October 2022

2.9.0

19 October 2022

2.8.0

4 February 2022

  • Add options to generate source-maps and minify output irrespective of Magento 2 JS minification setting #132
  • Update README.md 43105d8
  • Release 2.8.0 7b6dc1c

2.7.2

6 September 2021

2.7.1

6 September 2021

  • Fix selection of super attributes not propagating #123
  • Update CHANGELOG.md bfc335b
  • Release 2.7.1 c87f3ef

2.7.0

24 August 2021

  • Add option to skip checkout bundle generation #121
  • Run ESLint with GitHub Actions #119
  • Update CHANGELOG.md 4c2fe66
  • Update CHANGELOG.md 1fc3211
  • Release 2.7.0 24aa5b4

2.6.0

22 August 2021

  • Update README.md #96
  • Add --config param to Bundling section of README #97
  • Handle URLs without scheme #118
  • Update CHANGELOG.md 6edaa0e
  • Release 2.6.0 69f532f

2.5.0

8 August 2021

  • Exit with a proper status code on error #114
  • docs(README): Add a note that mixin.js is patched on Magento >= 2.3.6 #70
  • Release 2.5.0 8019991

2.4.1

26 November 2020

  • Pass the glob param from CLI down to the bundle function #68
  • Update changelog 9341320
  • Pass the glob param from CLI down to the bundle fuctions 811a6cd
  • Release 2.4.1 45a91f3

2.4.0

14 November 2020

  • Add support for bundling only themes defined by glob pattern #63
  • Add support for compact static deploy mode #40
  • Bump lodash from 4.17.15 to 4.17.19 #41
  • Handle add to cart of simple configurable products #39
  • Add tests 53431ef
  • Add support for bundling themes defined by glob pattern f42b5c9
  • Update changelog e97626a

2.3.2

3 May 2020

2.3.1

16 April 2020

  • Create bundle directory only if it doesn't exist yet a5d2eed
  • Release 2.3.0 a5f79aa
  • Release 2.3.1 5592af1

2.3.0

14 April 2020

  • Ignore certificate errors for easier generation against local environments #12
  • Update dependencies, remove need for fs-extra #7
  • Update CHANGELOG fcd8aa7
  • Release 2.3.0 1e97d5a

2.2.1

9 April 2020

  • Run puppeteer with --no-sandbox, fixes #6 #9
  • Fix UnhandledPromiseRejectionWarning during generation when closing the page #10
  • Merge pull request #9 from Hector68/master #6
  • Release 2.2.1 5721814
  • page close fix 2c91246
  • 6 run puppeteer with --no-sandbox 6aebd5d

2.2.0

8 April 2020

2.1.2

4 April 2020

  • Don't sort modules when collecting them bfc99c8
  • Release 2.1.2 32a994a

2.1.1

4 April 2020

  • Update CHANGELOG 4c3f795
  • Fix incorrect path generation when minification is disabled eedb372
  • Release 2.1.1 df31d61

2.1.0

3 April 2020

  • Fix swatches selection, add support for basic auth 5a16186
  • Add support for disabled minification e9ad6a9
  • Release 2.1.0 2a20b5a

2.0.1

1 April 2020

2.0.0

1 April 2020

1.4.0

31 October 2019

  • Fix module imports by exposing them through main file 6100148
  • Update changelog 16a406d
  • Release 1.4.0 305bf02

1.3.3

31 October 2019

1.3.2

31 October 2019

1.3.1

31 October 2019

1.3.0

31 October 2019

1.2.1

9 July 2019

1.2.0

8 July 2019

1.1.0

3 July 2019

1.0.3

11 June 2019

1.0.2

11 June 2019

  • Add small info about Puppeteer requirements 6354ebc
  • Release 1.0.2 8a1b73e

1.0.1

11 June 2019