Skip to content

Instantly share code, notes, and snippets.

@delikat
Last active August 4, 2021 03:39
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save delikat/d50cf9c8bce8b8f469a8eaacab170ac3 to your computer and use it in GitHub Desktop.
Save delikat/d50cf9c8bce8b8f469a8eaacab170ac3 to your computer and use it in GitHub Desktop.
Modular antd imports with next.js, next-less, next-typescript, and babel-plugin-import
const withLess = require('@zeit/next-less')
const withTypescript = require('@zeit/next-typescript')
const resolve = require('resolve')
module.exports = withTypescript(withLess({
lessLoaderOptions: {
javascriptEnabled: true,
// theme antd here
modifyVars: {'@primary-color': '#1Dd57A'}
},
webpack: (config, { defaultLoaders, dir, isServer }) => {
defaultLoaders.babel.options.plugins.push(['import', {
'libraryName': 'antd',
'style': true
}])
config.externals = []
if (isServer) {
config.externals.push((context, request, callback) => {
resolve(request, { basedir: dir, preserveSymlinks: true }, (err, res) => {
if (err) {
return callback()
}
// exclude webpack itself and antd from externals
if (res.match(/node_modules[/\\].*\.js/) && !res.match(/node_modules[/\\]webpack/) && !res.match(/node_modules[/\\]antd/)) {
return callback(null, `commonjs ${request}`)
}
callback()
})
})
}
return config
},
}))
@onury
Copy link

onury commented Nov 5, 2018

@delikat thanks for this but you said modular imports are working with this but entire css is still loaded in my tests. Am I missing something?

antd-css-size

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment