1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const { ConcatSource } = require("webpack-sources");
|
---|
9 | const Compilation = require("./Compilation");
|
---|
10 | const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
|
---|
11 | const Template = require("./Template");
|
---|
12 | const createSchemaValidation = require("./util/create-schema-validation");
|
---|
13 |
|
---|
14 | /** @typedef {import("../declarations/plugins/BannerPlugin").BannerFunction} BannerFunction */
|
---|
15 | /** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginArgument} BannerPluginArgument */
|
---|
16 | /** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginOptions} BannerPluginOptions */
|
---|
17 | /** @typedef {import("./Compilation").PathData} PathData */
|
---|
18 | /** @typedef {import("./Compiler")} Compiler */
|
---|
19 | /** @typedef {import("./TemplatedPathPlugin").TemplatePath} TemplatePath */
|
---|
20 |
|
---|
21 | const validate = createSchemaValidation(
|
---|
22 | /** @type {(function(typeof import("../schemas/plugins/BannerPlugin.json")): boolean)} */
|
---|
23 | (require("../schemas/plugins/BannerPlugin.check.js")),
|
---|
24 | () => require("../schemas/plugins/BannerPlugin.json"),
|
---|
25 | {
|
---|
26 | name: "Banner Plugin",
|
---|
27 | baseDataPath: "options"
|
---|
28 | }
|
---|
29 | );
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * @param {string} str string to wrap
|
---|
33 | * @returns {string} wrapped string
|
---|
34 | */
|
---|
35 | const wrapComment = str => {
|
---|
36 | if (!str.includes("\n")) {
|
---|
37 | return Template.toComment(str);
|
---|
38 | }
|
---|
39 | return `/*!\n * ${str
|
---|
40 | .replace(/\*\//g, "* /")
|
---|
41 | .split("\n")
|
---|
42 | .join("\n * ")
|
---|
43 | .replace(/\s+\n/g, "\n")
|
---|
44 | .trimEnd()}\n */`;
|
---|
45 | };
|
---|
46 |
|
---|
47 | class BannerPlugin {
|
---|
48 | /**
|
---|
49 | * @param {BannerPluginArgument} options options object
|
---|
50 | */
|
---|
51 | constructor(options) {
|
---|
52 | if (typeof options === "string" || typeof options === "function") {
|
---|
53 | options = {
|
---|
54 | banner: options
|
---|
55 | };
|
---|
56 | }
|
---|
57 |
|
---|
58 | validate(options);
|
---|
59 |
|
---|
60 | this.options = options;
|
---|
61 |
|
---|
62 | const bannerOption = options.banner;
|
---|
63 | if (typeof bannerOption === "function") {
|
---|
64 | const getBanner = bannerOption;
|
---|
65 | /** @type {BannerFunction} */
|
---|
66 | this.banner = this.options.raw
|
---|
67 | ? getBanner
|
---|
68 | : /** @type {BannerFunction} */ data => wrapComment(getBanner(data));
|
---|
69 | } else {
|
---|
70 | const banner = this.options.raw
|
---|
71 | ? bannerOption
|
---|
72 | : wrapComment(bannerOption);
|
---|
73 | /** @type {BannerFunction} */
|
---|
74 | this.banner = () => banner;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Apply the plugin
|
---|
80 | * @param {Compiler} compiler the compiler instance
|
---|
81 | * @returns {void}
|
---|
82 | */
|
---|
83 | apply(compiler) {
|
---|
84 | const options = this.options;
|
---|
85 | const banner = this.banner;
|
---|
86 | const matchObject = ModuleFilenameHelpers.matchObject.bind(
|
---|
87 | undefined,
|
---|
88 | options
|
---|
89 | );
|
---|
90 | const cache = new WeakMap();
|
---|
91 | const stage =
|
---|
92 | this.options.stage || Compilation.PROCESS_ASSETS_STAGE_ADDITIONS;
|
---|
93 |
|
---|
94 | compiler.hooks.compilation.tap("BannerPlugin", compilation => {
|
---|
95 | compilation.hooks.processAssets.tap(
|
---|
96 | {
|
---|
97 | name: "BannerPlugin",
|
---|
98 | stage
|
---|
99 | },
|
---|
100 | () => {
|
---|
101 | for (const chunk of compilation.chunks) {
|
---|
102 | if (options.entryOnly && !chunk.canBeInitial()) {
|
---|
103 | continue;
|
---|
104 | }
|
---|
105 |
|
---|
106 | for (const file of chunk.files) {
|
---|
107 | if (!matchObject(file)) {
|
---|
108 | continue;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** @type {PathData} */
|
---|
112 | const data = { chunk, filename: file };
|
---|
113 |
|
---|
114 | const comment = compilation.getPath(
|
---|
115 | /** @type {TemplatePath} */
|
---|
116 | (banner),
|
---|
117 | data
|
---|
118 | );
|
---|
119 |
|
---|
120 | compilation.updateAsset(file, old => {
|
---|
121 | const cached = cache.get(old);
|
---|
122 | if (!cached || cached.comment !== comment) {
|
---|
123 | const source = options.footer
|
---|
124 | ? new ConcatSource(old, "\n", comment)
|
---|
125 | : new ConcatSource(comment, "\n", old);
|
---|
126 | cache.set(old, { source, comment });
|
---|
127 | return source;
|
---|
128 | }
|
---|
129 | return cached.source;
|
---|
130 | });
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | );
|
---|
135 | });
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | module.exports = BannerPlugin;
|
---|