-
Notifications
You must be signed in to change notification settings - Fork 318
[next-less] doesn't play nice with NextJS 9.2 default css loader #598
Comments
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. |
Alright thank you for the answer. Maybe you can tell me why I can't use Whenever I try to import a .css file, I get this kind of error: .container {
^
SyntaxError: Unexpected token . I'm using |
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 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? |
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 |
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;
}
})
); |
Issue in inbuilt css support I am also facing the issue when i removed the next-css from next.config.js Any suggestions. cc @timneutkens |
save question |
no work |
Composing 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); |
@ckken I found a work around, just change the order of |
yes,it work now for me ,thanks |
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.
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? |
Having the same issue with
This is with the following versions:
and the following
|
Same issue how can I load sass and css files in my project? this is very confusing. |
Would it be better/easier to ditch next/css and next/sass for a custom webpack solution that just works? |
Looks like I will have to dive into webpack land once more, see you guys next year. |
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
}
]
]
} |
I have the same problem. I need less to use antd, but it seems that functionality for less is broken (none of the suggestions above works). |
I have the same problem. I have using next-CSS but they are giving me a warning and when I removed the next-CSS from next.config.my style CSS is not loaded chunks in the production. |
Next.js seems to work fine so long as you don't use a next.config file and delete any such files that appear in your folder. The documentation on this is extremely sparse, and wrong in some cases. I solved the Less problem by using Easy Less, which works well (if you have VSCode). |
@gaozhitw how you override the primary color of ant-design? this works on build version but not on dev version. |
Same problem here, i would like to use withLess only to use a custom theme in ant design and use built-in css modules in my project. Code can be found here https://github.com/clement-faure/next-antd-graphql-starter |
Can we update the official example with @gaozhitw answer? |
@timneutkens Are there any plans to add a default less configuration as now done with css and scss? |
This works in dev mode, but running
|
still getting error on me.
|
i tried this setting, i do remove all errors, but all settings in postcss.config.js does not work anymore after use this setting, i try to use tailwindcss and antd together. currently i have to use both withCSS and withLess. only use withLess it will keep show "Module parse failed: Unexpected character '@' (1:0)" error |
So in the end, I decided to separate out the LESS and SCSS build processes. Now I have an explicit |
Where are you configuring a watch step? This solution seems elegant. |
As an example ...
"build:less": "lessc --js ./styles/less/custom-antd.less ./styles/build/custom-antd.css",
"build:scss": "node-sass --omit-source-map-url ./styles/custom-bulma.scss ./styles/build/custom-bulma.css",
"watch:less": "less-watch-compiler --config ./less-watch.config.json",
"watch:scss": "yarn run build:scss --watch",
"watch:dev": "next",
"watch": "npm-run-all --parallel watch:*"
... For {
"watchFolder": "./styles/less",
"outputFolder": "./styles/build/",
"mainFile": "custom-antd.less",
"sourceMap": false,
"enableJs": true
} Make sure you are importing your import 'styles/build/custom-antd.css';
import 'styles/build/custom-bulma.css'; With these changes, I was able to remove Finally, just run |
This is what works for me, simple config but works like a charm. Had these issues for hours. Am using I don't have This is my const withImages = require("next-images");
const { withPlugins } = require("next-compose-plugins");
const FilterWarningsPlugin = require("webpack-filter-warnings-plugin");
const withLess = require("@zeit/next-less");
const withCss = require("@zeit/next-css");
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-custom.less"), "utf-8"));
const config = {
distDir: "_next", // changes from .next to _next to enable serving static assets with nginx's minimal configuration
trailingSlash: false,
lessLoaderOptions: {
javascriptEnabled: true,
modifyVars: themeVariables, // make your antd custom effective
},
webpack: function (config) {
config.module.rules.push({
test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)$/,
use: {
loader: "url-loader",
options: {
limit: 100000,
name: "[name].[ext]",
},
},
});
config.plugins.push(
new FilterWarningsPlugin({
exclude: /mini-css-extract-plugin[^]*Conflicting order between:/,
}),
);
return config;
},
};
module.exports = withPlugins([withImages, withCss, withLess], config); |
this way works fine, but just one concern which is the withCss plugin use old version of postcss and long time no update, also may be other features not supported when you disable the build-in css loader. |
This const withLess = require('@zeit/next-less')
const lessToJS = require('less-vars-to-js')
const fs = require('fs')
const path = require('path')
const withCss = require('@zeit/next-css')
const themeVariables = lessToJS(
fs.readFileSync(path.resolve(__dirname, './antd.less'), 'utf8')
)
module.exports = withCss({
cssModules: true,
...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'
})
}
config.resolve.alias['@'] = path.resolve(__dirname)
return config
}
})
}) |
I'm also getting the below error when running
|
I'm using antdesign with sass loader, I'm also having the same issue upon building. |
I'm using antdesign with less loader, I'm also having the same issue upon building. |
I have been wrestling with this issue for nearly 3 days now trying to find an elegant solution that would also work with Storybook.js. Right now it seems like everyone is just scrambling. Can we not get an official response from someone on what the best way to proceed here is? |
@timneutkens Any updates about native less support? |
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
$ npm run dev
_app.js
Expected behavior
The CSS should load natively without any configuration as of NextJS 9.2
System information
The text was updated successfully, but these errors were encountered: