html-webpack-plugin插件,webpack笔记——在html-webpack-plugin插件中提供给其它插件是使用的hooks...

本文详细探讨了html-webpack-plugin插件的工作原理,包括如何在webpack编译过程中添加自定义hooks,以及这些hooks如何被其他插件使用。通过分析源码,解释了插件如何处理chunks、生成assets,以及在不同阶段调用自定义钩子。同时,文中通过实例展示了如何在html-webpack-plugin的钩子中插入自定义逻辑。
摘要由CSDN通过智能技术生成

最近在这段时间刚好在温故下webpack源码,webpack5都出来了,4还不再学习下?

这次顺便学习下webpack的常用插件html-webpack-plugin。

发现这个插件里面还额外加入了自己的hooks,方便其它插件来实现自己的功能,不得不说作者真是个好人。

部分代码如下

// node_modules/html-webpack-plugin/index.js

app(compiler) {

// setup hooks for webpack 4

if (compiler.hooks) {

compiler.hooks.compilation.tap('HtmlWebpackPluginHooks', compilation => {

const SyncWaterfallHook = require('tapable').SyncWaterfallHook;

const AsyncSeriesWaterfallHook = require('tapable').AsyncSeriesWaterfallHook;

compilation.hooks.htmlWebpackPluginAlterChunks = new SyncWaterfallHook(['chunks', 'objectWithPluginRef']);

compilation.hooks.htmlWebpackPluginBeforeHtmlGeneration = new AsyncSeriesWaterfallHook(['pluginArgs']);

compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing = new AsyncSeriesWaterfallHook(['pluginArgs']);

compilation.hooks.htmlWebpackPluginAlterAssetTags = new AsyncSeriesWaterfallHook(['pluginArgs']);

compilation.hooks.htmlWebpackPluginAfterHtmlProcessing = new AsyncSeriesWaterfallHook(['pluginArgs']);

compilation.hooks.htmlWebpackPluginAfterEmit = new AsyncSeriesWaterfallHook(['pluginArgs']);

});

}

...

// Backwards compatible version of: compiler.plugin.emit.tapAsync()

(compiler.hooks ? compiler.hooks.emit.tapAsync.bind(compiler.hooks.emit, 'HtmlWebpackPlugin') : compiler.plugin.bind(compiler, 'emit'))((compilation, callback) => {

const applyPluginsAsyncWaterfall = self.applyPluginsAsyncWaterfall(compilation);

// Get chunks info as json

// Note: we're excluding stuff that we don't need to improve toJson serialization speed.

const chunkOnlyConfig = {

assets: false,

cached: false,

children: false,

chunks: true,

chunkModules: false,

chunkOrigins: false,

errorDetails: false,

hash: false,

modules: false,

reasons: false,

source: false,

timings: false,

version: false

};

const allChunks = compilation.getStats().toJson(chunkOnlyConfig).chunks;

// Filter chunks (options.chunks and options.excludeCHunks)

let chunks = self.filterChunks(allChunks, self.options.chunks, self.options.excludeChunks);

// Sort chunks

chunks = self.sortChunks(chunks, self.options.chunksSortMode, compilation);

// Let plugins alter the chunks and the chunk sorting

if (compilation.hooks) {

chunks = compilation.hooks.htmlWebpackPluginAlterChunks.call(chunks, { plugin: self });

} else {

// Before Webpack 4

chunks = compilation.applyPluginsWaterfall('html-webpack-plugin-alter-chunks', chunks, { plugin: self });

}

// Get assets

const assets = self.htmlWebpackPluginAssets(compilation, chunks);

// If this is a hot update compilation, move on!

// This solves a problem where anindex.html file is generated for hot-update js files

// It only happens in Webpack 2, where hot updates are emitted separately before the full bundle

if (self.isHotUpdateCompilation(assets)) {

return callback();

}

// If the template and the assets did not change we don't have to emit the html

const assetJson = JSON.stringify(self.getAssetFiles(assets));

if (isCompilationCached && self.options.cache && assetJson === self.assetJson) {

return callback();

} else {

self.assetJson = assetJson;

}

Promise.resolve()

// Favicon

.then(() => {

if (self.options.favicon) {

return self.addFileToAsset

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值