Next: Plugins, Previous: Output, Up: Webpack Concepts [Index]
Loaders allow webpack to process other types of files and convert them into valid modules that can be consumed by your application and added to the dependency graph.
At a high level, loaders have two properties in your webpack configuration:
const path = require('path');
module.exports = {
output: {
filename: 'my-first-webpack.bundle.js'
},
module: {
rules: [
{ test: /\.txt$/, use: 'raw-loader' }
]
}
};
The configuration above has defined a ‘rules’ property for a single module with two required properties: ‘test’ and ‘use’. This tells webpack’s compiler the following:
webpack finds a .txt file inside a require or import statement
(a module) use the ‘raw-loader’ to transform it before adding it to the
bundle.
webpack
config, you are defining them under ‘module.rules’ and not ‘rules’. For
your benefit, webpack will warn you if this is done incorrectly.