A small web application, such as a theme, can include rich user interactions that depend on many small JavaScript and CSS modules. If we were to embed each of the JavaScript modules in a template file with a separate <script> tag, the shopper’s browser would need to make separate HTTP requests to retrieve each module.
In some cases, it would take longer to set up the HTTP request than to download the small JavaScript module, leading to slower load times. On mobile devices, slow load times can be especially frustrating.
To solve this problem Stencil, like other modern front-end frameworks, bundles all the JavaScript modules into a single file, allowing the shopper’s browser to make only a single HTTP request. After the browser has downloaded the bundle of JavaScript modules the browser caches them, speeding up the rest of the shopper’s session.
Beyond reducing the number of HTTP calls required to fetch all the required JavaScript modules, we can reduce the size of the individual JavaScript modules through minification. JavaScript minification removes white space and comments, shortens variable and function names, removes dead code, and more. The goal in all cases is to reduce the amount of bandwidth necessary to transmit the JavaScript module to the browser.
When you add JavaScript to a theme, use one of the following techniques, so that Stencil will automatically bundle and minify your modules.
These techniques are outlined in detail in the next sections.
Many third-party JavaScript components are distributed with npm. When you use the npm command-line utility to add a JavaScript component to your theme, Stencil will automatically bundle and minify the component.
To enable this bundling and minification, run each module’s npm install command from the root directory of your theme (only for the module you want to add).
The npm package manager facilitates the management of third-party JavaScript components by placing each JavaScript component in the correct directories.
However, as a developer you will still need to edit your theme files to wire up the JavaScript component to expose it on your storefront.
Stencil themes include an API for running JavaScript on a per-page basis. To properly write JavaScript for your theme, you will have the following page types available to you:
For further information, you can view a list of all the page types identified by app.js in Cornerstone.
These page types correspond to the pages within your theme. Each page type maps to an ES6 module that extends the base PageManager abstract class.
Example:
Note that the class name (“Blog”) is arbitrary. Mapping in the pageClasses object in assets/js/app.js is what determines which PageManager subclass will load on that page type.
Developers can freely create subdirectories within assets/js/, to contain new JavaScript modules. The constraint is that all JavaScript files in each module must use the .js file extension.
In some advanced cases, it is possible to use other file types if you reconfigure webpack to transpile them. The blog below demonstrates how to reconfigure webpack to transpile .jsx files so that you can implement React code directly in the theme.

As a Solution Architect at BigCommerce, I meet clients and agencies who want to incorporate React into their Stencil theme. Maybe you want to utilize your existing component library, or React is just your preferred front-end framework.
In your theme’s assets/js/theme/ subdirectory, you will find a tree of JavaScript files.
Each file is a JavaScript module. Some modules are for specific page types. Others are common modules that can be used in other modules. Still, others are global modules that are available on every page.
To find the mapping from page types to modules in assets/js/theme/, examine the pageClasses object in the file:assets/js/app.js.
Each =>import(…) function within this class maps a page type to the entry module for that page type. For example, when the cart page type is loaded in the browser the JavaScript module at assets/theme/js/cart.js will be loaded.
Mapping example in app.js
Below is an excerpt of mappings from the Cornerstone base theme’s assets/js/app.js.
Mapping example in cart.js
Inside the cart module (assets/js/theme/cart.js) other modules are imported, and custom JavaScript methods for the cart module are created for the Cart class.
Below is an excerpt from Cornerstone’s assets/js/theme/cart.js file. See the full code here.
If you add custom page templates to your theme, you can edit the same assets/js/app.js file to map each custom template to an appropriate JavaScript module.
In order to successfully map your custom module to a custom template file, that file must do the following:
In the following example, we will map a custom JavaScript file, assets/js/theme/custom.js to a custom product page file templates/pages/custom/product/customProd.html.
This is a basic module that creates a class called Custom which extends the PageManager class.
The sample code below is the custom template that would live somewhere in templates/pages/custom/product/*.
It is a good idea to pull in {{>layout/base}} to your custom template file because of these requirements.
Finally, use the customClasses function in assets/js/app.js to map the custom page file to your custom template. Your app.js file should look like this:
To review, the basics of using JavaScript in your Stencil theme are: