【问题标题】:add scss (bootstrap) compilation with output css file in react project在反应项目中添加带有输出css文件的scss(引导)编译
【发布时间】:2018-05-17 23:32:55
【问题描述】:

我有一个 react/express 项目 我希望能够自定义 Bootstrap 4 并输出一个 CSS 文件,其中将包含自定义的 boostrap.css。

我已经 npm 安装了引导程序需要的加载器(postcss-loader、sass-loader、style-loader 等),尽管在 web 中我看到了一个使用 ExtractTextPlugin 的示例以获取输出文件

当我运行 webpack 配置时出现错误 " throw new Error("'output.filename' 是必需的,在配置文件中或作为 --output-filename");"

任何教程/帮助如何在单独的输出文件中完成编译自定义引导程序?

webpack config

const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {

  //root file for the client/browser app
  entry: './src/client/client.js',

  //output file and its location
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname,'public'),
  },

  //run babel for every filename
  module: {
    rules: [
      {
        test: /\.js?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          presets:
          [
            'react',
            'stage-0',
            [
              'env',
              {
                targets:
                {
                  browsers:
                  ['last 2 versions']
                }
              }
            ]
          ]
        }
      }
    ]
  },
};
module.exports = {
  //  root file for the client/browser app
  entry: './src/client/styles/main.scss',

  //  output file and its location
  output: {
    filename: 'styles2.css',
    path: path.resolve(__dirname,'public'),
  },
  module: {
    loaders: [
      // ...
      {
        test: /\.scss$/,
        loader: ExtractTextPlugin.extract('css!sass')
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('public/style.css', {
      allChunks: true,
    })
  ]
};

【问题讨论】:

    标签: reactjs webpack bootstrap-sass


    【解决方案1】:

    您需要删除多余的module.exports。我认为不需要其中两个,因为您可以在单个 module.exports 中简单地实现相同的目标。

    你可以这样实现:

    const path = require('path');
    const ExtractTextPlugin = require('extract-text-webpack-plugin');
    
    module.exports = {
      //root file for the client/browser app
      entry: [
        './src/client/client.js',
        './src/client/styles/main.scss'
      ],
    
      //output file and its location
      output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname,'public'),
      },
    
      //run babel for every filename
      module: {
        rules: [
          {
            test: /\.js?$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            options: {
              presets:[
               'react',
               'stage-0',
               [
                 'env',
                  {
                    targets:
                   {
                    browsers:
                    ['last 2 versions']
                  }
                }
              ]
            ]
          }
        },
        {
          test: /\.scss$/,
          loader: ExtractTextPlugin.extract('css-loader!sass-loader')
        }
      ],
      plugins: [
          new ExtractTextPlugin('public/style.css', {
          allChunks: true,
        })
       ]
     },};
    

    【讨论】:

    • 我按照你之前的建议做了,但没有在我的公共文件夹中生成任何 style.css
    • 是否仍然产生你在帖子中提到的错误?另外,我忘了写 sass-loader 而不是 sass
    • 不,这不是我认为我什么都没做并尝试创建 2 个 modules.exports 的原因
    • 你试过更新答案了吗?也就是放sass-loader和css-loader而不是sass和css
    • yes ('css-loader!sass-loader') ,也许有问题,因为他们网站上的引导程序声明它需要 postcss-loader ?
    猜你喜欢
    • 2015-04-22
    • 2017-12-25
    • 2020-10-26
    • 2018-02-23
    • 1970-01-01
    • 2020-11-11
    • 2017-01-30
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多