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: [
{ // loaderA - default
test: /\.js/,
loaders: ['loader-a1', 'loader-a2']
},
{ // loaderB - I want to enable this in require
test: /\.js/,
loaders: ['loader-b1', 'loader-b2']
}
]
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');
Known solution
My example can be written like this (but is not and answer for me):
weback config
loaders: [
{
test: /\.js/,
loaders: ['loader-a1', 'loader-a2']
}
]
usage
default
require('./module');
custom
require('!loader-b1!loader-b2!./module');
Motivation
1. Creating loaders chain in require can be long and DRY. This require can sometimes can be really to long
require('!loader-b1!loader-b2!loader-b3!loader-b4!./module');
2. It's hard to use variables in require loaders
Creating variables in webpack.config.js
can easily handle different use case of loaders. It's awful to crate the same case in inline require
weback config
loaders: [
{
test: /\.js/,
loaders: [
`loader-a1?${JSON.stringify({
sourceMap: DEBUG,
minimize: !DEBUG
})}`,
`loader-a2?${JSON.stringify({
sourceMap: DEBUG,
minimize: !DEBUG
})}`
]
}
]
usage
default - using variables in loaders
require('./module');
custom - using variables in loaders - it looks so awful and you have to expose building variable DEBUG
require(`!loader-b1?${JSON.stringify({ sourceMap: DEBUG, minimize: !DEBUG })}!loader-b2?${JSON.stringify({ sourceMap: DEBUG, minimize: !DEBUG })}!./module`);
Tip
I used word alias.