Css-loader: Incompat with webpack2 LoaderOptionsPlugin

Created on 21 Sep 2016  Â·  7Comments  Â·  Source: webpack-contrib/css-loader

In lib/loader.js
https://github.com/webpack/css-loader/blob/master/lib/loader.js#L101
this.options may be rewritten by LoaderOptionsPlugin in the following line
https://github.com/webpack/webpack/blob/master/lib/LoaderOptionsPlugin.js#L29
thus this.options.context could be undefined and cause an error.
Should we change it to this.context to fix the bug?

Bug

Most helpful comment

mocking up output and context in LoaderOptionsPlugin fixes the issue for me

new webpack.LoaderOptionsPlugin({
      sourceMap: true,
      minimize: true,
      discardComments: {
        removeAll: true
      },
      options: {
        context: '',
        output: //webpackconfig.output
      }
})

All 7 comments

+1

I've encountered Module build failed: TypeError: Path must be a string. Received undefined and changing this.options.context to this.context solved the issue.

How exactly did you fix this? I got this same exact error...? I mean I can change it directly, but it seems the bug stopped appearing for you all of a sudden...?

I'm still getting the issue too, you can switch to the fork I'm about to pull request until it's merged into mainline (:

@sokra Looks like there's an easy fix for that, can we get this in?

I'm afraid #356 is not enough, there are other places using this.option, for example, https://github.com/webpack/css-loader/blob/master/lib/getLocalIdent.js#L11

var request = path.relative(options.context, loaderContext.resourcePath);

Maybe this should be fixed from the LoaderOptionsPlugin side

Once consequence of this incompatibility I encountered today was that I was in not being able to set {discardComments:{removeAll: true}} via webpack.LoaderOptionsPlugin. This did remove comments:

// ...
  }, {
    test: /\.css$/,
    loader: ExtractTextPlugin.extract({
      fallbackLoader: 'style-loader',
      loader: 'css-loader?{discardComments:{removeAll: true}}',
    }),
  }, {
    test: /\.less$/,
    loader: ExtractTextPlugin.extract({
      fallbackLoader: 'style-loader',
      loader: ['css-loader?{discardComments:{removeAll: true}}', 'less-loader'],
    }),
// ...

config.plugins.push(
  new webpack.LoaderOptionsPlugin({
    minimize: true,
  })
);
// ...

This and variations did not:

// ...
  }, {
    test: /\.css$/,
    loader: ExtractTextPlugin.extract({
      fallbackLoader: 'style-loader',
      loader: 'css-loader',
    }),
  }, {
    test: /\.less$/,
    loader: ExtractTextPlugin.extract({
      fallbackLoader: 'style-loader',
      loader: ['css-loader', 'less-loader'],
    }),
// ...

config.plugins.push(
  new webpack.LoaderOptionsPlugin({
    minimize: true,
    discardComments: {
      removeAll: true
    }
  }
});
// ...

This is because css-loader looks into query rather than options if I'm not wrong.

So for those who are willing to get rid of ?... in the loader as I have been just trying, here is a tip: _DON'T!_ (while this issue is open).

:–)

mocking up output and context in LoaderOptionsPlugin fixes the issue for me

new webpack.LoaderOptionsPlugin({
      sourceMap: true,
      minimize: true,
      discardComments: {
        removeAll: true
      },
      options: {
        context: '',
        output: //webpackconfig.output
      }
})
Was this page helpful?
0 / 5 - 0 ratings