Skip to content

Commit

Permalink
test: 🤖 banner plugin (#3579)
Browse files Browse the repository at this point in the history
chore: 🤖 banner plugin
  • Loading branch information
IWANABETHATGUY authored Jun 20, 2023
1 parent 33e0faa commit 8a5c47f
Show file tree
Hide file tree
Showing 12 changed files with 1,096 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/rspack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"react-relay": "^14.1.0",
"sass": "^1.56.2",
"sass-loader": "^13.2.0",
"schema-utils": "^4.0.0",
"sinon": "14.0.0",
"source-map": "^0.7.4",
"terser": "5.16.1",
Expand All @@ -65,7 +66,6 @@
"graceful-fs": "4.2.10",
"neo-async": "2.6.2",
"react-refresh": "0.14.0",
"schema-utils": "^4.0.0",
"tapable": "2.2.1",
"watchpack": "^2.4.0",
"webpack-sources": "3.2.3",
Expand Down
4 changes: 3 additions & 1 deletion packages/rspack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import BannerPlugin = require("./lib/BannerPlugin");

export * from "./compiler";
export * from "./multiCompiler";
export * from "./compilation";
Expand All @@ -8,7 +10,7 @@ export * from "./multiStats";
export * from "./chunk_group";
export * from "./normalModuleFactory";
export { cachedCleverMerge as cleverMerge } from "./util/cleverMerge";

export { BannerPlugin };
import { Configuration } from "./config";
// TODO(hyf0): should remove this re-export when we cleanup the exports of `@rspack/core`
export type OptimizationSplitChunksOptions = NonNullable<
Expand Down
127 changes: 127 additions & 0 deletions packages/rspack/src/lib/BannerPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
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"
}
);

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")
.trimEnd()}\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
);
const cache = new WeakMap();

compiler.hooks.compilation.tap("BannerPlugin", compilation => {
compilation.hooks.processAssets.tap(
{
name: "BannerPlugin",
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
},
() => {
for (const chunk of compilation.chunks) {
if (options.entryOnly && !chunk.canBeInitial()) {
continue;
}

for (const file of chunk.files) {
if (!matchObject(file)) {
continue;
}

const data = {
// chunk,
filename: file
};

let normalizedBanner =
typeof banner === "function" ? banner({ chunk }) : banner;
const comment = compilation.getPath(normalizedBanner, data);

compilation.updateAsset(file, old => {
let cached = cache.get(old);
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;
49 changes: 49 additions & 0 deletions packages/rspack/src/lib/ModuleFilenameHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/

"use strict";

/** @typedef {import("./ChunkGraph")} ChunkGraph */
/** @typedef {import("./Module")} Module */
/** @typedef {import("./RequestShortener")} RequestShortener */
/** @typedef {typeof import("./util/Hash")} Hash */

const ModuleFilenameHelpers = exports;

const asRegExp = test => {
if (typeof test === "string") {
test = new RegExp("^" + test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
}
return test;
};

ModuleFilenameHelpers.matchPart = (str, test) => {
if (!test) return true;
test = asRegExp(test);
if (Array.isArray(test)) {
return test.map(asRegExp).some(regExp => regExp.test(str));
} else {
return test.test(str);
}
};

ModuleFilenameHelpers.matchObject = (obj, str) => {
if (obj.test) {
if (!ModuleFilenameHelpers.matchPart(str, obj.test)) {
return false;
}
}
if (obj.include) {
if (!ModuleFilenameHelpers.matchPart(str, obj.include)) {
return false;
}
}
if (obj.exclude) {
if (ModuleFilenameHelpers.matchPart(str, obj.exclude)) {
return false;
}
}
return true;
};
Loading

0 comments on commit 8a5c47f

Please sign in to comment.