How to add a query to a webpack loader with multiple loaders?

前端 未结 4 1468
春和景丽
春和景丽 2020-12-13 05:50

I have this Babel loader that\'s working

{ test: /\\.jsx?$/, loader: \'babel\', query: babelSettings, exclude: /node_modules/ },

But now I

4条回答
  •  粉色の甜心
    2020-12-13 06:35

    Update: With non-legacy versions of Webpack you can define an array of loaders in the Webpack configuration.

    If you need to use an older versions of Webpack or add the options inline, the original answer is below.


    The way to do this is to set the query parameters in the loader string itself, as the query object key will only work for one loader.

    Assuming your settings object can be serialized to JSON, as your example indicates, you could easily pass your settings object as a JSON query. Then only the Babel loader will get the settings.

    { test: /\.coffee$/, loader: 'babel?'+JSON.stringify(babelSettings)+'!coffee', exclude: /node_modules/ }
    

    The feature for doing this is somewhat documented here:

    Using Loaders: Query parameters

    Most loaders accept parameters in the normal query format (?key=value&key2=value2) and as JSON object (?{"key":"value","key2":"value2"}).

提交回复
热议问题