3.0.1 • Published 1 year ago

craco-less v3.0.1

Weekly downloads
38,612
License
MIT
Repository
github
Last release
1 year ago

Test Status Coverage Status MIT License


Community Maintained

We rely on your help to keep this project up to date and work with the latest versions of craco and react-scripts.

Before you send a PR, please check the following:

  • 100% test coverage
yarn test
  • Code is formatted with Prettier
yarn format
  • No ESLint warnings
yarn lint
  • No security vulnerabilities in any NPM packages
yarn audit

You are also welcome to add your GitHub username to the Contributors section at the bottom of this README. (optional)

Please don't send a pull request if it does not meet the above requirements

Pull requests will be ignored and closed if there is a failing build on Travis CI.


Craco Less Plugin

This is a craco plugin that adds Less support to create-react-app version >= 2.

Use react-app-rewired for create-react-app version 1.

Ant Design

If you want to use Ant Design with create-react-app, you should use the craco-antd plugin. craco-antd includes Less and babel-plugin-import (to only include the required CSS.) It also makes it easy to customize the theme variables.

Supported Versions

craco-less is tested with:

  • react-scripts: ^3.2.0
  • @craco/craco: ^5.5.0

Installation

First, follow the craco Installation Instructions to install the craco package, create a craco.config.js file, and modify the scripts in your package.json.

Then install craco-less:

$ yarn add craco-less

# OR

$ npm i -S craco-less

Usage

Here is a complete craco.config.js configuration file that adds Less compilation to create-react-app:

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

module.exports = {
  plugins: [{ plugin: CracoLessPlugin }],
};

Configuration

You can pass an options object to configure the loaders and plugins(configure less and less modules at the same time). You can also pass a modifyLessRule(or modifyLessModuleRule) callback to have full control over the Less webpack rule.

  • options.styleLoaderOptions
  • options.cssLoaderOptions
  • options.postcssLoaderOptions
  • options.lessLoaderOptions
  • options.miniCssExtractPluginOptions (only used in production)
  • options.modifyLessRule(lessRule, context)
    • A callback function that receives two arguments: the webpack rule, and the context. You must return an updated rule object.
      • lessRule:
        • test: Regex (default: /\.less$/)
        • exclude: Regex (default: /\.module\.less$/)
        • use: Array of loaders and options.
        • sideEffects: Boolean (default: true)
      • context:
        • env: "development" or "production"
        • paths: An object with paths, e.g. appBuild, appPath, ownNodeModules
  • options.modifyLessModuleRule(lessModuleRule, context)
    • A callback function that receives two arguments: the webpack rule, and the context. You must return an updated rule object.
      • lessModuleRule:
        • test: Regex (default: /\.module\.less$/)
        • use: Array of loaders and options.
      • context:
        • env: "development" or "production"
        • paths: An object with paths, e.g. appBuild, appPath, ownNodeModules

For example, to configure less-loader:

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

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        lessLoaderOptions: {
          lessOptions: {
            modifyVars: {
              "@primary-color": "#1DA57A",
              "@link-color": "#1DA57A",
              "@border-radius-base": "2px",
            },
            javascriptEnabled: true,
          },
        },
      },
    },
  ],
};

CSS / Less Modules

CSS / Less modules are enabled by default, and the default file suffix for less modules is .module.less.

If your project is using typescript, please append the following code to ./src/react-app-env.d.ts

declare module "*.module.less" {
  const classes: { readonly [key: string]: string };
  export default classes;
}

You can use modifyLessModuleRule to configure the file suffix and loaders (css-loader, less-loader ...) for less modules.

For example:

const CracoLessPlugin = require("craco-less");
const { loaderByName } = require("@craco/craco");

module.exports = {
  plugins: [
    {
      plugin: CracoLessPlugin,
      options: {
        modifyLessRule(lessRule, context) {
          // You have to exclude these file suffixes first,
          // if you want to modify the less module's suffix
          lessRule.exclude = /\.m\.less$/;
          return lessRule;
        },
        modifyLessModuleRule(lessModuleRule, context) {
          // Configure the file suffix
          lessModuleRule.test = /\.m\.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;
        },
      },
    },
  ],
};

