这是我花了几个星期学习webpack4的学习笔记。内容不够细,由于一些相对比较简单的,就随意带过了。但愿文章能给你们带来帮助。若有错误,但愿及时指出。例子都在learn-webpack仓库上。若是你从中有所收获的话,但愿你能给个人github
点个star
。javascript
// index.js console.log('hello, atie')
配置webpack.config.js
html
// webpack.config.js module: { rules: [ { test: /\.js$/, include: /src/, loader: path.resolve(__dirname, './loaders/replaceLoader.js') } ] },
// 函数不能使用箭头函数 module.exports = function(source) { console.log(source, 'source') return source.replace('atie', 'world') }
loader
文件其实就是导出一个函数,source
就是webpack
打包出的js
字符串。这里的loader
就是将上面的console.log('hello, atie')
替换为console.log('hello, world')
java
打包下代码,不出所料。控制台就会打印出hello, world
webpack
当你想要给loader传参时,可配置以下git
module: { rules: [ { test: /\.js$/, include: /src/, use: [{ loader: path.resolve(__dirname, './loaders/replaceLoader.js'), options: { name: 'haha' } }] } ] },
经过给loader
添加options
github
这样loader
中就能够经过this.query
获取该参数了web
module.exports = function(source) { // 控制台输出:console.log('hello atie') { name: 'haha' } source console.log(source, this.query, 'source') return source.replace('atie', 'world') }
固然变量不必定非要经过this.query
来获取npm
可经过loader-utils
这个包来获取传入的变量app
安装: npm i loader-utils -D
异步
const loaderUtils = require('loader-utils') // 函数不能使用箭头函数 module.exports = function(source) { // console.log(source, this.query, 'source') const options = loaderUtils.getOptions(this) console.log(options, 'options') // { name: 'haha' } 'options' return source.replace('atie', 'world') }
打印出来的与上面this.query
一致
上面都是直接经过return
返回的,那么咱们还有没有其余方法返回loader
翻译后的代码呢?`
这里就会用到callback
this.callback( err: Error | null, content: string | Buffer, sourceMap?: SourceMap, meta?: any );
上面的代码就能够改写成
module.exports = function(source) { const options = loaderUtils.getOptions(this) const result = source.replace('atie', options.name) this.callback(null, result) }
callback
优点在于它能够传递多余的参数
module.exports = function(source) { setTimeout(() => { return source.replace('atie', 'world') }, 1000) }
当咱们把return
包到异步方法里,打包的时候就会报错,那么咱们该怎么办呢?
这个时候就须要用到this.async()
module.exports = function(source) { const callback = this.async() setTimeout(() => { callback(null, source.replace('atie', 'world')) }, 2000) }
经过调用this.async()
返回的callback
方法来返回结果
use中的loader执行顺序,先右后左,先下后上
在根目录下新建plugins
文件夹,并新建copyright-webpack-plugin.js
,内容以下:
class Copyright { constructor() { console.log('this is plugin') } apply(compiler) { } } module.exports = Copyright
注意:apply这个方法必须存在,否则插件被执行的时候会报错。
配置webpack.config.js
,以下:
const Copyrgiht = require('./plugins/copyright-webpack-plugin.js') ... plugins: [ new Copyrgiht() ]
执行下打包命令后
this is plugin Hash: 479baeba2207182096f8 Version: webpack 4.30.0 Time: 615ms Built at: 2019-05-08 23:05:08 Asset Size Chunks Chunk Names bundle.js 3.77 KiB main [emitted] main index.html 182 bytes [emitted]
控制台打印出了this is plugin
接下来,咱们继续探索插件的奥秘
在使用插件的时候还能够传参
// webpack.config.js plugins: [ new Copyrgiht({ name: 'atie' }) ]
class Copyright { constructor(options) { // console.log(options, 'this is plugin') this.options = options } apply(compiler) { console.log(this.options) } }
执行下打包命令:
{ name: 'atie' } Hash: 479baeba2207182096f8 Version: webpack 4.30.0 Time: 742ms Built at: 2019-05-08 23:24:10 Asset Size Chunks Chunk Names bundle.js 3.77 KiB main [emitted] main index.html 182 bytes [emitted]
控制就会输出 {name: 'atie'}
webpack
在调用apply
会传递一个compiler
参数,这个参数能够作不少事情,具体能够参考webpack
官网
这里介绍下钩子
class Copyright { apply(compiler) { compiler.hooks.emit.tapAsync('Copyright', (compilation,callback) => { console.log(compilation.assets, '以具备延迟的异步方式触及 run 钩子。'); compilation.assets['copyright.txt'] = { source: function() { return 'copyright by atie' }, size: function() { return 17 } } callback() }) } } module.exports = Copyright
该钩子是在文件生成前触发的。咱们在文件生成前,在asset
对象上在加一个文件对象
打包以后
. ├── bundle.js ├── copyright.txt └── index.html
能够看到多了一个copyright.txt
,也就是咱们上面建立的文件。点开该文件还会看到里面的内容正是copyright by atie