webpack/lib/BannerPlugin.js

123 lines
2.8 KiB
JavaScript
Raw Normal View History

2013-05-13 05:16:22 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const { ConcatSource } = require("webpack-sources");
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
const Template = require("./Template");
2013-05-13 05:16:22 +08:00
const validateOptions = require("schema-utils");
const schema = require("../schemas/plugins/BannerPlugin.json");
2017-10-28 05:23:38 +08:00
/** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginArgument} BannerPluginArgument */
/** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginOptions} BannerPluginOptions */
2018-02-25 09:00:20 +08:00
const wrapComment = str => {
if (!str.includes("\n")) {
return Template.toComment(str);
}
2018-02-25 09:00:20 +08:00
return `/*!\n * ${str
.replace(/\*\//g, "* /")
.split("\n")
.join("\n * ")}\n */`;
2017-04-12 07:54:08 +08:00
};
2013-05-13 05:16:22 +08:00
class BannerPlugin {
/**
* @param {BannerPluginArgument} options options object
*/
constructor(options) {
if (arguments.length > 1) {
2018-02-25 09:00:20 +08:00
throw new Error(
"BannerPlugin only takes one argument (pass an options object)"
);
}
2017-10-28 05:23:38 +08:00
validateOptions(schema, options, "Banner Plugin");
2017-10-28 05:23:38 +08:00
2019-08-11 11:28:55 +08:00
if (typeof options === "string" || typeof options === "function") {
options = {
banner: options
};
}
/** @type {BannerPluginOptions} */
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 {
2018-05-11 15:46:16 +08:00
const banner = this.options.raw
? bannerOption
: wrapComment(bannerOption);
this.banner = () => banner;
}
}
apply(compiler) {
2017-04-14 01:25:04 +08:00
const options = this.options;
const banner = this.banner;
2018-02-25 09:00:20 +08:00
const matchObject = ModuleFilenameHelpers.matchObject.bind(
undefined,
options
);
compiler.hooks.compilation.tap("BannerPlugin", compilation => {
compilation.hooks.optimizeChunkAssets.tap("BannerPlugin", chunks => {
for (const chunk of chunks) {
if (options.entryOnly && !chunk.canBeInitial()) {
2018-01-22 20:52:43 +08:00
continue;
}
2018-02-25 09:00:20 +08:00
for (const file of chunk.files) {
if (!matchObject(file)) {
2018-01-22 20:52:43 +08:00
continue;
}
let query = "";
let filename = file;
const hash = compilation.hash;
const querySplit = filename.indexOf("?");
2018-02-25 09:00:20 +08:00
if (querySplit >= 0) {
2018-01-22 20:52:43 +08:00
query = filename.substr(querySplit);
filename = filename.substr(0, querySplit);
}
const lastSlashIndex = filename.lastIndexOf("/");
2019-08-11 10:31:40 +08:00
const basename =
lastSlashIndex === -1
? filename
: filename.substr(lastSlashIndex + 1);
2018-01-22 20:52:43 +08:00
const data = {
2018-01-22 20:52:43 +08:00
hash,
chunk,
filename,
basename,
2018-02-25 09:00:20 +08:00
query
};
const comment = compilation.getPath(banner(data), data);
2018-01-22 20:52:43 +08:00
compilation.updateAsset(
file,
old => new ConcatSource(comment, "\n", old)
2018-02-25 09:00:20 +08:00
);
2018-01-22 20:52:43 +08:00
}
}
2013-05-13 05:16:22 +08:00
});
});
}
}
module.exports = BannerPlugin;