// "postcss" loader applies autoprefixer to our CSS. // "css" loader resolves paths in CSS and adds assets as dependencies. // "style" loader turns CSS into JS modules that inject <style> tags. // In production, we use a plugin to extract that CSS to a file, but // in development "style" loader enables hot editing of CSS. { test: /\.(css|less)$/, use: [ require.resolve('style-loader'), { loader: require.resolve('css-loader'), options: { importLoaders: 1, }, }, { loader: require.resolve('postcss-loader'), options: { // Necessary for external CSS imports to work // https://github.com/facebookincubator/create-react-app/issues/2677 ident: 'postcss', plugins: () => [ require('postcss-flexbugs-fixes'), autoprefixer({ browsers: [ '>1%', 'last 4 versions', 'Firefox ESR', 'not ie < 9', // React doesn't support IE8 anyway ], flexbox: 'no-2009', }), ], }, }, { loader: require.resolve('less-loader') } ], },
至此,就已经完成了create-react-app 对 Less 的支持。
效果验证
最后,在我们的实验项目中验证一下配置是否生效。
首先在src根目录下使用 Less 语法创建一个 less 文件,取名为Test.less:
@title-color:#f00; .App-title { color: @title-color }
然后在App.js文件中通过如下API导入上述的 less 文件:
import './Test.less';
再次yarn start运行我们的程序,如果标题Welcome to React变成红色则说明配置没有问题。