JavaScript resources in Magento
Overview
This topic describes the general concepts of how work with JavaScript components is organized in Magento.
To address the problem of slow page load, we exclude JavaScript from the page headers and we added the ability to use the RequireJS library.
RequireJS improves the perceived page load, time because it allows JavaScript to load in the background; in particular, because it enables asynchronous JavaScript loading.
Explore JavaScript resources
JS resources location
In Magento, you can find the JS components on the following levels:
- Library level (
lib/web
). Resources located here are available in any place in Magento. - Module level (
<module_dir>/view/<areaname>/web
). If the module is enabled, resources added here are available in other modules and themes. - Theme level, for a particular module (
<theme_dir>/<VendorName>_<ModuleName>/web
). Resources added here are available for inheriting themes. - Theme level (
<theme_dir>/web
). Resources added here are available for inheriting themes.
Library level can only contain core Magento resources. Do not put custom JS files in the lib/web
directory.
Specifying JS
We recommend specifying JavaScript resources in the templates rather than in the layout updates, to ensure processing of the resources in body of a page.
Accessing JS resources
JS resources are accessed using relative paths.
Examples:
Example 1
- File actual location:
app/code/Magento/ConfigurableProduct/view/frontend/web/js/configurable.js
- File published to
pub/static
:pub/static/frontend/Magento/<theme>/<locale>/Magento_Configurable/js/configurable.js
. Here<theme>
and<locale>
are the currently applied in your instance theme and locale. - Called in script:
- File actual location:
app/code/design/frontend/Magento/blank/web/js/theme.js
- File published to
pub/static
:pub/static/frontend/Magento/<theme>/<locale>/js/theme.js
- Called in script:
- File actual location:
lib/web/jquery.js
- File published to
pub/static
:pub/static/<area>/Magento/<theme>/<locale>/jquery.js
- Called in script:
These relative paths are also used in for mapping and setting paths
in requirejs-config.js configuration files.
Dependencies between JavaScript resources
To build a dependency on the third-party plugin, specify a shim in the following configuration files:
requirejs-config.js
<third-party-plugin>.js
RequireJS library
Including RequireJS
To be available for the entire Magento instance, RequireJS library is included in the following layout files:
-
For the
adminhtml
area:
- For the
frontend
area the similar configuration is located inapp/code/Magento/Theme/view/frontend/layout/default.xml
.
Mapping JS resources
To make the configurations more precise and specific for different modules/themes, requirejs-config.js
files can be placed in different locations depending on your needs.
All configurations are collected and executed in the following order:
- Library configurations.
- Configurations at the module level.
- Configurations at the theme module level for the ancestor themes.
- Configurations at the theme module level for a current theme.
- Configurations at the theme level for the ancestor themes.
- Configurations at the theme level for the current theme.
Dependencies between the modules or themes are considered as well.
The baseUrl
parameter for RequireJS is specified in the following files:
- for the
frontend
area: app/code/Magento/Theme/view/frontend/templates/page/js/require_js.phtml - for the
adminhtml
area: app/code/Magento/Backend/view/adminhtml/templates/page/js/require_js.phtml