This Webpack config was created to replace Slate by Shopify. This workflow uses ThemeKit but offers you the ability to use modern JavaScript and create template specific JS and CSS bundles. While it's possible to migrate an existing theme to this workflow, the intention and real benefit comes when new themes are built from scratch using this setup. Please post general questions in the Discussions tab in GitHub.
- All .scss files created under
src/styles/sections/
will be processed withnode-sass
and compiled to a .css file with asection.
prefix (See example files indist
). These files will be bundled intodist/assets
. - Note: Shopify's preferred method of including section file css is to add a liquid stylesheet tag at the top fo each section file. See example section file (Approach taken from the new Dawn theme).
- JSON templates will now be ignored by ThemeKit via the config.yml file. This is due to the fact that section setting content is stored within the template.json file. Allowing ThemeKit to push new versions of this file would overwrite any changes made in section settings from the theme customizer. If you use a deployment tool like Buddy or Beanstalk, be sure to add
templates/*.json
as an ignore there as well.
- In a new project directory, run Theme Kit's 'new' command to generate a new theme on your store. After that theme has been uploaded to your store, delete the new local theme files that Theme Kit downloaded to your directory as you won't need them. The starter liquid files included
dist/
are the same files you'll get when generating a new theme with Theme Kit. - Download and unzip this repo, and move all files into your new directory created in step 1.
- Open terminal, cd into your project directory, and and run
npm install
to install packages. - In Shopify, copy the theme ID for the new theme, then update the
<PASSWORD>
,THEME_ID
, andSTORE_URL
in config.yml with your store & theme details. - Your
config.yml
file should look like this:development: password: <PRIVATE_APP_PASSWORD> theme_id: "<THEME_ID>" store: <STORE_URL> directory: dist/ ignore_files: - config/settings_data.json
- If migrating an existing theme, copy over all assets, liquid and config files from your theme into their respective directories in
src/
. - Run
npm start
to run your first Webpack build and start watching for file changes to be uploaded to Shopify.
npm start
- Completes a Webpack build in development mode
- Webpack begins watching for file changes
- Theme Kit begins watching for file changes in
dist/
- Theme Kit opens your development theme in your default browser
npm run build
- Completes a Webpack build in production mode
npm run deploy
- Completes a Webpack build in production mode
- Deploys dist folder to the development theme in
config.yml
All JavaScript files in the js/bundles
directory & subdirectories are used as entry points. All other JavaScript modules should added to additional subdirectories of js/
. An entry point file must be created for each liquid template file, including alternate templates. A CSS file for each template and layout should also be added to styles/layout
and styles/templates
. These CSS files should be imported at the top of each JavaScript entry file.
Webpack will generate a JavaScript file for each template and layout file in the bundles
directory. The CSS files imported in each bundle entry file will also generate CSS files. Webpack will add all output files to dist/assets
.
The Theme Kit configuration file uses dist
as the root directory for watching files to upload.
When running npm start
, Webpack will use a plugin that runs shopify-themekit watch
after a successful build. Webpack will then watch and recompile entry file changes, and Theme Kit will watch for file changes in the dist
directory.
- The layout and template entry files in
src/js/bundles/
are necessary for Webpack to generate the CSS and JavaScript assets for each layout and template. Additional entry files will be required when creating new liquid templates or alternate templates, ie.page.about.js
. - The
style-bundle.liquid
andscript-bundle.liquid
snippets output dynamic asset URLs based on current layout and template. These have been added to sampletheme.liquid
. Thelayout
variable is required.
If your store is on Shopify Plus, you'll need to do the following:
- Create
checkout.scss
and add tosrc/styles/layout/
. - Create
checkout.js
and add tosrc/js/bundles/layout/
. - Add
import "Styles/layout/checkout.scss";
incheckout.js
. - Render the style-bundle and script-bundle snippets in
checkout.liquid
by changing the snippet's layout variable value tocheckout
. ie.{% render 'style-bundle', layout: 'checkout' %}
and{% render 'script-bundle', layout: 'checkout' %}
.
- Subdirectories are allowed in
assets/
,js/
,styles/
,snippets/
. - A
Styles
module alias for the styles directory is ready to use. ie.import "Styles/layout/theme.scss"
. - To reference an asset url in an SCSS file such as a background image, just use
./filename.ext
, since all final CSS and images live in thedist/assets/
directory. - If you add a new JavaScript entry file to
js/bundles/
while Webpack and Theme Kit are watching for changes, you'll need to end the process and runnpm start
again so that Webpack is aware of the new entry file. - A git pre-commit hook is installed that will run
webpack build
prior to the commit. This is useful if using a code deployment tool so that you never push and deploy an unbuilt theme. clean-webpack-plugin
was intentionally not included to make incremental deployments faster using Buddy. If you remove a bundle entry file, you'll also need to delete the bundle files fromdist/assets
.- If you update or switch node versions using
nvm
, you will need to runnpm rebuild node-sass
to refresh node-sass for your current environment. - When merging 2 git feature branches, you only need to resolve the conlficts inside
src/
. Any conflicts insidedist/
can be resolved withnpm run build
. Always runnpm run build
after resolving merge conflicts. - Vendor CSS and JS files can be added to
src/assets/vendor
, and will be copied intodist/assets
.
A few issues with this workflow that I'm working on a solution for:
- If a Webpack entry file is deleted, how to also remove the generated output files from
dist/assets/
. Theclean-webpack-plugin
removes the entire dist folder which git tracks as new changes to every file in the directory, so that is not an option. - Currently, if the same vendor module is imported in a layout and template entry file, that code will be included twice. How to split out vendor file imports but also make them available in the necessary modules.
- Removes Babel. Replaces with
eslint-plugin-compat
&.eslintrc
(only polyfill what is necessary for the browsers your project requires). - Adds
defer
to layout and template script bundles, and moves to<head>
.
- Fixes bug with account page style and JavaScript bundles. Updates the
style-bundle.liquid
andscript-bundle.liquid
snippets to removecustomers/
from the asset URL output as this is not included in the filename that webpack generates.
- Moves
templates/
,sections/
,snippets/
andlayout/
directories into aliquid/
directory insrc/
. - Updates copy plugin in
webpack.config.js
to accommodate the new liquid directory structure.
- Adds apps.js and apps.scss. These assets will load in your theme layout JS and CSS on pages where apps run and a Shopify template is not used. (ie. ___.myshopify.com/apps/store-locator)
- Updates style-bundle.liquid and script-bundle.liquid to include app.js and app.scss for template-less pages
- Updates style-bundle.liquid and script-bundle.liquid to remove string filter for capturing template name and suffix
- Uninstalls unnecessary npm dependencies