Getting css output using webpack ExtractTextPlugin

后端 未结 4 1386
轻奢々
轻奢々 2020-12-24 10:43

I am trying to get css requires to work in webpack using the ExtractTextPlugin but with no success

I want a separate css file rather than inlining any css.

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 11:36

    Here's a webpack.config.js that works. I don't use the same directory names you do, but I think you can see the differences and make the needed changes. I'm also including my current module versions.

    const path = require('path');
    const ExtractTextPlugin = require('extract-text-webpack-plugin');
    
    const config = {
      entry: './src/index.js',
      output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'bundle.js',
        publicPath: 'build/'
      },
      module: {
        rules: [
          {
            use: 'babel-loader',
            test: /\.js$/
          },
          {
            loader: ExtractTextPlugin.extract({fallback: 'style-loader', use: 'css-loader'}),
            test: /\.css$/
          },
          {
            test: /\.(jpe?g|png|gif|svg)$/,
            use: [
              {
                loader: 'url-loader',
                options: { limit: 40000 }
              },
              'image-webpack-loader?bypassOnDebug'
            ]
          }
        ]
      },
      plugins: [
        new ExtractTextPlugin({filename: 'style.css',
          allChunks: true
        })
      ]
    };
    
    module.exports = config;
    

    // and modules:

    "devDependencies": {
        "babel-core": "^6.24.1",
        "babel-loader": "^6.4.1",
        "babel-preset-env": "^1.3.3",
        "css-loader": "^0.28.0",
        "extract-text-webpack-plugin": "^2.0.0-beta.4",
        "file-loader": "^0.11.1",
        "image-webpack-loader": "^3.3.0",
        "style-loader": "^0.16.1",
        "url-loader": "^0.5.8",
        "webpack": "^2.2.0-rc.0"
      }
    

提交回复
热议问题