Question
Is it possible to specify the loader from the webpack configuration file in the request using something like an alias? I am looking for an implementation of this idea:
Webpack configuration
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'] } ]
Using
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 it is not and the answer is for me):
Web browser configuration
loaders: [ { test: /\.js/, loaders: ['loader-a1', 'loader-a2'] } ]
Using
default
require('./module');
order
require('!loader-b1!loader-b2!./module');
Motivation
1. Creating loader chains on demand can be long and dry. It can sometimes be very long
require('!loader-b1!loader-b2!loader-b3!loader-b4!./module');
2. It is difficult to use variables in bootloaders
Creating variables in webpack.config.js can easily handle various use cases for loaders. It's terrible to insert the same case in inline require
Web browser configuration
loaders: [ { test: /\.js/, loaders: [ `loader-a1?${JSON.stringify({ sourceMap: DEBUG, minimize: !DEBUG })}`, `loader-a2?${JSON.stringify({ sourceMap: DEBUG, minimize: !DEBUG })}` ] } ]
Using
default - use variables in bootloaders
require('./module');
custom - using variables in bootloaders - it looks so awful and you need to set the DEBUG building variable
require(`!loader-b1?${JSON.stringify({ sourceMap: DEBUG, minimize: !DEBUG })}!loader-b2?${JSON.stringify({ sourceMap: DEBUG, minimize: !DEBUG })}!./module`);
Tip
I used the word alias .