Skip to content

Commit f73e87d

Browse files
committed
refactor(BannerPlugin): update BannerPlugin to es2015
1 parent 79791a5 commit f73e87d

File tree

1 file changed

+41
-30
lines changed

1 file changed

+41
-30
lines changed

lib/BannerPlugin.js

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,51 @@
11
/*
2-
MIT License http://www.opensource.org/licenses/mit-license.php
3-
Author Tobias Koppers @sokra
4-
*/
5-
var ConcatSource = require("webpack-sources").ConcatSource;
6-
var ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
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").ConcatSource;
9+
const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
710

811
function wrapComment(str) {
9-
if(str.indexOf("\n") < 0) return "/*! " + str + " */";
10-
return "/*!\n * " + str.split("\n").join("\n * ") + "\n */";
12+
if(!str.includes("\n")) return `/*! ${str} */`;
13+
return `/*!\n * ${str.split("\n").join("\n * ")}\n */`;
1114
}
1215

13-
function BannerPlugin(options) {
14-
if(arguments.length > 1)
15-
throw new Error("BannerPlugin only takes one argument (pass an options object)");
16-
if(typeof options === "string")
17-
options = {
18-
banner: options
19-
};
20-
this.options = options || {};
21-
this.banner = this.options.raw ? options.banner : wrapComment(options.banner);
22-
}
23-
module.exports = BannerPlugin;
16+
class BannerPlugin {
17+
constructor(options) {
18+
if(arguments.length > 1)
19+
throw new Error("BannerPlugin only takes one argument (pass an options object)");
20+
if(typeof options === "string")
21+
options = {
22+
banner: options
23+
};
24+
this.options = options || {};
25+
this.banner = this.options.raw ? options.banner : wrapComment(options.banner);
26+
}
2427

25-
BannerPlugin.prototype.apply = function(compiler) {
26-
var options = this.options;
27-
var banner = this.banner;
28+
apply(compiler) {
29+
let options = this.options;
30+
let banner = this.banner;
2831

29-
compiler.plugin("compilation", function(compilation) {
30-
compilation.plugin("optimize-chunk-assets", function(chunks, callback) {
31-
chunks.forEach(function(chunk) {
32-
if(options.entryOnly && !chunk.isInitial()) return;
33-
chunk.files.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options)).forEach(function(file) {
34-
compilation.assets[file] = new ConcatSource(banner, "\n", compilation.assets[file]);
32+
compiler.plugin("compilation", (compilation) => {
33+
compilation.plugin("optimize-chunk-assets", (chunks, callback) => {
34+
chunks.forEach((chunk) => {
35+
if(options.entryOnly && !chunk.isInitial()) return;
36+
37+
chunk.files
38+
.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options))
39+
.forEach((file) =>
40+
compilation.assets[file] = new ConcatSource(
41+
banner, "\n", compilation.assets[file]
42+
)
43+
);
3544
});
45+
callback();
3646
});
37-
callback();
3847
});
39-
});
40-
};
48+
}
49+
}
50+
51+
module.exports = BannerPlugin;

0 commit comments

Comments
 (0)