Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add less loader #504

Open
farhanmalhi-dbank opened this issue Mar 29, 2023 · 1 comment
Open

How to add less loader #504

farhanmalhi-dbank opened this issue Mar 29, 2023 · 1 comment
Labels
feature request New feature or request

Comments

@farhanmalhi-dbank
Copy link

farhanmalhi-dbank commented Mar 29, 2023

(clearly describe the functionality you think CRACO should have)
this is the config

const { theme } = require('antd/lib');
const { convertLegacyToken } = require('@ant-design/compatible/lib');

const { defaultAlgorithm, defaultSeed } = theme;
const mapToken = defaultAlgorithm(defaultSeed);
const v4Token = convertLegacyToken(mapToken);

module.exports = {
  webpack: {
    configure: (webpackConfig, { env, paths }) => {
      const lessRegex = /\.less$/;
      const lessModuleRegex = /\.module\.less$/;

      const getStyleLoaders = (cssOptions, preProcessor) => {
        const loaders = [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader',
            options: cssOptions
          }
        ];

        if (preProcessor) {
          loaders.push(preProcessor);
        }
        return loaders;
      };

      webpackConfig.module.rules.push(
        {
          test: /\.mjs$/,
          include: /node_modules/,
          type: 'javascript/auto'
        },
        {
          test: /\.(ts|tsx)$/,
          exclude: /node_modules/,
          use: [
            {
              loader: 'babel-loader'
            },
            {
              loader: 'ts-loader',
              options: {
                transpileOnly: true
              }
            }
          ]
        },
        {
          test: lessRegex,
          exclude: lessModuleRegex,
          use: getStyleLoaders(
            {
              importLoaders: 2
            },
            {
              loader: 'less-loader',
              options: {
                lessOptions: {
                  modifyVars: v4Token,
                  javascriptEnabled: true
                }
              }
            }
          )
        },
        {
          test: lessModuleRegex,
          use: getStyleLoaders(
            {
              importLoaders: 2,
              modules: {
                localIdentName: '[local]_[hash:base64:5]'
              }
            },
            {
              loader: 'less-loader',
              options: {
                lessOptions: {
                  modifyVars: v4Token,
                  javascriptEnabled: true
                }
              }
            }
          )
        }
      );

      return webpackConfig;
    }
  }
};

it is not picking my styles in .less files.

FYI
Reason for not using craco-less is latest version of craco-less supports craco 6 version which has webpack 4
i want to use webpack 5.

@farhanmalhi-dbank farhanmalhi-dbank added the feature request New feature or request label Mar 29, 2023
@dilanx
Copy link
Owner

dilanx commented Apr 11, 2023

Have you checked out the less-loader documentation? Ideally, following those instructions then adding the relevant webpack config contents to the craco config should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants