webpack 4.1.0 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +719 -721
  2. package/bin/webpack.js +69 -10
  3. package/lib/APIPlugin.js +84 -84
  4. package/lib/AmdMainTemplatePlugin.js +75 -77
  5. package/lib/AsyncDependencyToInitialChunkError.js +21 -23
  6. package/lib/BannerPlugin.js +101 -101
  7. package/lib/Chunk.js +477 -469
  8. package/lib/ChunkTemplate.js +51 -53
  9. package/lib/Compilation.js +1858 -1851
  10. package/lib/Compiler.js +493 -478
  11. package/lib/ConcurrentCompilationError.js +19 -0
  12. package/lib/ContextModule.js +696 -685
  13. package/lib/ContextModuleFactory.js +245 -243
  14. package/lib/DefinePlugin.js +197 -197
  15. package/lib/DelegatedModule.js +101 -101
  16. package/lib/DependenciesBlockVariable.js +51 -52
  17. package/lib/Dependency.js +53 -52
  18. package/lib/DllModule.js +54 -54
  19. package/lib/DllModuleFactory.js +29 -29
  20. package/lib/EnvironmentPlugin.js +65 -67
  21. package/lib/EvalDevToolModuleTemplatePlugin.js +60 -60
  22. package/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +105 -105
  23. package/lib/ExportPropertyMainTemplatePlugin.js +40 -40
  24. package/lib/ExternalModule.js +159 -159
  25. package/lib/FunctionModuleTemplatePlugin.js +98 -98
  26. package/lib/HotModuleReplacement.runtime.js +631 -631
  27. package/lib/HotModuleReplacementPlugin.js +407 -406
  28. package/lib/HotUpdateChunkTemplate.js +78 -80
  29. package/lib/JavascriptGenerator.js +228 -229
  30. package/lib/JavascriptModulesPlugin.js +184 -158
  31. package/lib/JsonGenerator.js +42 -42
  32. package/lib/MainTemplate.js +406 -402
  33. package/lib/Module.js +343 -340
  34. package/lib/ModuleBuildError.js +42 -42
  35. package/lib/ModuleError.js +28 -28
  36. package/lib/ModuleFilenameHelpers.js +166 -166
  37. package/lib/ModuleTemplate.js +77 -79
  38. package/lib/ModuleWarning.js +30 -30
  39. package/lib/MultiCompiler.js +271 -259
  40. package/lib/MultiModule.js +78 -75
  41. package/lib/MultiModuleFactory.js +23 -23
  42. package/lib/MultiWatching.js +38 -37
  43. package/lib/NoModeWarning.js +23 -21
  44. package/lib/NormalModule.js +478 -470
  45. package/lib/NormalModuleFactory.js +483 -481
  46. package/lib/OptionsDefaulter.js +80 -86
  47. package/lib/Parser.js +2074 -2071
  48. package/lib/ProgressPlugin.js +231 -231
  49. package/lib/RawModule.js +54 -55
  50. package/lib/RecordIdsPlugin.js +160 -160
  51. package/lib/RemovedPluginError.js +13 -13
  52. package/lib/ResolverFactory.js +64 -67
  53. package/lib/RuntimeTemplate.js +267 -297
  54. package/lib/SetVarMainTemplatePlugin.js +57 -57
  55. package/lib/SourceMapDevToolPlugin.js +302 -308
  56. package/lib/Stats.js +1234 -1212
  57. package/lib/Template.js +205 -205
  58. package/lib/TemplatedPathPlugin.js +170 -143
  59. package/lib/UmdMainTemplatePlugin.js +264 -269
  60. package/lib/Watching.js +193 -193
  61. package/lib/WebAssemblyParser.js +50 -54
  62. package/lib/WebpackOptionsApply.js +401 -401
  63. package/lib/WebpackOptionsDefaulter.js +337 -317
  64. package/lib/WebpackOptionsValidationError.js +316 -319
  65. package/lib/debug/ProfilingPlugin.js +409 -405
  66. package/lib/dependencies/AMDDefineDependencyParserPlugin.js +328 -311
  67. package/lib/dependencies/AMDRequireContextDependency.js +20 -20
  68. package/lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +270 -241
  69. package/lib/dependencies/HarmonyAcceptImportDependency.js +23 -23
  70. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +620 -606
  71. package/lib/dependencies/HarmonyExportSpecifierDependency.js +53 -53
  72. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +214 -214
  73. package/lib/dependencies/HarmonyImportSpecifierDependency.js +154 -156
  74. package/lib/dependencies/ImportDependenciesBlock.js +17 -17
  75. package/lib/dependencies/ImportDependency.js +34 -34
  76. package/lib/dependencies/ImportEagerDependency.js +32 -32
  77. package/lib/dependencies/ImportParserPlugin.js +175 -179
  78. package/lib/dependencies/ImportWeakDependency.js +34 -34
  79. package/lib/dependencies/JsonExportsDependency.js +25 -25
  80. package/lib/dependencies/ModuleDependency.js +20 -20
  81. package/lib/dependencies/NullDependency.js +20 -20
  82. package/lib/dependencies/RequireContextDependency.js +22 -22
  83. package/lib/dependencies/RequireIncludeDependency.js +40 -40
  84. package/lib/dependencies/WebpackMissingModule.js +20 -22
  85. package/lib/node/NodeChunkTemplatePlugin.js +31 -31
  86. package/lib/node/NodeHotUpdateChunkTemplatePlugin.js +36 -36
  87. package/lib/node/NodeMainTemplatePlugin.js +320 -273
  88. package/lib/node/ReadFileCompileWasmMainTemplatePlugin.js +113 -115
  89. package/lib/optimize/AggressiveSplittingPlugin.js +281 -281
  90. package/lib/optimize/ConcatenatedModule.js +1364 -1366
  91. package/lib/optimize/RemoveParentModulesPlugin.js +114 -114
  92. package/lib/optimize/SplitChunksPlugin.js +519 -491
  93. package/lib/performance/SizeLimitsPlugin.js +105 -105
  94. package/lib/util/TrackingSet.js +35 -35
  95. package/lib/util/objectToMap.js +10 -10
  96. package/lib/wasm/WasmModuleTemplatePlugin.js +106 -106
  97. package/lib/web/JsonpChunkTemplatePlugin.js +47 -47
  98. package/lib/web/JsonpExportMainTemplatePlugin.js +47 -47
  99. package/lib/web/JsonpHotUpdateChunkTemplatePlugin.js +39 -39
  100. package/lib/web/JsonpMainTemplatePlugin.js +425 -403
  101. package/lib/webpack.js +182 -179
  102. package/lib/webworker/WebWorkerChunkTemplatePlugin.js +35 -35
  103. package/lib/webworker/WebWorkerHotUpdateChunkTemplatePlugin.js +40 -40
  104. package/lib/webworker/WebWorkerMainTemplatePlugin.js +177 -154
  105. package/package.json +9 -8
  106. package/schemas/WebpackOptions.json +1973 -1951
  107. package/schemas/ajv.absolutePath.js +55 -29
  108. package/schemas/plugins/BannerPlugin.json +85 -85
  109. package/schemas/plugins/DllPlugin.json +28 -28
  110. package/schemas/plugins/DllReferencePlugin.json +99 -99
  111. package/schemas/plugins/HashedModuleIdsPlugin.json +24 -24
  112. package/schemas/plugins/LoaderOptionsPlugin.json +26 -26
  113. package/schemas/plugins/SourceMapDevToolPlugin.json +187 -187
  114. package/schemas/plugins/WatchIgnorePlugin.json +16 -16
  115. package/schemas/plugins/debug/ProfilingPlugin.json +12 -12
  116. package/schemas/plugins/optimize/AggressiveSplittingPlugin.json +22 -22
  117. package/schemas/plugins/optimize/LimitChunkCountPlugin.json +15 -15
  118. package/schemas/plugins/optimize/MinChunkSizePlugin.json +13 -13
