Created
October 24, 2020 01:25
-
-
Save feedsbrain/4966df97c09c30eeaea01667b0bbe1ad to your computer and use it in GitHub Desktop.
Next.js Ant Design LESS + CSS Loader (Custom Plugin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cssLoaderConfig = require('@zeit/next-css/css-loader-config') | |
module.exports = (nextConfig = {}) => { | |
return Object.assign({}, nextConfig, { | |
webpack(config, options) { | |
if (!options.defaultLoaders) { | |
throw new Error( | |
'This plugin is not compatible with Next.js versions below 5.0.0 https://err.sh/next-plugins/upgrade' | |
) | |
} | |
const { dev, isServer } = options | |
const { | |
cssLoaderOptions, | |
postcssLoaderOptions, | |
lessLoaderOptions = {} | |
} = nextConfig | |
// use Object for `test` to bypass NextJS detection | |
// so that it will keep CSS and SASS support. | |
config.module.rules.push({ | |
test: { | |
and: [/\.less$/], | |
not: [/\.module\.less$/] | |
}, | |
use: cssLoaderConfig(config, { | |
extensions: ['less'], | |
cssModules: false, | |
cssLoaderOptions, | |
postcssLoaderOptions, | |
dev, | |
isServer, | |
loaders: [ | |
{ | |
loader: 'less-loader', | |
options: lessLoaderOptions | |
} | |
] | |
}) | |
}) | |
config.module.rules.push({ | |
test: /\.module\.less$/, | |
use: cssLoaderConfig(config, { | |
extensions: ['module.less'], | |
cssModules: true, | |
cssLoaderOptions, | |
postcssLoaderOptions, | |
dev, | |
isServer, | |
loaders: [ | |
{ | |
loader: 'less-loader', | |
options: lessLoaderOptions | |
} | |
] | |
}) | |
}) | |
if (typeof nextConfig.webpack === 'function') { | |
return nextConfig.webpack(config, options) | |
} | |
return config | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment