最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

如何解决React官方脚手架不支持Less的问题(小结)

来源:动视网 责编:小采 时间:2020-11-27 22:07:48
文档

如何解决React官方脚手架不支持Less的问题(小结)

如何解决React官方脚手架不支持Less的问题(小结):说在前面 create-react-app 是由 React 官方提供并推荐使用构建新的 React 单页面应用程序的最佳方式,不过目前版本(1.5.x)其构建的项目中默认是不支持动态样式语言Less 的。如果我们的项目必须要使用 Less 呢,这就需要我们手动集成一下。本篇主要针对集成
推荐度:
导读如何解决React官方脚手架不支持Less的问题(小结):说在前面 create-react-app 是由 React 官方提供并推荐使用构建新的 React 单页面应用程序的最佳方式,不过目前版本(1.5.x)其构建的项目中默认是不支持动态样式语言Less 的。如果我们的项目必须要使用 Less 呢,这就需要我们手动集成一下。本篇主要针对集成


大概意思是,执行该命令后会把已构建依赖项、配置文件和脚本复制到程序目录中。该操作是不可逆转的,执行完成后会删除这个命令,也就是说只能执行一次。

至于 react-scripts 都集成了哪些东西,通过yarn eject命令的执行记录也能看出个大概:

λ yarn eject
yarn run v1.6.0
$ react-scripts eject
? Are you sure you want to eject? This action is permanent. Yes
Ejecting...

Copying files into E:\React\my-app
 Adding \config\env.js to the project
 Adding \config\paths.js to the project
 Adding \config\polyfills.js to the project
 Adding \config\webpack.config.dev.js to the project
 Adding \config\webpack.config.prod.js to the project
 Adding \config\webpackDevServer.config.js to the project
 Adding \config\jest\cssTransform.js to the project
 Adding \config\jest\fileTransform.js to the project
 Adding \scripts\build.js to the project
 Adding \scripts\start.js to the project
 Adding \scripts\test.js to the project

Updating the dependencies
 Removing react-scripts from dependencies
 Adding autoprefixer to dependencies
 Adding babel-core to dependencies
 Adding babel-eslint to dependencies
 Adding babel-jest to dependencies
 Adding babel-loader to dependencies
 Adding babel-preset-react-app to dependencies
 Adding babel-runtime to dependencies
 Adding case-sensitive-paths-webpack-plugin to dependencies
 Adding chalk to dependencies
 Adding css-loader to dependencies
 Adding dotenv to dependencies
 Adding dotenv-expand to dependencies
 Adding eslint to dependencies
 Adding eslint-config-react-app to dependencies
 Adding eslint-loader to dependencies
 Adding eslint-plugin-flowtype to dependencies
 Adding eslint-plugin-import to dependencies
 Adding eslint-plugin-jsx-a11y to dependencies
 Adding eslint-plugin-react to dependencies
 Adding extract-text-webpack-plugin to dependencies
 Adding file-loader to dependencies
 Adding fs-extra to dependencies
 Adding html-webpack-plugin to dependencies
 Adding jest to dependencies
 Adding object-assign to dependencies
 Adding postcss-flexbugs-fixes to dependencies
 Adding postcss-loader to dependencies
 Adding promise to dependencies
 Adding raf to dependencies
 Adding react-dev-utils to dependencies
 Adding resolve to dependencies
 Adding style-loader to dependencies
 Adding sw-precache-webpack-plugin to dependencies
 Adding url-loader to dependencies
 Adding webpack to dependencies
 Adding webpack-dev-server to dependencies
 Adding webpack-manifest-plugin to dependencies
 Adding whatwg-fetch to dependencies

Updating the scripts
 Replacing "react-scripts start" with "node scripts/start.js"
 Replacing "react-scripts build" with "node scripts/build.js"
 Replacing "react-scripts test" with "node scripts/test.js"

Configuring package.json
 Adding Jest configuration
 Adding Babel preset
 Adding ESLint configuration

Ejected successfully!

Please consider sharing why you ejected in this survey:
 http://goo.gl/forms/Bi6CZjk1EqsdelXk1

Done in 5.37s.

说了这么多,现在怎样才能在我们的项目中暴露 webpack 的配置文件?没错,你没猜错,只需要运行一下yarn eject即可。

再来看我们的实验项目的目录,您会发现其中多了一个config文件夹,其中就有三个关于 webpack 的配置文件:

webpack.config.dev.js # 开发环境配置
webpack.config.prod.js # 生产环境配置
webpackDevServer.config.js # 开发服务器配置

我们需要关注的是前两个,最后一个是关于本地开发服务器http://localhost:3000的一些相关配置。

修改 webpack 配置

理论上讲,需要同步修改 webpack.config.dev.js webpack.config.prod.js 配置文件:

在module.rules节点中找到 css 文件的加载规则:

1.test: /\.css$/ 修改为 test: /\.(css|less)$/;
2.在use数组最后新增一个对象元素{loader: require.resolve('less-loader')}。

修改完成后:

// "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变成红色则说明配置没有问题。

文档

如何解决React官方脚手架不支持Less的问题(小结)

如何解决React官方脚手架不支持Less的问题(小结):说在前面 create-react-app 是由 React 官方提供并推荐使用构建新的 React 单页面应用程序的最佳方式,不过目前版本(1.5.x)其构建的项目中默认是不支持动态样式语言Less 的。如果我们的项目必须要使用 Less 呢,这就需要我们手动集成一下。本篇主要针对集成
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top