背景
- 不知道webpack插件是怎么回事,除了官方的文檔外,還有一個很直觀的方式,就是看源碼 。
- 看源碼是一個挖寶的行動,也是一次冒險,我們可以找一些代碼量不是很大的源碼
- 比如webpack插件,我們就可以通過BannerPlugin源碼,來看下官方是如何實現一個插件的
- 希望對各位同學有所幫助,必要時可以通過源碼進行一門技術的學習,加深理解
配合文檔apihttps://webpack.docschina.org/api/compilation-object/#updateasset
代碼分析已添加中文注釋
/* MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra*/"use strict";const { ConcatSource } = require("webpack-sources");const Compilation = require("./Compilation");const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");const Template = require("./Template");const createSchemaValidation = require("./util/create-schema-validation");/** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginArgument} BannerPluginArgument *//** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginOptions} BannerPluginOptions *//** @typedef {import("./Compiler")} Compiler */// 創建一個驗證const validate = createSchemaValidation(require("../schemas/plugins/BannerPlugin.check.js"),() => require("../schemas/plugins/BannerPlugin.json"),{name: "Banner Plugin",baseDataPath: "options",});//包裝Banner文字const wrapComment = (str) => {if (!str.includes("\n")) {return Template.toComment(str);}return `/*!\n * ${str.replace(/\*\//g, "* /").split("\n").join("\n * ").replace(/\s+\n/g, "\n").trimRight()}\n */`;};//插件類class BannerPlugin {/*** @param {BannerPluginArgument} options options object* 初始化插件配置*/constructor(options) {if (typeof options === "string" || typeof options === "function") {options = {banner: options,};}validate(options);this.options = options;const bannerOption = options.banner;if (typeof bannerOption === "function") {const getBanner = bannerOption;this.banner = this.options.raw? getBanner: (data) => wrapComment(getBanner(data));} else {const banner = this.options.raw? bannerOption: wrapComment(bannerOption);this.banner = () => banner;}}/*** Apply the plugin* @param {Compiler} compiler the compiler instance* @returns {void}* 插件主方法*/apply(compiler) {const options = this.options;const banner = this.banner;const matchObject = ModuleFilenameHelpers.matchObject.bind(undefined,options);//創建一個Map,處理如果添加過的文件,不在添加const cache = new WeakMap();compiler.hooks.compilation.tap("BannerPlugin", (compilation) => {//處理Assets的hookcompilation.hooks.processAssets.tap({name: "BannerPlugin",//PROCESS_ASSETS_STAGE_ADDITIONS — 為現有的 asset 添加額外的內容,例如 banner 或初始代碼 。stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,},() => {//遍歷當前編譯對象的chunksfor (const chunk of compilation.chunks) {//如果配置標識只處理入口,但是當前chunk不是入口,直接進入下一次循環if (options.entryOnly && !chunk.canBeInitial()) {continue;}//否則,遍歷chunk下的文件for (const file of chunk.files) {//根據配置匹配文件是否滿足要求,如果不滿足,直接進入下一次循環,處理下一個文件if (!matchObject(file)) {continue;}//否則,const data = https://www.huyubaike.com/biancheng/{chunk,filename: file,};//獲取插值路徑?https://webpack.docschina.org/api/compilation-object/#getpathconst comment = compilation.getPath(banner, data);//修改Asset,https://webpack.docschina.org/api/compilation-object/#updateassetcompilation.updateAsset(file, (old) => {//從緩存中獲取let cached = cache.get(old);//如果緩存不存在 或者緩存的comment 不等于當前的commentif (!cached || cached.comment !== comment) {//源文件追加到頭部或者尾部const source = options.footer? new ConcatSource(old,"\n", comment): new ConcatSource(comment, "\n", old);//創建對象加到緩存cache.set(old, { source, comment });//返回修改后的源return source;}//返回緩存中的源return cached.source;});}}});});}}module.exports = BannerPlugin;
經驗總結擴展閱讀
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- MES會成為象ERP一樣的標準產品嗎?
- 明朝那個皇帝復辟過
- 屏幕喚醒是什么意思
- 澳洲龍蝦頭怎么吃
- 毅然決然不猶豫是什么動物
- 導航怎么看比較準
- 皮膚黃光做幾個療程
- 梓墨這個名字的寓意
- 中國銀行外匯卡怎么辦理
- 香煎魚怎么做
