Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ant Design with custom theme #470

Closed
eduludi opened this issue Jan 22, 2018 · 5 comments
Closed

Ant Design with custom theme #470

eduludi opened this issue Jan 22, 2018 · 5 comments

Comments

@eduludi
Copy link

eduludi commented Jan 22, 2018

Background

I'm attempting to integrate Ant Design (antd) with Razzle. It's "working", but looks like I have an issue with the Webpack configuration, as antd styles are not bundled in the css output file, but as inline css. This causes a styles flickering as the inline styles are loaded once the app is loaded.

This is the production output:

File sizes after gzip:
  153.35 KB     build/static/js/bundle.8ce9c7da.js    // including antd styles (inline)
  1.01 KB       build/static/css/bundle.089fbb86.css  // Only with my styles

And in <head>:

screen shot 2018-01-22 at 13 22 32

Question

How can I bring the antd's and my app's styles together?

Configuration

Dependencies:

  • antd
  • less
  • less-loader
  • babel-plugin-import
// razzle.config.js
const antdTheme = require('./antdTheme') // Include variables to override antd theme

module.exports = {
  modify: (config, { target, dev }, webpack) => {
    return {
      ...config,
      module: {
        ...config.module,
        rules: [
          ...config.module.rules,
          {
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            options: {
              plugins: [['import', { libraryName: 'antd', style: true }]],
            },
          },
          {
            test: /\.less$/,
            use: [
              { loader: 'style-loader' },
              { loader: 'css-loader' },
              {
                loader: 'less-loader',
                options: {
                  modifyVars: antdTheme,
                },
              },
            ],
          },
        ],
      },
    }
  },
}
// antdTheme.js
module.exports = {
  'primary-color': 'red',
}
@eduludi
Copy link
Author

eduludi commented Jan 22, 2018

After reading about less-loader in production I got it:

"Usually, it's recommended to extract the style sheets into a dedicated file in production using the ExtractTextPlugin. This way your styles are not dependent on JavaScript"

So here is my working razzle.config.js:

// razzle.config.js
const ExtractTextPlugin = require('extract-text-webpack-plugin')

const extractLess = new ExtractTextPlugin({
  filename: '[name].[contenthash].css',
  disable: process.env.NODE_ENV === 'development', // disabled during development 
})

const antdTheme = require('./antdTheme') // Include variables to override antd theme

module.exports = {
  modify: (config, { target, dev }, webpack) => {
    return {
      ...config,
      module: {
        ...config.module,
        rules: [
          ...config.module.rules,
          {
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            options: {
              plugins: [['import', { libraryName: 'antd', style: true }]],
            },
          },
          {
            test: /\.less$/,
            use: extractLess.extract({  // use the ExtractTextPlugin instance
              use: [
                {
                  loader: 'css-loader',
                },
                {
                  loader: 'less-loader',
                  options: {
                    modifyVars: antdTheme,
                  },
                },
              ],
              // use style-loader in development
              fallback: 'style-loader',
            }),
          },
        ],
      },
      plugins: [
        ...config.plugins,
        extractLess // <- Add the ExtractTextPlugin instance here
      ],
    }
  },
}

@eduludi eduludi closed this as completed Jan 22, 2018
@eduludi
Copy link
Author

eduludi commented Jan 22, 2018

Full example in #471

@fdidron
Copy link

fdidron commented Jan 24, 2018

Thank you so much for this, I would like to note that this works well with after.js too !

@mbrochh
Copy link

mbrochh commented Mar 1, 2018

@eduludi super amazing example, after failing to accomplish this myself, I got it running following your example!

Question: Does this work with SSR? At the moment, the server rendered response that I get does show a button, for example, but only after the react bundle is loaded on the client, the ant.design styles kick in.

@cbbfcd
Copy link

cbbfcd commented Jul 23, 2018

@mbrochh so you hava any idea ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants