Giter Site home page Giter Site logo

Comments (9)

langarus avatar langarus commented on August 23, 2024 12

@taoxhsmile thanks a lot. I found the answer yesterday, and I thought that I updated the comment here. But it's good that you posted your code. I just added the same plugin again at the beginning. I didn't see the need to use CracoAntDesignPlugin as you can already use modifyVars in CracoLessPlugin

This could have been solved much easier i think if craco would use a newer version of react-scripts and css-loader. Because css-loader has an option to automatically detect if the file is simple or module and treat it accordingly. It's only available from css-loader 3.5, and this project only uses 3.4. But hey, we can't complain, it's free.

Here is my final craco.config.js:

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

// this is a variable that's going to be available in both .less and .module.less files
const modifiedTheme = {
  "@primary-color": "#1DA57A",
};

module.exports = {
  plugins: [
    // This plugin takes care of the .less files
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            javascriptEnabled: true,
            modifyVars: modifiedTheme,
          },
        },
      },
    },
    // This plugin take scare of the .less.module files
    {
      plugin: CracoLessPlugin,
      options: {
        modifyLessRule: function (lessRule, _context) {
          lessRule.test = /\.(less)$/;
          lessRule.use = [
            {
              loader: "style-loader",
            },
            {
              loader: "css-loader",
              options: {
                modules: true,
              },
            },
            {
              loader: "less-loader",
              options: {
                lessOptions: {
                  modifyVars: modifiedTheme,
                  javascriptEnabled: true,
                },
              },
            },
          ];
          lessRule.exclude = /node_modules/;
          return lessRule;
        },
      },
    },
  ],
};

from craco-less.

CreazyJack avatar CreazyJack commented on August 23, 2024 1

But how can i use less module?
Help!

from craco-less.

taoxhsmile avatar taoxhsmile commented on August 23, 2024 1

I deal with this by configuring two plugins.

const lessLoaderOptions = {
  lessOptions: {
    modifyVars: { '@primary-color': '#ff6a00', '@link-color': '#ff6a00' },
    javascriptEnabled: true,
  },
};
module.exports = {
  plugins: [
    {
      plugin: CracoAntDesignPlugin,
      options: {
        lessLoaderOptions,
      },
    },
    {
      plugin: CracoLessPlugin,
      options: {
        cssLoaderOptions: {
          modules: { localIdentName: '[local]_[hash:base64:5]' },
        },
        lessLoaderOptions,
        modifyLessRule(lessRule, context) {
          lessRule.test = /\.module\.less$/;
          // lessRule.exclude = /node_modules/;
          delete lessRule.exclude;
          return lessRule;
        },
      },
    },
  ],
}

from craco-less.

fanck0605 avatar fanck0605 commented on August 23, 2024 1

Hello, it would be great if someone could please help by submitting a PR that adds this functionality. Thank you!

@ndbroadbent I have created a pull request to add this feature.

Now, we can use less and less module at the same time without any additional configuration.

from craco-less.

CreazyJack avatar CreazyJack commented on August 23, 2024

me too!

from craco-less.

epver avatar epver commented on August 23, 2024

I read the source code, it seems that this function has not been added, I hope the author can add this function.

from craco-less.

langarus avatar langarus commented on August 23, 2024

This would be great if it would get patched

from craco-less.

ndbroadbent avatar ndbroadbent commented on August 23, 2024

Hello, it would be great if someone could please help by submitting a PR that adds this functionality. Thank you!

from craco-less.

langarus avatar langarus commented on August 23, 2024

For now I've managed to interpret .module.less into css modules as such

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        modifyLessRule: function (lessRule, _context) {
          lessRule.test = /\.(less)$/;
          lessRule.use = [
            {
              loader: "style-loader",
            },
            {
              loader: "css-loader",
              options: {
                modules: true,
              },
            },
            {
              loader: "less-loader",
              options: {
                lessOptions: {
                  modifyVars: { "@primary-color": "#1DA57A" },
                  javascriptEnabled: true,
                },
              },
            },
          ];
          lessRule.exclude = /node_modules/;

          return lessRule;
        },
      },
    },
  ],
};

But in this way I lose the ability to have global .less files. It's not that big of a deal, but if anyone has an idea, it would be great.

from craco-less.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.