Skip to content Skip to sidebar Skip to footer

Specify Loader Configuration From Webpack Config File In Request

Question Is possible to specify loader from webpack config file in request by something like alias? I'm looking for implementation of this idea: webpack config loaders: [ { /

Solution 1:

Solution for this is resolveLoader.alias

webpack config

module: {
    loaders: [
        {   // loaderA - default
            test: /\.js/,
            loaders: ['loader-a1', 'loader-a2'] 
        }
    ]
},
resolveLoader: {
    alias: {
        loaderB: ['loader-b1', 'loader-b2'] // I want to enable this in require
    }
}

usage

default - will use ['loader-a1', 'loader-a2']

require('./module');

custom - will use ['loader-b1', 'loader-b2']: disable loaderA and enable loaderB

require('!loaderB!./module');

Solution 2:

It may not be the answer you are looking for, but you could decorate the JavaScript file extensions and have a loader definition for each

loaders: [
{   // loaderA - defaulttest: /\(.a)?.js/,
    loaders: ['loader-a1', 'loader-a2'] 
},

{   // loaderB - I want to enable this in requiretest: /\.b.js/,
    loaders: ['loader-b1', 'loader-b2'] 
}
]

You would want to make one of the extensions optional so that it can fall back to external libraries or other JS files.

You could also use the includes property which will take a regex and tell webpack to only look for files (you want to run with loaderA) in a specific folder path.

Post a Comment for "Specify Loader Configuration From Webpack Config File In Request"