With Webpack, is it possible to generate CSS only, excluding the output.js?

前端 未结 4 1045
春和景丽
春和景丽 2021-01-30 11:25

I\'m using Webpack with the extract-text-webpack-plugin.

In my project, I have some build scripts. One of the build scripts is supposed to bundle and minify CSS only. As

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 11:54

    There is an easy way, no extra tool is required.

    There is an easy way and you don't need extra libraries except which you are already using: webpack with the extract-text-webpack-plugin.

    In short:

    Make the output js and css file have identical name, then the css file will override js file.

    A real example (webpack 2.x):

    import path from 'path'
    import ExtractTextPlugin from 'extract-text-webpack-plugin'
    
    const config = {
      target: 'web',
      entry: {
        'one': './src/one.css',
        'two': './src/two.css'
      },
      output: {
        path: path.join(__dirname, './dist/'),
        filename: '[name].css' // output js file name is identical to css file name
      },
      module: {
        rules: [
          {
            test: /\.css$/,
            use: ExtractTextPlugin.extract({
              fallback: 'style-loader',
              use: 'css-loader'
            })
          }
        ]
      },
      plugins: [
        new ExtractTextPlugin('[name].css') // css file will override generated js file
      ]
    }
    

提交回复
热议问题