package/bin/webpack.js CHANGED
@@ -1,21 +1,80 @@
1
1
  #!/usr/bin/env node
2
+ function runCommand(command, options) {
3
+ const cp = require("child_process");
4
+ return new Promise((resolve, reject) => {
5
+ const executedCommand = cp.spawn(command, options, {
6
+ stdio: "inherit"
7
+ });
8
+
9
+ executedCommand.on("error", error => {
10
+ reject(error);
11
+ });
12
+
13
+ executedCommand.on("exit", code => {
14
+ if (code === 0) {
15
+ resolve(true);
16
+ } else {
17
+ reject();
18
+ }
19
+ });
20
+ });
21
+ }
2
22
 
3
23
  let webpackCliInstalled = false;
4
24
  try {
5
25
  require.resolve("webpack-cli");
6
26
  webpackCliInstalled = true;
7
- } catch (e) {
27
+ } catch (err) {
8
28
  webpackCliInstalled = false;
9
29
  }
10
30
 
11
- if (webpackCliInstalled) {
12
- require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
31
+ if (!webpackCliInstalled) {
32
+ const path = require("path");
33
+ const fs = require("fs");
34
+ const readLine = require("readline");
35
+ const isYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
36
+
37
+ const packageManager = isYarn ? "yarn" : "npm";
38
+ const options = ["install", "-D", "webpack-cli"];
39
+
40
+ if (isYarn) {
41
+ options[0] = "add";
42
+ }
43
+
44
+ const commandToBeRun = `${packageManager} ${options.join(" ")}`;
45
+
46
+ const question = `Would you like to install webpack-cli? (That will run ${commandToBeRun}) `;
47
+
48
+ console.error("The CLI moved into a separate package: webpack-cli");
49
+ const questionInterface = readLine.createInterface({
50
+ input: process.stdin,
51
+ output: process.stdout
52
+ });
53
+ questionInterface.question(question, answer => {
54
+ questionInterface.close();
55
+ switch (answer.toLowerCase()) {
56
+ case "y":
57
+ case "yes":
58
+ case "1": {
59
+ runCommand(packageManager, options)
60
+ .then(result => {
61
+ return require("webpack-cli"); //eslint-disable-line
62
+ })
63
+ .catch(error => {
64
+ console.error(error);
65
+ process.exitCode = 1;
66
+ });
67
+ break;
68
+ }
69
+ default: {
70
+ console.error(
71
+ "It needs to be installed alongside webpack to use the CLI"
72
+ );
73
+ process.exitCode = 1;
74
+ break;
75
+ }
76
+ }
77
+ });
13
78
  } else {
14
- console.error("The CLI moved into a separate package: webpack-cli.");
15
- console.error(
16
- "Please install 'webpack-cli' in addition to webpack itself to use the CLI."
17
- );
18
- console.error("-> When using npm: npm install webpack-cli -D");
19
- console.error("-> When using yarn: yarn add webpack-cli -D");
20
- process.exitCode = 1;
79
+ require("webpack-cli"); // eslint-disable-line
21
80
  }
package/lib/APIPlugin.js CHANGED
@@ -1,84 +1,84 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- "use strict";
6
-
7
- const ConstDependency = require("./dependencies/ConstDependency");
8
- const ParserHelpers = require("./ParserHelpers");
9
-
10
- const NullFactory = require("./NullFactory");
11
-
12
- /* eslint-disable camelcase */
13
- const REPLACEMENTS = {
14
- __webpack_require__: "__webpack_require__",
15
- __webpack_public_path__: "__webpack_require__.p",
16
- __webpack_modules__: "__webpack_require__.m",
17
- __webpack_chunk_load__: "__webpack_require__.e",
18
- __non_webpack_require__: "require",
19
- __webpack_nonce__: "__webpack_require__.nc",
20
- "require.onError": "__webpack_require__.oe"
21
- };
22
- const NO_WEBPACK_REQUIRE = {
23
- __non_webpack_require__: true
24
- };
25
- const REPLACEMENT_TYPES = {
26
- __webpack_public_path__: "string",
27
- __webpack_require__: "function",
28
- __webpack_modules__: "object",
29
- __webpack_chunk_load__: "function",
30
- __webpack_nonce__: "string"
31
- };
32
- /* eslint-enable camelcase */
33
-
34
- class APIPlugin {
35
- apply(compiler) {
36
- compiler.hooks.compilation.tap(
37
- "APIPlugin",
38
- (compilation, { normalModuleFactory }) => {
39
- compilation.dependencyFactories.set(ConstDependency, new NullFactory());
40
- compilation.dependencyTemplates.set(
41
- ConstDependency,
42
- new ConstDependency.Template()
43
- );
44
-
45
- const handler = parser => {
46
- Object.keys(REPLACEMENTS).forEach(key => {
47
- parser.hooks.expression
48
- .for(key)
49
- .tap(
50
- "APIPlugin",
51
- NO_WEBPACK_REQUIRE[key]
52
- ? ParserHelpers.toConstantDependency(
53
- parser,
54
- REPLACEMENTS[key]
55
- )
56
- : ParserHelpers.toConstantDependencyWithWebpackRequire(
57
- parser,
58
- REPLACEMENTS[key]
59
- )
60
- );
61
- parser.hooks.evaluateTypeof
62
- .for(key)
63
- .tap(
64
- "APIPlugin",
65
- ParserHelpers.evaluateToString(REPLACEMENT_TYPES[key])
66
- );
67
- });
68
- };
69
-
70
- normalModuleFactory.hooks.parser
71
- .for("javascript/auto")
72
- .tap("APIPlugin", handler);
73
- normalModuleFactory.hooks.parser
74
- .for("javascript/dynamic")
75
- .tap("APIPlugin", handler);
76
- normalModuleFactory.hooks.parser
77
- .for("javascript/esm")
78
- .tap("APIPlugin", handler);
79
- }
80
- );
81
- }
82
- }
83
-
84
- module.exports = APIPlugin;
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+ "use strict";
6
+
7
+ const ConstDependency = require("./dependencies/ConstDependency");
8
+ const ParserHelpers = require("./ParserHelpers");
9
+
10
+ const NullFactory = require("./NullFactory");
11
+
12
+ /* eslint-disable camelcase */
13
+ const REPLACEMENTS = {
14
+ __webpack_require__: "__webpack_require__",
15
+ __webpack_public_path__: "__webpack_require__.p",
16
+ __webpack_modules__: "__webpack_require__.m",
17
+ __webpack_chunk_load__: "__webpack_require__.e",
18
+ __non_webpack_require__: "require",
19
+ __webpack_nonce__: "__webpack_require__.nc",
20
+ "require.onError": "__webpack_require__.oe"
21
+ };
22
+ const NO_WEBPACK_REQUIRE = {
23
+ __non_webpack_require__: true
24
+ };
25
+ const REPLACEMENT_TYPES = {
26
+ __webpack_public_path__: "string",
27
+ __webpack_require__: "function",
28
+ __webpack_modules__: "object",
29
+ __webpack_chunk_load__: "function",
30
+ __webpack_nonce__: "string"
31
+ };
32
+ /* eslint-enable camelcase */
33
+
34
+ class APIPlugin {
35
+ apply(compiler) {
36
+ compiler.hooks.compilation.tap(
37
+ "APIPlugin",
38
+ (compilation, { normalModuleFactory }) => {
39
+ compilation.dependencyFactories.set(ConstDependency, new NullFactory());
40
+ compilation.dependencyTemplates.set(
41
+ ConstDependency,
42
+ new ConstDependency.Template()
43
+ );
44
+
45
+ const handler = parser => {
46
+ Object.keys(REPLACEMENTS).forEach(key => {
47
+ parser.hooks.expression
48
+ .for(key)
49
+ .tap(
50
+ "APIPlugin",
51
+ NO_WEBPACK_REQUIRE[key]
52
+ ? ParserHelpers.toConstantDependency(
53
+ parser,
54
+ REPLACEMENTS[key]
55
+ )
56
+ : ParserHelpers.toConstantDependencyWithWebpackRequire(
57
+ parser,
58
+ REPLACEMENTS[key]
59
+ )
60
+ );
61
+ parser.hooks.evaluateTypeof
62
+ .for(key)
63
+ .tap(
64
+ "APIPlugin",
65
+ ParserHelpers.evaluateToString(REPLACEMENT_TYPES[key])
66
+ );
67
+ });
68
+ };
69
+
70
+ normalModuleFactory.hooks.parser
71
+ .for("javascript/auto")
72
+ .tap("APIPlugin", handler);
73
+ normalModuleFactory.hooks.parser
74
+ .for("javascript/dynamic")
75
+ .tap("APIPlugin", handler);
76
+ normalModuleFactory.hooks.parser
77
+ .for("javascript/esm")
78
+ .tap("APIPlugin", handler);
79
+ }
80
+ );
81
+ }
82
+ }
83
+
84
+ module.exports = APIPlugin;
@@ -1,77 +1,75 @@
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").ConcatSource;
9
- const Template = require("./Template");
10
-
11
- class AmdMainTemplatePlugin {
12
- constructor(name) {
13
- this.name = name;
14
- }
15
-
16
- apply(compilation) {
17
- const { mainTemplate, chunkTemplate } = compilation;
18
-
19
- const onRenderWithEntry = (source, chunk, hash) => {
20
- const externals = chunk.getModules().filter(m => m.external);
21
- const externalsDepsArray = JSON.stringify(
22
- externals.map(
23
- m => (typeof m.request === "object" ? m.request.amd : m.request)
24
- )
25
- );
26
- const externalsArguments = externals
27
- .map(
28
- m => `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(`${m.id}`)}__`
29
- )
30
- .join(", ");
31
-
32
- if (this.name) {
33
- const name = mainTemplate.getAssetPath(this.name, {
34
- hash,
35
- chunk
36
- });
37
-
38
- return new ConcatSource(
39
- `define(${JSON.stringify(name)}, ${externalsDepsArray}, function(${
40
- externalsArguments
41
- }) { return `,
42
- source,
43
- "});"
44
- );
45
- } else if (externalsArguments) {
46
- return new ConcatSource(
47
- `define(${externalsDepsArray}, function(${
48
- externalsArguments
49
- }) { return `,
50
- source,
51
- "});"
52
- );
53
- } else {
54
- return new ConcatSource("define(function() { return ", source, "});");
55
- }
56
- };
57
-
58
- for (const template of [mainTemplate, chunkTemplate]) {
59
- template.hooks.renderWithEntry.tap(
60
- "AmdMainTemplatePlugin",
61
- onRenderWithEntry
62
- );
63
- }
64
-
65
- mainTemplate.hooks.globalHashPaths.tap("AmdMainTemplatePlugin", paths => {
66
- if (this.name) paths.push(this.name);
67
- return paths;
68
- });
69
-
70
- mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => {
71
- hash.update("exports amd");
72
- hash.update(this.name);
73
- });
74
- }
75
- }
76
-
77
- module.exports = AmdMainTemplatePlugin;
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 Template = require("./Template");
10
+
11
+ class AmdMainTemplatePlugin {
12
+ constructor(name) {
13
+ this.name = name;
14
+ }
15
+
16
+ apply(compilation) {
17
+ const { mainTemplate, chunkTemplate } = compilation;
18
+
19
+ const onRenderWithEntry = (source, chunk, hash) => {
20
+ const externals = chunk.getModules().filter(m => m.external);
21
+ const externalsDepsArray = JSON.stringify(
22
+ externals.map(
23
+ m => (typeof m.request === "object" ? m.request.amd : m.request)
24
+ )
25
+ );
26
+ const externalsArguments = externals
27
+ .map(
28
+ m => `__WEBPACK_EXTERNAL_MODULE_${Template.toIdentifier(`${m.id}`)}__`
29
+ )
30
+ .join(", ");
31
+
32
+ if (this.name) {
33
+ const name = mainTemplate.getAssetPath(this.name, {
34
+ hash,
35
+ chunk
36
+ });
37
+
38
+ return new ConcatSource(
39
+ `define(${JSON.stringify(
40
+ name
41
+ )}, ${externalsDepsArray}, function(${externalsArguments}) { return `,
42
+ source,
43
+ "});"
44
+ );
45
+ } else if (externalsArguments) {
46
+ return new ConcatSource(
47
+ `define(${externalsDepsArray}, function(${externalsArguments}) { return `,
48
+ source,
49
+ "});"
50
+ );
51
+ } else {
52
+ return new ConcatSource("define(function() { return ", source, "});");
53
+ }
54
+ };
55
+
56
+ for (const template of [mainTemplate, chunkTemplate]) {
57
+ template.hooks.renderWithEntry.tap(
58
+ "AmdMainTemplatePlugin",
59
+ onRenderWithEntry
60
+ );
61
+ }
62
+
63
+ mainTemplate.hooks.globalHashPaths.tap("AmdMainTemplatePlugin", paths => {
64
+ if (this.name) paths.push(this.name);
65
+ return paths;
66
+ });
67
+
68
+ mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => {
69
+ hash.update("exports amd");
70
+ hash.update(this.name);
71
+ });
72
+ }
73
+ }
74
+
75
+ module.exports = AmdMainTemplatePlugin;
@@ -1,23 +1,21 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Sean Larkin @thelarkinn
4
- */
5
- "use strict";
6
-
7
- const WebpackError = require("./WebpackError");
8
-
9
- module.exports = class AsyncDependencyToInitialChunkError extends WebpackError {
10
- constructor(chunkName, module, loc) {
11
- super();
12
-
13
- this.name = "AsyncDependencyToInitialChunkError";
14
- this.message = `It's not allowed to load an initial chunk on demand. The chunk name "${
15
- chunkName
16
- }" is already used by an entrypoint.`;
17
- this.module = module;
18
- this.origin = module;
19
- this.originLoc = loc;
20
-
21
- Error.captureStackTrace(this, this.constructor);
22
- }
23
- };
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Sean Larkin @thelarkinn
4
+ */
5
+ "use strict";
6
+
7
+ const WebpackError = require("./WebpackError");
8
+
9
+ module.exports = class AsyncDependencyToInitialChunkError extends WebpackError {
10
+ constructor(chunkName, module, loc) {
11
+ super();
12
+
13
+ this.name = "AsyncDependencyToInitialChunkError";
14
+ this.message = `It's not allowed to load an initial chunk on demand. The chunk name "${chunkName}" is already used by an entrypoint.`;
15
+ this.module = module;
16
+ this.origin = module;
17
+ this.originLoc = loc;
18
+
19
+ Error.captureStackTrace(this, this.constructor);
20
+ }
21
+ };
@@ -1,101 +1,101 @@
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").ConcatSource;
9
- const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
10
- const Template = require("./Template");
11
-
12
- const validateOptions = require("schema-utils");
13
- const schema = require("../schemas/plugins/BannerPlugin.json");
14
-
15
- const wrapComment = str => {
16
- if (!str.includes("\n")) return Template.toComment(str);
17
- return `/*!\n * ${str
18
- .replace(/\*\//g, "* /")
19
- .split("\n")
20
- .join("\n * ")}\n */`;
21
- };
22
-
23
- class BannerPlugin {
24
- constructor(options) {
25
- if (arguments.length > 1)
26
- throw new Error(
27
- "BannerPlugin only takes one argument (pass an options object)"
28
- );
29
-
30
- validateOptions(schema, options, "Banner Plugin");
31
-
32
- if (typeof options === "string")
33
- options = {
34
- banner: options
35
- };
36
- this.options = options || {};
37
- this.banner = this.options.raw
38
- ? options.banner
39
- : wrapComment(options.banner);
40
- }
41
-
42
- apply(compiler) {
43
- const options = this.options;
44
- const banner = this.banner;
45
- const matchObject = ModuleFilenameHelpers.matchObject.bind(
46
- undefined,
47
- options
48
- );
49
-
50
- compiler.hooks.compilation.tap("BannerPlugin", compilation => {
51
- compilation.hooks.optimizeChunkAssets.tap("BannerPlugin", chunks => {
52
- for (const chunk of chunks) {
53
- if (options.entryOnly && !chunk.canBeInitial()) {
54
- continue;
55
- }
56
-
57
- for (const file of chunk.files) {
58
- if (!matchObject(file)) {
59
- continue;
60
- }
61
-
62
- let basename;
63
- let query = "";
64
- let filename = file;
65
- const hash = compilation.hash;
66
- const querySplit = filename.indexOf("?");
67
-
68
- if (querySplit >= 0) {
69
- query = filename.substr(querySplit);
70
- filename = filename.substr(0, querySplit);
71
- }
72
-
73
- const lastSlashIndex = filename.lastIndexOf("/");
74
-
75
- if (lastSlashIndex === -1) {
76
- basename = filename;
77
- } else {
78
- basename = filename.substr(lastSlashIndex + 1);
79
- }
80
-
81
- const comment = compilation.getPath(banner, {
82
- hash,
83
- chunk,
84
- filename,
85
- basename,
86
- query
87
- });
88
-
89
- compilation.assets[file] = new ConcatSource(
90
- comment,
91
- "\n",
92
- compilation.assets[file]
93
- );
94
- }
95
- }
96
- });
97
- });
98
- }
99
- }
100
-
101
- module.exports = BannerPlugin;
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 ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
10
+ const Template = require("./Template");
11
+
12
+ const validateOptions = require("schema-utils");
13
+ const schema = require("../schemas/plugins/BannerPlugin.json");
14
+
15
+ const wrapComment = str => {
16
+ if (!str.includes("\n")) return Template.toComment(str);
17
+ return `/*!\n * ${str
18
+ .replace(/\*\//g, "* /")
19
+ .split("\n")
20
+ .join("\n * ")}\n */`;
21
+ };
22
+
23
+ class BannerPlugin {
24
+ constructor(options) {
25
+ if (arguments.length > 1)
26
+ throw new Error(
27
+ "BannerPlugin only takes one argument (pass an options object)"
28
+ );
29
+
30
+ validateOptions(schema, options, "Banner Plugin");
31
+
32
+ if (typeof options === "string")
33
+ options = {
34
+ banner: options
35
+ };
36
+ this.options = options || {};
37
+ this.banner = this.options.raw
38
+ ? options.banner
39
+ : wrapComment(options.banner);
40
+ }
41
+
42
+ apply(compiler) {
43
+ const options = this.options;
44
+ const banner = this.banner;
45
+ const matchObject = ModuleFilenameHelpers.matchObject.bind(
46
+ undefined,
47
+ options
48
+ );
49
+
50
+ compiler.hooks.compilation.tap("BannerPlugin", compilation => {
51
+ compilation.hooks.optimizeChunkAssets.tap("BannerPlugin", chunks => {
52
+ for (const chunk of chunks) {
53
+ if (options.entryOnly && !chunk.canBeInitial()) {
54
+ continue;
55
+ }
56
+
57
+ for (const file of chunk.files) {
58
+ if (!matchObject(file)) {
59
+ continue;
60
+ }
61
+
62
+ let basename;
63
+ let query = "";
64
+ let filename = file;
65
+ const hash = compilation.hash;
66
+ const querySplit = filename.indexOf("?");
67
+
68
+ if (querySplit >= 0) {
69
+ query = filename.substr(querySplit);
70
+ filename = filename.substr(0, querySplit);
71
+ }
72
+
73
+ const lastSlashIndex = filename.lastIndexOf("/");
74
+
75
+ if (lastSlashIndex === -1) {
76
+ basename = filename;
77
+ } else {
78
+ basename = filename.substr(lastSlashIndex + 1);
79
+ }
80
+
81
+ const comment = compilation.getPath(banner, {
82
+ hash,
83
+ chunk,
84
+ filename,
85
+ basename,
86
+ query
87
+ });
88
+
89
+ compilation.assets[file] = new ConcatSource(
90
+ comment,
91
+ "\n",
92
+ compilation.assets[file]
93
+ );
94
+ }
95
+ }
96
+ });
97
+ });
98
+ }
99
+ }
100
+
101
+ module.exports = BannerPlugin;