CSS modules gotcha

There is a known problem with Less and CSS modules regarding relative file paths in url(...) statements. See this issue for an explanation.

(Copied from the less-loader README.)

Further Configuration

If you need to configure anything else for the webpack build, take a look at the Configuration Overview section in the craco README. You can use CracoLessPlugin while making other changes to babel and webpack, etc.

Contributing

Install dependencies:

$ yarn install

# OR

$ npm install

Run tests:

$ yarn test

Before submitting a pull request, please check the following:

  • All tests are passing
    • Run yarn test
  • 100% test coverage
    • Coverage will be printed after running tests.
    • Check the coverage results in your browser: open coverage/lcov-report/index.html
  • No ESLint errors
    • yarn lint
  • All code is formatted with Prettier
    • yarn format
    • If you use VS Code, you should enable the formatOnSave option.
  • Using the correct webpack version as a dependency
    • yarn update_deps
    • NOTE: The webpack dependency is needed to silence some annoying warnings from NPM. This must always match the version from react-scripts.

Releasing a new version

  • Make sure the "Supported Versions" section is updated at the top of the README.
  • Check which files will be included in the NPM package:
    • npm pack
    • Update .npmignore to exclude any files.
  • Release new version to NPM:
    • npm publish

License

MIT

Contributors

sykmeldinger-testpcs-notif-and-msgcra-template-gtja-czcra-template-ltbycecra-template-cz-testreact-ui-framework-momo-testteespace-calendar-appteespace-coreteespace-mail-appteespace-meeting-appteespace-note-appteespace-office-appwapl-worksteespace-admincra-template-ifunpluscra-template-antd-ifunplustsf_empty_cardtsf_popupdel-generator-stepper@mrbatman/craco-antdcraco-antd-plugin@infinitebrahmanuniverse/nolb-crac@wbts/common@wbts/sso@wbts/template-reactsykmeldinger-test3@everything-registry/sub-chunk-1399the-moment-uiarco-custom-librarysystems-clientweb-members-pluginxlshenxlshen-uisen-kitsavvycom-erpshookttechsophy-workflow-modelersdkdcmh-client-scaffoldmani-rbl-npm-pkgpapanacheomsorgspenger-kalkulatorpermission-manager-test4permission-manager-test5pcs-message-and-notificationpherusa-libreact-codebasereact-fcw-componentsreact-api-previewquestionnaire-si2react-ui-demo1srm_moresettingsstartups-mobile-componentstartups-website-componentuat-data-designertn-componentszhangshaowei_pro2warehouse-ikunuse-commonses@h-cli/ui-fe@gongyy123/story_ui@leessony/layout@newcoin-foundation/iosdk@newstackdev/iosdk@navikt/nav-dekoratoren@navikt/omsorgspenger-kalkulator@kirrosh/craco-antd@igor-lemon/craco-antd-scss@jyd-team/cra-template@semantic-ui-react/craco-less@senswap/sen-ui@toy-box/flow-builder@w3ctech-editorial-department/craco-antd-mobilealbisonb2c-evm-compatible-bridge-appca-template-reactcalliope-ssgcbridge-web-widgetcbridge-widgetcra-template-spa-overallchuangshi-os-componentchuangshi-os-mobile-componentchuangshi-os-website-componentcra-template-easystartcra-template-boanergestsclient-scaffold2craco-antdcra-template-rstore-typescript-cracocra-ts-antwindcra-template-typescript-antwinddcm-us-sdk
3.0.1

1 year ago

3.0.0

1 year ago

2.1.0-alpha.0

3 years ago

2.0.0

3 years ago

1.20.0

3 years ago

1.19.0

3 years ago

1.18.0

3 years ago

1.17.1

4 years ago

1.17.0

5 years ago

1.16.0

5 years ago

1.15.0

5 years ago

1.14.4

5 years ago

1.14.3

5 years ago

1.14.2

5 years ago

1.14.1

5 years ago

1.14.0

5 years ago

1.13.0

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago