next-plugins icon indicating copy to clipboard operation
next-plugins copied to clipboard

[next-less] doesn't play nice with NextJS 9.2 default css loader

Open watch-janick opened this issue 6 years ago • 41 comments

Bug report

Describe the bug

NextJS 9.2 CSS loading doesn't work when using next-less

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

https://github.com/watch-janick/nextjs-css-with-less

  1. Install packages
  2. $ npm run dev
  3. Unable to load the CSS file in _app.js

Expected behavior

The CSS should load natively without any configuration as of NextJS 9.2

System information

  • Version of Next.js: 9.2.0
  • Version of next-less: 1.0.1

watch-janick avatar Jan 21 '20 06:01 watch-janick

When you add any of the css related plugins, including less/sass, it disables built-in support for CSS loading. This was done to ensure the new CSS support does not conflict with existing apps.

timneutkens avatar Jan 21 '20 09:01 timneutkens

Alright thank you for the answer.

Maybe you can tell me why I can't use next-css with the next-with-ant-design-less example?

Whenever I try to import a .css file, I get this kind of error:

.container {
^
SyntaxError: Unexpected token .

I'm using next-css without CSS modules.

watch-janick avatar Jan 21 '20 09:01 watch-janick

Not sure, would have to investigate, however we're going to deprecate next-css soon. Could potentially investigate it under enterprise support. Feel free to email [email protected].

timneutkens avatar Jan 21 '20 09:01 timneutkens

@timneutkens what do you mean, "deprecate next-css"? you mean to say next-css and all of the preprocessor plugins, e.g. next-sass, next-less, next-stylus will no longer work in future versions of Next.js?

snapwich avatar Jan 28 '20 21:01 snapwich

you mean to say next-css and all of the preprocessor plugins, e.g. next-sass, next-less, next-stylus will no longer work in future versions of Next.js?

Deprecation doesn't mean "will not work", it means those plugins won't be maintained as better solutions exist (built-in to Next.js).

Built-in CSS support is already disabled for backwards compat when you add any of the plugins you mentioned.

timneutkens avatar Jan 29 '20 08:01 timneutkens

When you add any of the css related plugins, including less/sass, it disables built-in support for CSS loading. This was done to ensure the new CSS support does not conflict with existing apps.

@timneutkens Is there a way to modify this behaviour via settings in next.config.js? Also how does the roadmap look for importing both css and less files for a next js >9.2 project? Are you dropping support for less/sass?

mansdahlstrom1 avatar Feb 25 '20 15:02 mansdahlstrom1

i want to use nextjs and antd in my product, but i have trouble in the same problem. Any help?

fangwohung avatar Feb 26 '20 07:02 fangwohung

i want to use nextjs and antd in my product, but i have trouble in the same problem. Any help?

I think we still need to use this config, and hope for better future (next js 9.2)

const withLess = require("@zeit/next-less");
const withCSS = require("@zeit/next-css");

module.exports = withCSS(
  withLess({
    webpack(config, options) {
      return config;
    }
  })
);

VityokKlymets avatar Feb 26 '20 15:02 VityokKlymets

Issue in inbuilt css support

I am also facing the issue when i removed the next-css from next.config.js

FYI: image

Any suggestions.

cc @timneutkens

shrmaky avatar Mar 04 '20 09:03 shrmaky

save question

ckken avatar Mar 09 '20 04:03 ckken

i want to use nextjs and antd in my product, but i have trouble in the same problem. Any help?

I think we still need to use this config, and hope for better future (next js 9.2)

const withLess = require("@zeit/next-less");
const withCSS = require("@zeit/next-css");

module.exports = withCSS(
  withLess({
    webpack(config, options) {
      return config;
    }
  })
);

no work

ckken avatar Mar 09 '20 05:03 ckken

Composing next-css and next-less still cannot work. This is my configs:

const withCSS = require('@zeit/next-css');
const withLess = require('@zeit/next-less');
const withPlugins = require('next-compose-plugins');
const fs = require('fs');
const path = require('path');

const nextConfig = {
  env: {},
};

const plugins = [
  withCSS,
  withLess({
    lessLoaderOptions: {
      javascriptEnabled: true,
    },
    webpack: (config, { isServer }) => {
      // Fixes npm packages that depend on `fs` module
      config.node = {
        fs: 'empty',
      };

      if (isServer) {
        const antStyles = /antd\/.*?\/style.*?/;
        const origExternals = [...config.externals];
        config.externals = [
          (context, request, callback) => {
            if (request.match(antStyles)) return callback();
            if (typeof origExternals[0] === 'function') {
              origExternals[0](context, request, callback);
            } else {
              callback();
            }
          },
          ...(typeof origExternals[0] === 'function' ? [] : origExternals),
        ];

        config.module.rules.unshift({
          test: antStyles,
          use: 'null-loader',
        });
      }

      return config;
    },
  }),
];

module.exports = withPlugins(plugins, nextConfig);

judewang avatar Mar 09 '20 07:03 judewang

@ckken I found a work around, just change the order of withCSS and withLess, then it worked.

judewang avatar Mar 09 '20 08:03 judewang

@ckken I found a work around, just change the order of withCSS and withLess, then it worked.

yes,it work now for me ,thanks

ckken avatar Mar 16 '20 05:03 ckken

This is so annoying. I'm generally using CSS in my project, including CSS modules. Built-in CSS support is making it working seamlessly. I decided to use LESS loader for the sake of external library (antd), that required me to change the next.config.js configuration. Right now, next no longer knows how to handle vanilla CSS files across my project. I basically need to reinvent the wheel, and configure CSS loader from the scratch to support normal CSS, CSS modules and PostCSS. This is so stupid. Is there any reason not to reuse existing, built-in CSS support, if we are not overriding it in our next.config.js anyways? It really makes no sense to me.


When you add any of the css related plugins, including less/sass, it disables built-in support for CSS loading. This was done to ensure the new CSS support does not conflict with existing apps.

Still, CSS loader shouldn't conflict with LESS loader and vice versa. Furthermore, why not give programmer a possibility to enable (or include) built-in loader that will work along other loaders if needed?

lvkins avatar Mar 30 '20 13:03 lvkins

Having the same issue with @zeist/next-sass

▶ yarn dev
yarn run v1.21.1
$ next dev
[ wait ]  starting the development server ...
[ info ]  waiting on http://localhost:3000 ...
Warning: Built-in CSS support is being disabled due to custom CSS configuration being detected.
See here for more info: https://err.sh/next.js/built-in-css-disabled

[ error ] ./assets/css/styles.css
ModuleParseError: Module parse failed: Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)

This is with the following versions:

    "next": "9.3.2",
    "next-images": "^1.4.0",
    "@zeit/next-css": "^1.0.1",
    "@zeit/next-sass": "^1.0.1",

and the following next.config.js

const withImages = require('next-images');
const path = require('path');
const withSass = require('@zeit/next-sass');
const withCSS = require('@zeit/next-css');


module.exports = withSass(
  withCSS(
    withImages({
      cssModules: true,
      webpack(config, options) {
        config.resolve.alias['assets'] = path.join(__dirname, 'assets');
        config.resolve.alias['components'] = path.join(__dirname, 'components');
        return config
      }
    })
  )
);

DeanMW avatar Apr 14 '20 21:04 DeanMW

Same issue how can I load sass and css files in my project? this is very confusing.

betancourtl avatar Apr 18 '20 01:04 betancourtl

Would it be better/easier to ditch next/css and next/sass for a custom webpack solution that just works?

betancourtl avatar Apr 18 '20 01:04 betancourtl

Looks like I will have to dive into webpack land once more, see you guys next year.

betancourtl avatar Apr 18 '20 01:04 betancourtl

I used the following configuration to integrate the built-in loader, less-loader and Ant Design Less.

const withLess = require('@zeit/next-less');
// const lessToJS = require('less-vars-to-js');
const fs = require('fs');
const path = require('path');

// const themeVariables = lessToJS(
//   fs.readFileSync(path.resolve(__dirname, './assets/antd.less'), 'utf8')
// );

module.exports = withLess({
  lessLoaderOptions: {
    javascriptEnabled: true,
    // modifyVars: themeVariables,
  },
  webpack: (config, { isServer, dev }) => {
    if (isServer) {
      const antStyles = /antd\/.*?\/style.*?/;
      const origExternals = [...config.externals];
      config.externals = [
        (context, request, callback) => {
          if (request.match(antStyles)) return callback();
          if (typeof origExternals[0] === 'function') {
            origExternals[0](context, request, callback);
          } else {
            callback();
          }
        },
        ...(typeof origExternals[0] === 'function' ? [] : origExternals),
      ];

      config.module.rules.unshift({
        test: antStyles,
        use: 'null-loader',
      });
    }

    const builtInLoader = config.module.rules.find((rule) => {
      if (rule.oneOf) {
        return (
          rule.oneOf.find((deepRule) => {
            if (deepRule.test && deepRule.test.toString().includes('/a^/')) {
              return true;
            }
            return false;
          }) !== undefined
        );
      }
      return false;
    });

    if (typeof builtInLoader !== 'undefined') {
      config.module.rules.push({
        oneOf: [
          ...builtInLoader.oneOf.filter((rule) => {
            return (rule.test && rule.test.toString().includes('/a^/')) !== true;
          }),
        ],
      });
    }

    config.resolve.alias['@'] = path.resolve(__dirname);
    return config;
  },
});

Version of Next.js: 9.3.5

.babelrc

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

gaozhitw avatar Apr 23 '20 15:04 gaozhitw