question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Support] - How to add less loader

See original GitHub issue

Thank you for this great plugin.

I’m trying to implement ant.design library with the latest version of create react app and craco. I manged to implement the babel import loader, but i’m currently stuck add implementing the less loader.

Ant design documentation on how to implement less loader https://ant.design/docs/react/customize-theme https://ant.design/docs/react/use-with-create-react-app

My implementation with craco:

module.exports = {
  style: {
    less: {
      loaderOptions: {
        modifyVars: {
          'primary-color': '#1DA57A',
          'link-color': '#1DA57A',
          'border-radius-base': '2px',
         },
       javascriptEnabled: true,
      }
    },
  },
  babel: {
    plugins: [
      ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": true}, "ant"],
    ],
  },
}

I think that I misunderstand the documentation on how to add less-loader support with craco. Any help is welcome!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
jaredjj3commented, Nov 7, 2018

I’ve Frankensteined together a craco.config.js that accomplishes what you want. This can be extracted into a plugin. I used a fresh install using react-scripts 2.1.1.

// https://github.com/timarney/react-app-rewired/blob/master/packages/react-app-rewire-less/index.js
const path = require('path');

const loaderNameMatches = function(rule, loaderName) {
  return rule && rule.loader && typeof rule.loader === 'string' &&
    (rule.loader.indexOf(`${path.sep}${loaderName}${path.sep}`) !== -1 ||
    rule.loader.indexOf(`@${loaderName}${path.sep}`) !== -1);
};

const getLoader = function(rules, matcher) {
  let loader;

  // Array.prototype.some is used to return early if a matcher is found
  rules.some(rule => {
    return (loader = matcher(rule)
      ? rule
      : getLoader(rule.use || rule.oneOf || (Array.isArray(rule.loader) && rule.loader) || [], matcher));
  });

  return loader;
};

module.exports = {
  babel: {
    plugins: [
      [
        'import',
        {
          'libraryName': 'antd',
          'libraryDirectory':
          'es',
          'style': true
        },
        'ant'
      ],
    ],
  },
  plugins: [
    {
      plugin: {
        overrideWebpackConfig: ({ webpackConfig }) => {
          const lessExtension = /\.less$/;

          const fileLoader = getLoader(
            webpackConfig.module.rules,
            rule => loaderNameMatches(rule, 'file-loader')
          );
          fileLoader.exclude.push(lessExtension);

          const lessRules = {
            oneOf: [{
              test: lessExtension,
              use: [
                {
                  loader: require.resolve('style-loader')
                }, {
                  loader: require.resolve('css-loader')
                }, {
                  loader: require.resolve('less-loader'),
                  options: {
                    modifyVars: {
                      '@primary-color': '#1DA57A',
                      '@link-color': '#1DA57A',
                      '@border-radius-base': '2px',
                    },
                    javascriptEnabled: true
                  }
                }
              ]
            }]
          }
        
          const oneOfRule = webpackConfig.module.rules.find(rule => (
            typeof rule.oneOf !== 'undefined'
          ));
          const appendTo = oneOfRule ? oneOfRule.oneOf : webpackConfig.module.rules;
          appendTo.push(lessRules);

          return webpackConfig;
        }
      }
    }
  ]
};
2reactions
jaredjj3commented, Nov 6, 2018

It looks like the current implementation does not support a less loader yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

less-loader | webpack - JS.ORG
webpack provides an advanced mechanism to resolve files. less-loader applies a Less plugin that passes all queries to the webpack resolver if less...
Read more >
using less-loader in webpack - Stack Overflow
I have decided I want to start using LESS as well and I have tried to add the less-loader as per the instructions:....
Read more >
[Support] - How to add less loader · Issue #5 · dilanx/craco
I'm trying to implement ant.design library with the latest version of create react app and craco. I manged to implement the babel import...
Read more >
less-loader - webpack
To begin, you'll need to install less-loader : $ npm install less-loader --save-dev. Then add the loader to your webpack config. For example:....
Read more >
Webpack and LESS. How to make them play along nicely
First, the less-loader is run. This passes the content of the file on to the LESS compiler, which then returns compiled CSS. Second,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found