美文网首页
create-react-app 脚手架引入 scss

create-react-app 脚手架引入 scss

作者: MrDcheng | 来源:发表于2021-04-26 22:34 被阅读0次

    create-react-app 7.3.0 已经在 node_modules/react-scripts/config/webpack.config.js 当中内置了 scss 的预处理器,不用再手动添加,如下:

    ……
    // style files regexes
    const cssRegex = /\.css$/;
    const cssModuleRegex = /\.module\.css$/;
    const sassRegex = /\.(scss|sass)$/;
    const sassModuleRegex = /\.module\.(scss|sass)$/;
    ……
    
    ……
    
    // Opt-in support for SASS (using .scss or .sass extensions).
    // By default we support SASS Modules with the
    // extensions .module.scss or .module.sass
    {
      test: sassRegex,
      exclude: sassModuleRegex,
      use: getStyleLoaders(
        {
          importLoaders: 3,
          sourceMap: isEnvProduction
            ? shouldUseSourceMap
            : isEnvDevelopment,
        },
        'sass-loader'
      ),
      // Don't consider CSS imports dead code even if the
      // containing package claims to have no side effects.
      // Remove this when webpack adds a warning or an error for this.
      // See https://github.com/webpack/webpack/issues/6571
      sideEffects: true,
    },
    // Adds support for CSS Modules, but using SASS
    // using the extension .module.scss or .module.sass
    {
      test: sassModuleRegex,
      use: getStyleLoaders(
        {
          importLoaders: 3,
          sourceMap: isEnvProduction
            ? shouldUseSourceMap
            : isEnvDevelopment,
          modules: {
            getLocalIdent: getCSSModuleLocalIdent,
          },
        },
        'sass-loader'
      ),
    },
    ……
    

    直接安装依赖即可使用:npm install sass-loader node-sass --save

    相关文章

      网友评论

          本文标题:create-react-app 脚手架引入 scss

          本文链接:https://www.haomeiwen.com/subject/gdsyrltx.html