???create-react-app???react??????????????????css

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了???create-react-app???react??????????????????css相关的知识,希望对你有一定的参考价值。

?????????round   xbox   ref   row   ??????   external   ??????   ??????   resolve   

?????????webpack?????????????????????css

?????????????????????????????????????????????????????????????????????css??????vue??????????????????style?????????scope???????????????create-react-app???react?????????????????????????????????style???

??????style-loader????????????css

?????????????????????style-loader???modules??????

?????????webpack?????? ?????????????????? ???????????????

npm run eject

????????????

// config > webpack.config.dev.js


// style files regexes
const cssRegex = /.css$/;
const useable = /.use(able)?.css$/;  // ?????? ??????css
const cssModuleRegex = /.module.css$|.use(able)?.css$/;


 {
    test: cssRegex,
    exclude: cssModuleRegex,
    use: getStyleLoaders({
      importLoaders: 1,
      modules: true  // ???????????????
    }),
  },

css ???????????????????????????????????????????????????use.css??????useable.css??????????????????

//  style.use.css
.box {
    background: red;
}

???????????????

import React, {Component} from ???react???;
import style from ???./style.use.css???

export default class Test extends Component {
    render() {
        return(
            <div className = {style.box}>
                <div>
                    {????????????}
                </div>
            </div>
        )
    }
}

??????class??????????????????????????????????????????????????????????????????????????????????????????css???
??????????????????

???????????????

?????????????????????????????????????????????????????????????????????style?????????????????????????????????,???style?????????????????????????????????????????????????????????style

?????????????????????webpack?????? ?????????????????? ???????????????

npm run eject
// config > webpack.config.dev.js

????????????
// style files regexes
const cssRegex = /.css$/;
const useable = /.use(able)?.css$/;  // ?????? ??????css
const cssModuleRegex = /.module.css$|.use(able)?.css$/;


// ????????????getUseableLoaders
const getUseableLoaders = (cssOptions, preProcessor) => {
  const loaders = [
    require.resolve(???style-loader/useable???),
    {
      loader: require.resolve(???css-loader???),
      options: cssOptions,
    },
    {
      // Options for PostCSS as we reference these options twice
      // Adds vendor prefixing based on your specified browser support in
      // package.json
      loader: require.resolve(???postcss-loader???),
      options: {
        // Necessary for external CSS imports to work
        // https://github.com/facebook/create-react-app/issues/2677
        ident: ???postcss???,
        plugins: () => [
          require(???postcss-flexbugs-fixes???),
          require(???postcss-preset-env???)({
            autoprefixer: {
              flexbox: ???no-2009???,
            },
            stage: 3,
          }),
        ],
      },
    },
  ];
  if (preProcessor) {
    loaders.push(require.resolve(preProcessor));
  }
  return loaders;
};
   ...
   ...
   {
    test: cssRegex,
    exclude: cssModuleRegex,
    use: getStyleLoaders({
      importLoaders: 1,
    }),
  },
  { // ??????loader
    test: useable,
    exclude: cssModuleRegex,
    use: getUseableLoaders({
      importLoaders: 1,
    }),
  },
  ...
  ...

css ???????????????????????????????????????????????????use.css??????useable.css??????????????????

//  style.use.css
.box {
    background: red;
}

???????????????

import React, {Component} from ???react???;
import style from ???./style.use.css???

export default class Test extends Component {
    render() {
        return(
            <div className = ???box???>
                <div>
                    {????????????}
                </div>
            </div>
        )
    }
}

???????????????????????????????????? style.use.css ???????????????


以上是关于???create-react-app???react??????????????????css的主要内容,如果未能解决你的问题,请参考以下文章

Bootstrap 在 create-react-app 中不起作用

运行 create-react-app 构建脚本时如何设置构建 .env 变量?

尝试测试 create-react-app 项目时出现“ReferenceError:文档未定义”

我无法使用 npx create-react-app 安装反应?

在使用 create-react-app 创建项目时有啥方法可以使用 NPM 而不是 Yarn

使用 create-react-app 和 node 设置 jest 多项目