This tutorial provides the steps to enable SASS or LESS support in your storefront project.
Use these Webpack configurations to add support for SASS and LESS alongside CSS Modules
Getting Started
This tutorial provides the steps to enable SASS or LESS support in your storefront project.
Use these Webpack configurations to add support for SASS and LESS alongside CSS Modules
Use a package manager, such as yarn or npm, to install the SASS loader as a dev dependency.
yarn add --dev sass-loader node-sass
Edit webpack.config.js and add a new config rule entry:
config.plugins = [ ...config.plugins, new DefinePlugin({ /** * Make sure to add the same constants to * the globals object in jest.config.js. */ POSSIBLE_TYPES: JSON.stringify(possibleTypes), STORE_NAME: JSON.stringify('Venia') }), new HTMLWebpackPlugin({ filename: 'index.html', template: './template.html', minify: { collapseWhitespace: true, removeComments: true } }) ]; + + config.module.rules.push({ + test: /\.s[ca]ss$/, + use: [ + 'style-loader', + { + loader: 'css-loader', + options: { + modules: true, + sourceMap: true, + localIdentName: '[name]-[local]-[hash:base64:3]' + } + }, + 'sass-loader' + ] + });
Create the myComponent.scss file:
$button-color: #ff495b; $button-color-hover: #ff9c1a; .root { padding: 15px; } .button { color: $button-color; &:hover { color: $button-color-hover; } }
Create a component and import the SASS file:
import React from 'react'; import defaultClasses from './myComponent.scss'; const MyComponent = () => ( <div className={defaultClasses.root}> <button className={defaultClasses.button}>My Component</button> </div> ); export default MyComponent;
Use a package manager, such as yarn or npm, to install the LESS loader as a dev dependency.
yarn add --dev less-loader less
Edit webpack.config.js and add a new config rule entry:
config.plugins = [ ...config.plugins, new DefinePlugin({ /** * Make sure to add the same constants to * the globals object in jest.config.js. */ POSSIBLE_TYPES: JSON.stringify(possibleTypes), STORE_NAME: JSON.stringify('Venia') }), new HTMLWebpackPlugin({ filename: 'index.html', template: './template.html', minify: { collapseWhitespace: true, removeComments: true } }) ]; + + config.module.rules.push({ + test: /\.less$/, + use: [ + 'style-loader', + { + loader: 'css-loader', + options: { + modules: true, + sourceMap: true, + localIdentName: '[name]-[local]-[hash:base64:3]' + } + }, + 'less-loader' + ] + });
Create the myComponent.less file:
@button-color: #ff495b; @button-color-hover: #ff9c1a; .root { padding: 15px; } .button { color: @button-color; &:hover { color: @button-color-hover; } }
Create a component and import the LESS file:
import React from 'react'; import defaultClasses from './myComponent.less'; const MyComponent = () => ( <div className={defaultClasses.root}> <button className={defaultClasses.button}>My Component</button> </div> ); export default MyComponent;
Thank you!
We will contact you shortly
Thank you!
We will contact you shortly
For Magento stores, PWA is just evolving and emerging. You have a chance to be the first among your competitors to squeeze the most out of it: speed, decrease in bounce rates, boosted conversions, and much more.
Sign up with your social media account
Please enter your email below and we will send you a reset email.