https://github.com/webpack/webpack/blob/main/lib/BannerPlugin.js
配合檔案api
https://webpack.docschina.org/api/compilation-object/
程式碼分析已新增中文註釋
/*
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的hook
compilation.hooks.processAssets.tap(
{
name: "BannerPlugin",
//PROCESS_ASSETS_STAGE_ADDITIONS — 為現有的 asset 新增額外的內容,例如 banner 或初始程式碼。
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
() => {
//遍歷當前編譯物件的chunks
for (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 = {
chunk,
filename: file,
};
//獲取插值路徑?https://webpack.docschina.org/api/compilation-object/#getpath
const comment = compilation.getPath(banner, data);
//修改Asset,https://webpack.docschina.org/api/compilation-object/
compilation.updateAsset(file, (old) => {
//從快取中獲取
let cached = cache.get(old);
//如果快取不存在 或者快取的comment 不等於當前的comment
if (!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;