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.

module.less not work

See original GitHub issue

I want to use module.less in my app, but it doesn’t work, the style was not applied. please help, this is my craco.config.js file: craco-less version: 1.17.1

const CracoLessPlugin = require('craco-less');

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            modifyVars: { '@primary-color': '#004DE5' },
            javascriptEnabled: true,
          },
        },
        modifyLessRule: function(lessRule, _context) {
          lessRule.test = /\.(module)\.(less)$/;
          lessRule.exclude = /node_modules/;

          return lessRule;
        },
        cssLoaderOptions: {
          modules: { localIdentName: "[local]_[hash:base64:5]" }
        }
      }
    },
  ],
};

and this is my tsx and style file, they were in the same dir. AppHeader.tsx import './AppHeader.module.less'; // import style file

AppHeader.module.less

.site_layout_header {
  display: flex;
  align-items: center;
  height: 64px;
  padding: 0 8px;
  justify-content: space-between;
  background-color: @primary-color;
}

.site_logo {
  height: 58px;
  margin-left: 0;
  cursor: pointer;
  object-fit: contain;
}

.site_info {
  display: flex;
  align-items: center;
}

.site_user {
  margin-left: 8px;
  color: white;
}

.logout_button {
  margin-left: 8px;
  color: #FFF;
  padding: 0 20px;
  border: 1px solid #16D0FF;
  border-radius: 15px;
  font-size: 12px;
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
magickeyyycommented, Apr 1, 2021
const CracoLessPlugin = require('craco-less');
const path = require('path');

const lessModifyVars = {};

module.exports = {
    plugins: [
        {
            plugin: CracoLessPlugin,
            options: {
                lessLoaderOptions: {
                    lessOptions: {
                        modifyVars: lessModifyVars,
                        javascriptEnabled: true,
                    },
                },
                modifyLessRule: function (lessRule, _context) {
                    lessRule.test = /\.less$/;
                    lessRule.exclude = /\.module\.less$/;
                    return lessRule;
                },
            },
        },
        {
            plugin: CracoLessPlugin,
            options: {
                lessLoaderOptions: {
                    lessOptions: {
                        modifyVars: lessModifyVars,
                        javascriptEnabled: true,
                    },
                },
                modifyLessRule: function (lessRule, _context) {
                    lessRule.test = /\.module\.less$/;
                    lessRule.exclude = undefined;
                    return lessRule;
                },
                cssLoaderOptions: {
                    modules: { localIdentName: '[local]_[hash:base64:5]' },
                },
            },
        },
    ],
};
2reactions
LevapVeeskelacommented, Mar 30, 2021
const CracoLessPlugin = require('craco-less')
const CracoAlias = require('craco-alias')

const lessModifyVars = {}
module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            modifyVars: lessModifyVars,
            javascriptEnabled: true,
          },
        },
      },
    },
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            modifyVars: lessModifyVars,
            javascriptEnabled: true,
          },
        },
        modifyLessRule: function (lessRule, context) {
          lessRule.test = /\.module\.(less)$/
          lessRule.exclude = undefined
          return lessRule
        },
        cssLoaderOptions: {
          modules: {
            localIdentName: '[local]_[hash:base64:5]',
          },
        },
      },
    },
    {
      plugin: CracoAlias,
      options: {
        source: 'tsconfig',
        baseUrl: '.',
        tsConfigPath: './tsconfig.path.json',
      },
    },
  ],
}

ok, that work for me, but module less don’t save selectors hierarchy inside, how can that I correct? for example btn selector doesn’t will be to use(

.navpanel__socials {
  .btn {
    position: relative;
  }
}

https://stackoverflow.com/questions/66872491/reactjscratscarcoless-dont-correct-work-module-system-css/66877609#66877609

Read more comments on GitHub >

github_iconTop Results From Across the Web

webpack error in Cannot find module 'less' - Stack Overflow
This error happens because npm@3 does not resolve peerDependencies any more. npm install less less-loader is the way to go.
Read more >
Magento 2 - Custom Module's _module.less file not updating
I'm working in developer mode. Any ideas? UPDATE. I checked Less files in custom modules but unfortunately this does not answer my question....
Read more >
Features | Vite
Native ES imports do not support bare module imports like the following: ... warn you against the features that do not work with...
Read more >
style-loader - webpack
Example with Locals (CSS Modules): ... Source maps do not work. ... for basic style-loader usage (similar to other file types, i.e. .lazy.less...
Read more >
Working with CSS | Vue CLI
Please also be aware, as it's a native module, there may be compatibility issues vary on the OS and build environment. In that...
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