Skip to content

Commit

Permalink
Merge pull request #15617 from DavidTanner/bannerAsFooter
Browse files Browse the repository at this point in the history
Allow banner to be placed as a footer
  • Loading branch information
sokra committed Apr 4, 2022
2 parents 2a58ce7 + 4f2adf9 commit 05ebf5b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
4 changes: 4 additions & 0 deletions declarations/plugins/BannerPlugin.d.ts
Expand Up @@ -38,6 +38,10 @@ export interface BannerPluginOptions {
* Exclude all modules matching any of these conditions.
*/
exclude?: Rules;
/**
* If true, banner will be placed at the end of the output.
*/
footer?: boolean;
/**
* Include all modules matching any of these conditions.
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/BannerPlugin.js
Expand Up @@ -106,7 +106,9 @@ class BannerPlugin {
compilation.updateAsset(file, old => {
let cached = cache.get(old);
if (!cached || cached.comment !== comment) {
const source = new ConcatSource(comment, "\n", old);
const source = options.footer
? new ConcatSource(old, "\n", comment)
: new ConcatSource(comment, "\n", old);
cache.set(old, { source, comment });
return source;
}
Expand Down
2 changes: 1 addition & 1 deletion schemas/plugins/BannerPlugin.check.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions schemas/plugins/BannerPlugin.json
Expand Up @@ -73,6 +73,10 @@
}
]
},
"footer": {
"description": "If true, banner will be placed at the end of the output.",
"type": "boolean"
},
"include": {
"description": "Include all modules matching any of these conditions.",
"oneOf": [
Expand Down
48 changes: 44 additions & 4 deletions test/BannerPlugin.test.js
Expand Up @@ -5,11 +5,15 @@ const fs = require("graceful-fs");

const webpack = require("..");

const pluginDir = path.join(__dirname, "js", "BannerPlugin");
const outputDir = path.join(pluginDir, "output");

it("should cache assets", done => {
const entry1File = path.join(__dirname, "js", "BannerPlugin", "entry1.js");
const entry2File = path.join(__dirname, "js", "BannerPlugin", "entry2.js");
const entry1File = path.join(pluginDir, "entry1.js");
const entry2File = path.join(pluginDir, "entry2.js");
const outputFile = path.join(outputDir, "entry1.js");
try {
fs.mkdirSync(path.join(__dirname, "js", "BannerPlugin"), {
fs.mkdirSync(path.join(pluginDir), {
recursive: true
});
} catch (e) {
Expand All @@ -22,14 +26,16 @@ it("should cache assets", done => {
entry2: entry2File
},
output: {
path: path.join(__dirname, "js", "BannerPlugin", "output")
path: outputDir
},
plugins: [new webpack.BannerPlugin("banner is a string")]
});
fs.writeFileSync(entry1File, "1", "utf-8");
fs.writeFileSync(entry2File, "1", "utf-8");
compiler.run(err => {
if (err) return done(err);
const footerFileResults = fs.readFileSync(outputFile, "utf8").split("\n");
expect(footerFileResults[0]).toBe("/*! banner is a string */");
fs.writeFileSync(entry2File, "2", "utf-8");
compiler.run((err, stats) => {
const { assets } = stats.toJson();
Expand All @@ -39,3 +45,37 @@ it("should cache assets", done => {
});
});
});

it("can place banner as footer", done => {
const footerFile = path.join(pluginDir, "footerFile.js");
const outputFile = path.join(outputDir, "footerFile.js");
try {
fs.mkdirSync(path.join(pluginDir), {
recursive: true
});
} catch (e) {
// empty
}
const compiler = webpack({
mode: "development",
entry: {
footerFile: footerFile
},
output: {
path: outputDir
},
plugins: [
new webpack.BannerPlugin({
banner: "banner is a string",
footer: true
})
]
});
fs.writeFileSync(footerFile, "footer", "utf-8");
compiler.run(err => {
if (err) return done(err);
const footerFileResults = fs.readFileSync(outputFile, "utf8").split("\n");
expect(footerFileResults.pop()).toBe("/*! banner is a string */");
done();
});
});
5 changes: 5 additions & 0 deletions types.d.ts
Expand Up @@ -430,6 +430,11 @@ declare interface BannerPluginOptions {
*/
exclude?: string | RegExp | Rule[];

/**
* If true, banner will be placed at the end of the output.
*/
footer?: boolean;

/**
* Include all modules matching any of these conditions.
*/
Expand Down

0 comments on commit 05ebf5b

Please sign in to comment.