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

Modify Less module rule for all files in a directory #100

Open
ppyoosef opened this issue Jun 15, 2022 · 2 comments
Open

Modify Less module rule for all files in a directory #100

ppyoosef opened this issue Jun 15, 2022 · 2 comments

Comments

@ppyoosef
Copy link

ppyoosef commented Jun 15, 2022

Hi

I am using antd as a UI toolkit. I want to implement style modules in my project. I tried like this

   `modifyLessRule(lessRule, context) {
      // You have to exclude these file suffixes first,
      // if you want to modify the less module's suffix
      // const relativePath = path.relative(context, resourcePath);
      console.log(context)
      lessRule.exclude = /\.less$/;
      return lessRule;

      
    },
    modifyLessModuleRule(lessModuleRule, context) {
      // Configure the file suffix
      lessModuleRule.test = /\.less$/;

      // Configure the generated local ident name.
      const cssLoader = lessModuleRule.use.find(loaderByName("css-loader"));
      cssLoader.options.modules = {
        localIdentName: "[local]_[hash:base64:5]",
      };

      return lessModuleRule;
    },`

But it applied to all less files including antd library. I want to apply only for styles inside the src folder. I know we can do by renaming all style files inside the src folder with some suffix (index.m.less) and change the regex.

I want to keep my style file as formatted now (index.less). Is there any option to apply modify rule only for my src folder?

@cdllqos
Copy link

cdllqos commented Sep 10, 2022

@ppyoosef
I configured like this solved my problem

// craco.config.ts
const CracoLessPlugin = require('craco-less');
const lessModuleReg = /src\/(pages|components).*\.less/;

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        modifyLessRule(lessRule: any) {
          lessRule.exclude = lessModuleReg;
          return lessRule;
        },
        modifyLessModuleRule(lessRule: any) {
          lessRule.test = lessModuleReg;
          lessRule.exclude = /node_modules/;
          return lessRule;
        },
      },
    },
  ],
};

@LLLIIYYY
Copy link

please read that: https://github.com/DocSpring/craco-less#css--less-modules
you can try to add declare for module.less to d.ts(like react-app-env-d.ts)
declare module "*.module.less" { const classes: { readonly [key: string]: string }; export default classes; }

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

No branches or pull requests

3 participants