玩转 webpack 学习笔记
定义:loader 只是一个导出为函数的 JavaScript 模块。
module.exports = function(source) {
return source;
};
多个 Loader 串行执行顺序从后到前
module.exports = {
entry: './src/index.js', output: {
filename: 'bundle.js', path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.less$/,
use: [
'style-loader',
'css-loader',
'less-loader'
]
}
]
}
};
compose = (f, g) => (...args) => f(g(...args));
a-loader.js
:
module.exports = function(source) {
console.log ('loader a is executed');
return source;
};
b-loader.js
:
module.exports = function(source) {
console.log ('loader b is executed');
return source;
};
新建文件夹 loader-order,然后执行初始化命令
npm init -y
npm i webpack webpack-cli -D
新建 webpack.config.js
添加下面配置:
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.join(__dirname, 'dist'),
filename: 'main.js'
},
module: {
rules: [
{
test: /\.js$/,
use: [
path.resolve('./loaders/a-loader.js'),
path.resolve('./loaders/b-loader.js')
]
}
]
}
}
结构如下:loader 的代码在上面,这里就不粘贴了
"build": "webpack"
运行 npm run build
,我们就可以知道顺序是从右往左。