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.

lessVarsFilePath not works

See original GitHub issue

I tried overwrite less vars in variables.less but it doesn’t work at all.

In next.config.js

const withNx = require('@nrwl/next/plugins/with-nx')
const withImages = require('next-images')
const withAntdLess = require('next-plugin-antd-less')

const nextConfig = {
    nx: {
        svgr: true,
    },
    images: {
        disableStaticImages: true,
    },
    lessVarsFilePath: './styles/variables.less',
}

module.exports = withAntdLess(withImages(withNx(nextConfig)))

In .babelrc

{
  "presets": [
    "@nrwl/next/babel"
  ],
  "plugins": [
    [
      "import",
      {
        "libraryName": "antd",
        "style": true
      }
    ]
  ],
  "ignore": [
    "node_modules"
  ]
}

In styles/variables.less

@import '~antd/lib/style/themes/default.less';

@primary-color: #477593;
@border-radius-base: 5px;
@body-background: #f7f7f7;

And in the component Test

In pages/test.tsx

import {Button} from 'antd'

export const Test = () => (
    <Button type='primary'>antd button</Button>
)

export default Test

The button color is still the default one.

Is there anything missed that I have to add more?

Current environment is below

    "babel-plugin-import": "^1.13.3",
    "next-plugin-antd-less": "^1.5.2",
    "antd": "^4.16.13",

Edited

below configuration on next.config.js is working, so annoying

const nextConfig = {
    nx: {
    // Set this to false if you do not want to use SVGR
    // See: https://github.com/gregberge/svgr
        svgr: true,
    },
    // FIXME: https://github.com/twopluszero/next-images/issues/73 still got a problem on loading images
    images: {
        disableStaticImages: true,
    },
    lessVarsFilePath: './styles/variables.less', // not working
    modifyVars: {'@primary-color': '#000000'}, // working
}

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
bigbizzecommented, Feb 1, 2022

your variables.less is in __ROOT__/styles/ dir? or __ROOT__/src/styles/?

try change to lessVarsFilePath: './src/styles/variables.less' is working?

so even just adding a containing folder named whatever in root and place my styles folder inside of it (for e.g. as per your suggestion ./src/styles/x.less rather than ./styles/x.less), the function “additionalData” defined on line 328 of overrideWebpackConfig.js actually fires (this should be easily fixed and probably shouldn’t be happening right…?) for anyone confused about this.

if your antd variables .less file at (after changing path) ./src/styles/x.less as commonly recommended looks something like this:

@import '~antd/lib/style/themes/default.less';

@primary-color: black; // change antd primary-color
@background-color-base: black;
@layout-header-background: green;
// ..

you also need to set “lessVarsFilePathAppendToEndOffContent” in next.config.js to true:

module.exports = withAntdLess({
  lessVarsFilePath: './src/styles/x.less',
  lessVarsFilePathAppendToEndOfContent: true,
  // ...

and then the variables at ./src/styles/x.less will be applied correctly

i’m assuming this should be pretty easy to fix & clean up so it’s easier for people to work with but i don’t have the time to contribute this myself currently.

thank you for the great project & work though!

1reaction
semoonparkcommented, Nov 24, 2021

at root/styles

When I use the modifyVars option with lessVarsToJs, it works well.

const themeVariables = lessToJs(
    fs.readFileSync(path.resolve(__dirname, './styles/variables.less'), 'utf8'),
)
/**
 * @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
 **/


const nextConfig = {
    nx: {
        svgr: true,
    },
    images: {
        disableStaticImages: true,
    },
    // lessVarsFilePath: './styles/variables.less',
    modifyVars: themeVariables,
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

lessVarsFilePath doesn't work · Issue #74 - GitHub
Issue I'm trying to use next-plugin-antd-less according to the documentation, but my antd variables don't work.
Read more >
How to configure Next.js with Antd / Less and Sass / CSS ...
I tried adding lessVarsFilePath to next.config.js (like what we need to do for next-plugin-antd-less), but it has no effect. – ...
Read more >
How to use Ant Design with Next.js - LogRocket Blog
By doing this, we import the Ant Design component styles globally and are not required to import each component individually. Ant Design also ......
Read more >
How to set up Ant Design with Next.js - Albin Groen
In this article we'll be covering how to make Ant Design with custom theming work flawlessly together with Next.js. Start by setting up...
Read more >
How to Customize Ant Design Theme in a Next.js App
... Or better still you can specify a path to a file lessVarsFilePath: '. ... So make sure it's not imported elsewhere.
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