https://github.com/webpack/webpack/blob/main/lib/BannerPlugin.js
创新互联公司主要从事成都网站设计、网站建设、外贸网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务龙马潭,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
配合文档api
https://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的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/#updateasset
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;