|
1 | 1 | /* |
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"); |
7 | 10 |
|
8 | 11 | 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 */`; |
11 | 14 | } |
12 | 15 |
|
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 | + } |
24 | 27 |
|
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; |
28 | 31 |
|
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 | + ); |
35 | 44 | }); |
| 45 | + callback(); |
36 | 46 | }); |
37 | | - callback(); |
38 | 47 | }); |
39 | | - }); |
40 | | -}; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +module.exports = BannerPlugin; |
0 commit comments