2024-10-08

大家好,今天我想给大家分享如何去写一个插件,相信大家都是用webpack来做前端的打包和构建,其中插件是为webpack非常重要的一个部分,webpack附带了很多内置插件,并且我们还可以自定义插件做很多事情。在说如何写插件之前我们必须要了解的是webpack的打包流程。
那么它的打包流程是什么样的呢?当我们输入打包命令之后发生了什么?运行命令之后,它拿到我们命令行的参数,然后结合这个webpack.config.js中的内容,去初始化参数合并参数,然后去准备编译。
准备编译的过程中,他又干了什么呢?他创建了一个compiler对象,然后把所有的插件都注册好,找到我们整个打包的入口,也就是这个entry,这就是它的准备工作。于是就开始编译了。
他首先是用loader去转换所有的模块,比如说ts,还有tsx啊,这些文件去转换成js文件。从入口开始转换所有模块。转换的同时,他还会去分析每个模块的依赖关系。编译完成之后他也就得到了所有模块之间的相互依赖关系,最后输出打包好的所有文件。
plugin的功能之所以强大,就是因为webpack在这打包的过程中,暴露了一个完整生命周期。这就意味着我们开发者可以在这个生命周期里面去执行任何的操作,影响他任意的打包步骤,来得到我们想要的结果。它可以做任何loader无法实现的事情。

接下来我给大家介绍一下如何写一个插件。这是每个插件都遵循的规范。首先你要有一个class函数,这个class函数里面要有一个Apply方法,这个方法会被webpack执行,我们的主要功能都写在这个apply方法里。然后调用一个webpack的钩子,webpack会在特定的时间去执行你想要的操作。我们可以在官网这里查看compiler所有的钩子函数,然后选择合适的钩子去做我们想做的事情。如果调用的是异步钩子,我们还需要去调用webpack提供的回调函数来告诉webpack我们功能完成了。

接下来我给大家介绍一下我们项目里面已经用到的自定义插件,这是一个简单的生成html并且把打包出来的文件都注入到html里的插件。当打包出来的文件包含哈希值时,这个插件就特别有用,不用我们自己去更改标签,容易出错。

由于我们的插件需要在打包完成之后再执行,因此我们是要找从里面找到这个emit钩子是符合我们的要求的,他是在输出所有的打包好的文件之前去执行的,那我们就可以利用这个钩子。
现在一起来看看我们的plugin具体实现的代码。我们用了一个类来定义插件,用apply方法来编写我们的主要功能。在这里我们调用了webpack的emit钩子,在输出所有的打包好的文件之前执行。compliation这个对象里面有所有这次打包的资源的信息,我们通过它拿到我们所有的打包好的入口文件路径,写成标签,然后用我们存在项目里的这个html文件作为模版,把写好的标签注入到这个html里面,然后把新生成的文件放到我们的compliation的assets对象里。这样就完成了。
这是我们的最后输出的html文件。这是我们webpack打包好的资源。这是通过我们自己定义的插件处理完的。除此以外我们项目还有其他的一些自定义插件,比如这个简易的将public文件夹下的assets copy到build文件的copy plugin。
我们需要用很多时间来学习和调试如何自定义插件,但是我们可以更加了解webpack在做什么,用插件去定义任何我们想要的功能。
最后,这是我参考的文档。谢谢!

【打开PPT】Hello everyone, today I want to share with you how to write a plug-in, I believe that everyone is using webpack to do front-end packaging, and plugin is a very important part. webpack comes with a lot of built-in plugins, and we can also customize plugins to do a lot of things. Before we talk about how to write a plugin, we must understand the webpack packaging process.【下一页PPT】
So what's the packaging process like? 【打开Powershell】What happens when we enter the pack command? After running the command, it takes the parameters of our command line, and then, 【打开VS code】combined with the parameters of this config file to get the final parameters, and then prepares to compile.
Before it compiles, It creates a compiler object, registers all the plugins, finds the entry where it need to start with. After this it began to compile.
It uses loader to convert all the modules from the entry. At the same time, It also analyzes the dependencies of each module.
【回到PPT】After the compilation is complete, wepack also gets the interdependencies between all the modules, and finally outputs all bundles.
The plugin is powerful because webpack exposes a full life cycle during this packaging process. This means that we developers can do whatever we want in this life cycle, influencing any of its packaging steps, to get the result we want. Plugin can do anything that loader can't do.
Next I'll show you how to write a plugin. 【下一页PPT】This is the specification that every plugin follows. First we must have a class function, and inside that class function must have an Apply method, which is executed by webpack, and our main functions are written in that apply method. Then we need to call a webpack compiler hook, and webpack will perform the desired action at a specific time. 【打开官网】We can check all the hooks of the compiler in the official website here, and then choose the appropriate hook to do what we want. 【回到PPT】If we are calling an asynchronous hook, we also need to call the callback function provided by webpack to tell webpack that our execution is complete.
【下一页PPT】Next, I'll introduce you to the custom plug-in we've used in our CCP project, which is a simple plug-in that generates html and injects the packaged files into the html. This plugin is especially useful when the packaged file contains a hash value, we dont need to change the tag ourselves.
【打开官网】Since our plugin needs to be executed after the packaging is completed, we find out that the emit hook is in line with our requirements, and it is executed before all the packaged files are output, so we can use this hook.
【打开VS code】Now let's take a look at the code of our plugin implementation. We used a class to define the plug-in and the apply method to write our main functionality. From here we call webpack's emit hook and execute it before exporting all the bundles. The compliation object contains all the information of the packaged resources. We obtain the path of all bundle entry files through it, write them into tags, and then use this html file in our project as a template to inject the written tags into the html. Then put the newly generated file into our compliation assets object. And that's it.
This is our final output html file. This is the resource we inject. This is done through our own plugins. In addition, our project also has some other custom plug-ins, such as this simple copy plugin to copy assets from the public folder to the build folder.
Webpack provides the appropriate hooks for each step, we just need to find the right moment to do the right thing.
Finally, this is the document I refer to. Thank you!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容