banner-loader
实现(自创)
给所有匹配的js
加一个注释
webpack.config.js
{ // 给所有匹配的`js`加一个注释
test: /\.js$/,
use: {
loader: 'banner-loader',
options: {
text: 'may',
filename: path.resolve(__dirname, 'banner.js')
}
}
}
banner.js
二次星球中毒
在loader
文件创建banner-loader.js
yarn add schema-utils
校验自己写的loader
格式是否正确
banner-loader.js
// 拿到loader的配置
let loaderUtils = require('loader-utils')
// 校验loader
let validateOptions = require('schema-utils')
// 读取文件
let fs = require('fs') // 异步
function loader(source) { // loader的参数就是源代码
let options = loaderUtils.getOptions(this)
let callback = this.async() // 读取文件是异步
let schema = {
type: 'object',
properties: {
text: {
type: 'string'
},
filename: {
type: 'string'
}
}
}
validateOptions(schema, options, 'banner-loader') // 自己的校验格式, 自己的写的配置, 对应的loader名字
if (options.filename) {
this.cacheable(false) // 不要缓存 如果有大量计算 推荐缓存
// this.cacheable && this.cacheable()
this.addDependency(options.filename) // 自动增加依赖
fs.readFile(options.filename, 'utf8', function (err, data) {
callback(err, `/**${data}**/${source}`)
})
} else {
callback(null, `/**${options.text}**/${source}`)
}
return source
}
module.exports = loader
优化:
- 修改
banner.js
的内容后,webpack
进行监控,打包webapck.config.js
配置watch: true
loader
缓存