Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add opt-in support for LESS to webpack config
Includes also the possibility to pass options to the
less loader through en environment variable.
  • Loading branch information
hbj committed Jun 28, 2021
commit b2029f2d95d8214d3dc6ee546d766932986925de
48 changes: 47 additions & 1 deletion packages/react-scripts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ const cssRegex = /\.css$/;
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
const lessRegex = /\.less$/;
const lessModuleRegex = /\.module\.less$/;

// style loaders options
const lessLoaderOptions = JSON.parse(process.env.LESS_LOADER_OPTIONS || null);

const hasJsxRuntime = (() => {
if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') {
Expand Down Expand Up @@ -107,7 +112,7 @@ module.exports = function (webpackEnv) {
const shouldUseReactRefresh = env.raw.FAST_REFRESH;

// common function to get style loaders
const getStyleLoaders = (cssOptions, preProcessor) => {
const getStyleLoaders = (cssOptions, preProcessor, preProcessorOptions) => {
const loaders = [
isEnvDevelopment && require.resolve('style-loader'),
isEnvProduction && {
Expand Down Expand Up @@ -163,6 +168,7 @@ module.exports = function (webpackEnv) {
loader: require.resolve(preProcessor),
options: {
sourceMap: true,
...(preProcessorOptions || {}),
},
}
);
Expand Down Expand Up @@ -592,6 +598,46 @@ module.exports = function (webpackEnv) {
'sass-loader'
),
},
// Opt-in support for LESS (using .less extension).
// By default we support LESS Modules with the
// extension .module.less
{
test: lessRegex,
exclude: lessModuleRegex,
use: getStyleLoaders(
{
importLoaders: 3,
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
},
'less-loader',
lessLoaderOptions
),
// Don't consider CSS imports dead code even if the
// containing package claims to have no side effects.
// Remove this when webpack adds a warning or an error for this.
// See https://github.com/webpack/webpack/issues/6571
sideEffects: true,
},
// Adds support for CSS Modules, but using LESS
// using the extension .module.less
{
test: lessModuleRegex,
use: getStyleLoaders(
{
importLoaders: 3,
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
modules: {
getLocalIdent: getCSSModuleLocalIdent,
},
},
'less-loader',
lessLoaderOptions
),
},
// "file" loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
Expand Down
1 change: 1 addition & 0 deletions packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"jest-circus": "26.6.0",
"jest-resolve": "26.6.0",
"jest-watch-typeahead": "0.6.1",
"less-loader": "^7.3.0",
"mini-css-extract-plugin": "0.11.3",
"optimize-css-assets-webpack-plugin": "5.0.4",
"pnp-webpack-plugin": "1.6.4",
Expand Down