webpack 4.19.0 → 4.20.2

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 (53) hide show
  1. package/README.md +4 -0
  2. package/lib/AmdMainTemplatePlugin.js +5 -3
  3. package/lib/BannerPlugin.js +13 -5
  4. package/lib/Chunk.js +3 -0
  5. package/lib/Compilation.js +23 -18
  6. package/lib/Compiler.js +6 -12
  7. package/lib/DllPlugin.js +5 -0
  8. package/lib/DllReferencePlugin.js +70 -46
  9. package/lib/DynamicEntryPlugin.js +4 -3
  10. package/lib/EntryOptionPlugin.js +2 -1
  11. package/lib/ExternalModule.js +16 -4
  12. package/lib/HashedModuleIdsPlugin.js +9 -1
  13. package/lib/HotUpdateChunk.js +1 -0
  14. package/lib/IgnorePlugin.js +10 -14
  15. package/lib/LibraryTemplatePlugin.js +33 -10
  16. package/lib/LoaderOptionsPlugin.js +5 -0
  17. package/lib/Module.js +4 -0
  18. package/lib/SourceMapDevToolPlugin.js +16 -3
  19. package/lib/Template.js +1 -0
  20. package/lib/UmdMainTemplatePlugin.js +2 -1
  21. package/lib/WatchIgnorePlugin.js +5 -0
  22. package/lib/WebpackOptionsApply.js +18 -2
  23. package/lib/WebpackOptionsDefaulter.js +5 -1
  24. package/lib/debug/ProfilingPlugin.js +6 -0
  25. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +16 -1
  26. package/lib/dependencies/HarmonyExportExpressionDependency.js +7 -2
  27. package/lib/dependencies/ImportParserPlugin.js +1 -1
  28. package/lib/dependencies/WebAssemblyImportDependency.js +9 -1
  29. package/lib/node/NodeMainTemplatePlugin.js +2 -2
  30. package/lib/optimize/AggressiveSplittingPlugin.js +9 -2
  31. package/lib/optimize/ConcatenatedModule.js +10 -2
  32. package/lib/optimize/LimitChunkCountPlugin.js +9 -2
  33. package/lib/optimize/MinChunkSizePlugin.js +5 -0
  34. package/lib/optimize/OccurrenceChunkOrderPlugin.js +5 -0
  35. package/lib/optimize/OccurrenceModuleOrderPlugin.js +5 -0
  36. package/lib/web/JsonpExportMainTemplatePlugin.js +3 -0
  37. package/lib/webpack.js +12 -1
  38. package/package.json +11 -8
  39. package/schemas/WebpackOptions.json +1293 -1113
  40. package/schemas/plugins/BannerPlugin.json +34 -27
  41. package/schemas/plugins/DllPlugin.json +18 -16
  42. package/schemas/plugins/DllReferencePlugin.json +180 -68
  43. package/schemas/plugins/HashedModuleIdsPlugin.json +9 -3
  44. package/schemas/plugins/IgnorePlugin.json +17 -11
  45. package/schemas/plugins/LoaderOptionsPlugin.json +27 -26
  46. package/schemas/plugins/SourceMapDevToolPlugin.json +87 -83
  47. package/schemas/plugins/WatchIgnorePlugin.json +18 -16
  48. package/schemas/plugins/debug/ProfilingPlugin.json +13 -12
  49. package/schemas/plugins/optimize/AggressiveSplittingPlugin.json +23 -22
  50. package/schemas/plugins/optimize/LimitChunkCountPlugin.json +16 -15
  51. package/schemas/plugins/optimize/MinChunkSizePlugin.json +4 -3
  52. package/schemas/plugins/optimize/OccurrenceOrderChunkIdsPlugin.json +2 -1
  53. package/schemas/plugins/optimize/OccurrenceOrderModuleIdsPlugin.json +2 -1
package/README.md CHANGED
@@ -11,6 +11,7 @@
11
11
  [![deps][deps]][deps-url]
12
12
  [![tests][tests]][tests-url]
13
13
  [![builds][builds]][builds-url]
14
+ [![builds2][builds2]][builds2-url]
14
15
  [![coverage][cover]][cover-url]
15
16
  [![licenses][licenses]][licenses-url]
16
17
 
@@ -752,6 +753,9 @@ src="https://static.monei.net/monei-logo.svg" height="30" alt="MONEI"></a>
752
753
  [builds-url]: https://ci.appveyor.com/project/sokra/webpack/branch/master
753
754
  [builds]: https://ci.appveyor.com/api/projects/status/github/webpack/webpack?svg=true
754
755
 
756
+ [builds2]: https://dev.azure.com/webpack/webpack/_apis/build/status/webpack.webpack
757
+ [builds2-url]: https://dev.azure.com/webpack/webpack/_build/latest?definitionId=3
758
+
755
759
  [licenses-url]: https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fwebpack%2Fwebpack?ref=badge_shield
756
760
  [licenses]: https://app.fossa.io/api/projects/git%2Bhttps%3A%2F%2Fgithub.com%2Fwebpack%2Fwebpack.svg?type=shield
757
761
 
@@ -12,10 +12,10 @@ const Template = require("./Template");
12
12
 
13
13
  class AmdMainTemplatePlugin {
14
14
  /**
15
- * @param {string} name the library name
15
+ * @param {string=} name the library name
16
16
  */
17
17
  constructor(name) {
18
- /** @type {string} */
18
+ /** @type {string=} */
19
19
  this.name = name;
20
20
  }
21
21
 
@@ -79,7 +79,9 @@ class AmdMainTemplatePlugin {
79
79
 
80
80
  mainTemplate.hooks.hash.tap("AmdMainTemplatePlugin", hash => {
81
81
  hash.update("exports amd");
82
- hash.update(this.name);
82
+ if (this.name) {
83
+ hash.update(this.name);
84
+ }
83
85
  });
84
86
  }
85
87
  }
@@ -12,6 +12,9 @@ const Template = require("./Template");
12
12
  const validateOptions = require("schema-utils");
13
13
  const schema = require("../schemas/plugins/BannerPlugin.json");
14
14
 
15
+ /** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginArgument} BannerPluginArgument */
16
+ /** @typedef {import("../declarations/plugins/BannerPlugin").BannerPluginOptions} BannerPluginOptions */
17
+
15
18
  const wrapComment = str => {
16
19
  if (!str.includes("\n")) {
17
20
  return Template.toComment(str);
@@ -23,6 +26,9 @@ const wrapComment = str => {
23
26
  };
24
27
 
25
28
  class BannerPlugin {
29
+ /**
30
+ * @param {BannerPluginArgument} options options object
31
+ */
26
32
  constructor(options) {
27
33
  if (arguments.length > 1) {
28
34
  throw new Error(
@@ -38,17 +44,19 @@ class BannerPlugin {
38
44
  };
39
45
  }
40
46
 
41
- this.options = options || {};
47
+ /** @type {BannerPluginOptions} */
48
+ this.options = options;
42
49
 
43
- if (typeof options.banner === "function") {
44
- const getBanner = this.options.banner;
50
+ const bannerOption = options.banner;
51
+ if (typeof bannerOption === "function") {
52
+ const getBanner = bannerOption;
45
53
  this.banner = this.options.raw
46
54
  ? getBanner
47
55
  : data => wrapComment(getBanner(data));
48
56
  } else {
49
57
  const banner = this.options.raw
50
- ? this.options.banner
51
- : wrapComment(this.options.banner);
58
+ ? bannerOption
59
+ : wrapComment(bannerOption);
52
60
  this.banner = () => banner;
53
61
  }
54
62
  }
package/lib/Chunk.js CHANGED
@@ -339,6 +339,9 @@ class Chunk {
339
339
  return this._modules.has(module);
340
340
  }
341
341
 
342
+ /**
343
+ * @returns {Module[]} an array of modules (do not modify)
344
+ */
342
345
  getModules() {
343
346
  return this._modules.getFromCache(getArray);
344
347
  }
@@ -402,7 +402,8 @@ class Compilation extends Tapable {
402
402
  this.inputFileSystem = compiler.inputFileSystem;
403
403
  this.requestShortener = compiler.requestShortener;
404
404
 
405
- const options = (this.options = compiler.options);
405
+ const options = compiler.options;
406
+ this.options = options;
406
407
  this.outputOptions = options && options.output;
407
408
  /** @type {boolean=} */
408
409
  this.bail = options && options.bail;
@@ -2253,24 +2254,28 @@ class Compilation extends Tapable {
2253
2254
  for (let i = 0; i < chunks.length; i++) {
2254
2255
  const chunk = chunks[i];
2255
2256
  const chunkHash = createHash(hashFunction);
2256
- if (outputOptions.hashSalt) {
2257
- chunkHash.update(outputOptions.hashSalt);
2257
+ try {
2258
+ if (outputOptions.hashSalt) {
2259
+ chunkHash.update(outputOptions.hashSalt);
2260
+ }
2261
+ chunk.updateHash(chunkHash);
2262
+ const template = chunk.hasRuntime()
2263
+ ? this.mainTemplate
2264
+ : this.chunkTemplate;
2265
+ template.updateHashForChunk(
2266
+ chunkHash,
2267
+ chunk,
2268
+ this.moduleTemplates.javascript,
2269
+ this.dependencyTemplates
2270
+ );
2271
+ this.hooks.chunkHash.call(chunk, chunkHash);
2272
+ chunk.hash = chunkHash.digest(hashDigest);
2273
+ hash.update(chunk.hash);
2274
+ chunk.renderedHash = chunk.hash.substr(0, hashDigestLength);
2275
+ this.hooks.contentHash.call(chunk);
2276
+ } catch (err) {
2277
+ this.errors.push(new ChunkRenderError(chunk, "", err));
2258
2278
  }
2259
- chunk.updateHash(chunkHash);
2260
- const template = chunk.hasRuntime()
2261
- ? this.mainTemplate
2262
- : this.chunkTemplate;
2263
- template.updateHashForChunk(
2264
- chunkHash,
2265
- chunk,
2266
- this.moduleTemplates.javascript,
2267
- this.dependencyTemplates
2268
- );
2269
- this.hooks.chunkHash.call(chunk, chunkHash);
2270
- chunk.hash = chunkHash.digest(hashDigest);
2271
- hash.update(chunk.hash);
2272
- chunk.renderedHash = chunk.hash.substr(0, hashDigestLength);
2273
- this.hooks.contentHash.call(chunk);
2274
2279
  }
2275
2280
  this.fullHash = hash.digest(hashDigest);
2276
2281
  this.hash = this.fullHash.substr(0, hashDigestLength);
package/lib/Compiler.js CHANGED
@@ -27,6 +27,9 @@ const RequestShortener = require("./RequestShortener");
27
27
  const { makePathsRelative } = require("./util/identifier");
28
28
  const ConcurrentCompilationError = require("./ConcurrentCompilationError");
29
29
 
30
+ /** @typedef {import("../declarations/WebpackOptions").Entry} Entry */
31
+ /** @typedef {import("../declarations/WebpackOptions").WebpackOptions} WebpackOptions */
32
+
30
33
  /**
31
34
  * @typedef {Object} CompilationParams
32
35
  * @property {NormalModuleFactory} normalModuleFactory
@@ -34,16 +37,6 @@ const ConcurrentCompilationError = require("./ConcurrentCompilationError");
34
37
  * @property {Set<string>} compilationDependencies
35
38
  */
36
39
 
37
- /** @typedef {string|string[]} EntryValues */
38
- /** @typedef {Record<string, EntryValues>} EntryOptionValues */
39
-
40
- /**
41
- * @callback EntryOptionValuesFunction
42
- * @returns {EntryOptionValues | EntryValues} the computed value
43
- */
44
-
45
- /** @typedef {EntryOptionValuesFunction | EntryOptionValues | EntryValues} EntryOptions */
46
-
47
40
  class Compiler extends Tapable {
48
41
  constructor(context) {
49
42
  super();
@@ -100,7 +93,7 @@ class Compiler extends Tapable {
100
93
  afterPlugins: new SyncHook(["compiler"]),
101
94
  /** @type {SyncHook<Compiler>} */
102
95
  afterResolvers: new SyncHook(["compiler"]),
103
- /** @type {SyncBailHook<string, EntryOptions>} */
96
+ /** @type {SyncBailHook<string, Entry>} */
104
97
  entryOption: new SyncBailHook(["context", "entry"])
105
98
  };
106
99
 
@@ -182,7 +175,8 @@ class Compiler extends Tapable {
182
175
  }
183
176
  };
184
177
 
185
- this.options = {};
178
+ /** @type {WebpackOptions} */
179
+ this.options = /** @type {WebpackOptions} */ ({});
186
180
 
187
181
  this.context = context;
188
182
 
package/lib/DllPlugin.js CHANGED
@@ -11,7 +11,12 @@ const FlagInitialModulesAsUsedPlugin = require("./FlagInitialModulesAsUsedPlugin
11
11
  const validateOptions = require("schema-utils");
12
12
  const schema = require("../schemas/plugins/DllPlugin.json");
13
13
 
14
+ /** @typedef {import("../declarations/plugins/DllPlugin").DllPluginOptions} DllPluginOptions */
15
+
14
16
  class DllPlugin {
17
+ /**
18
+ * @param {DllPluginOptions} options options object
19
+ */
15
20
  constructor(options) {
16
21
  validateOptions(schema, options, "Dll Plugin");
17
22
  this.options = options;
@@ -16,7 +16,13 @@ const WebpackError = require("./WebpackError");
16
16
  const validateOptions = require("schema-utils");
17
17
  const schema = require("../schemas/plugins/DllReferencePlugin.json");
18
18
 
19
+ /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */
20
+ /** @typedef {import("../declarations/plugins/DllReferencePlugin").DllReferencePluginOptionsManifest} DllReferencePluginOptionsManifest */
21
+
19
22
  class DllReferencePlugin {
23
+ /**
24
+ * @param {DllReferencePluginOptions} options options object
25
+ */
20
26
  constructor(options) {
21
27
  validateOptions(schema, options, "Dll Reference Plugin");
22
28
  this.options = options;
@@ -40,55 +46,71 @@ class DllReferencePlugin {
40
46
  compiler.hooks.beforeCompile.tapAsync(
41
47
  "DllReferencePlugin",
42
48
  (params, callback) => {
43
- const manifest = this.options.manifest;
44
- if (typeof manifest === "string") {
45
- params.compilationDependencies.add(manifest);
46
- compiler.inputFileSystem.readFile(manifest, (err, result) => {
47
- if (err) return callback(err);
48
- // Catch errors parsing the manifest so that blank
49
- // or malformed manifest files don't kill the process.
50
- try {
51
- params["dll reference " + manifest] = parseJson(
52
- result.toString("utf-8")
53
- );
54
- } catch (e) {
55
- // Store the error in the params so that it can
56
- // be added as a compilation error later on.
57
- const manifestPath = makePathsRelative(
58
- compiler.options.context,
59
- manifest
60
- );
61
- params[
62
- "dll reference parse error " + manifest
63
- ] = new DllManifestError(manifestPath, e.message);
64
- }
65
- return callback();
66
- });
67
- } else {
68
- return callback();
49
+ if ("manifest" in this.options) {
50
+ const manifest = this.options.manifest;
51
+ if (typeof manifest === "string") {
52
+ params.compilationDependencies.add(manifest);
53
+ compiler.inputFileSystem.readFile(manifest, (err, result) => {
54
+ if (err) return callback(err);
55
+ // Catch errors parsing the manifest so that blank
56
+ // or malformed manifest files don't kill the process.
57
+ try {
58
+ params["dll reference " + manifest] = parseJson(
59
+ result.toString("utf-8")
60
+ );
61
+ } catch (e) {
62
+ // Store the error in the params so that it can
63
+ // be added as a compilation error later on.
64
+ const manifestPath = makePathsRelative(
65
+ compiler.options.context,
66
+ manifest
67
+ );
68
+ params[
69
+ "dll reference parse error " + manifest
70
+ ] = new DllManifestError(manifestPath, e.message);
71
+ }
72
+ return callback();
73
+ });
74
+ return;
75
+ }
69
76
  }
77
+ return callback();
70
78
  }
71
79
  );
72
80
 
73
81
  compiler.hooks.compile.tap("DllReferencePlugin", params => {
74
- let manifest = this.options.manifest;
75
- if (typeof manifest === "string") {
76
- // If there was an error parsing the manifest
77
- // file, exit now because the error will be added
78
- // as a compilation error in the "compilation" hook.
79
- if (params["dll reference parse error " + manifest]) {
80
- return;
82
+ let name = this.options.name;
83
+ let sourceType = this.options.sourceType;
84
+ let content =
85
+ "content" in this.options ? this.options.content : undefined;
86
+ if ("manifest" in this.options) {
87
+ let manifestParameter = this.options.manifest;
88
+ let manifest;
89
+ if (typeof manifestParameter === "string") {
90
+ // If there was an error parsing the manifest
91
+ // file, exit now because the error will be added
92
+ // as a compilation error in the "compilation" hook.
93
+ if (params["dll reference parse error " + manifestParameter]) {
94
+ return;
95
+ }
96
+ manifest =
97
+ /** @type {DllReferencePluginOptionsManifest} */ (params[
98
+ "dll reference " + manifestParameter
99
+ ]);
100
+ } else {
101
+ manifest = manifestParameter;
102
+ }
103
+ if (manifest) {
104
+ if (!name) name = manifest.name;
105
+ if (!sourceType) sourceType = manifest.type;
106
+ if (!content) content = manifest.content;
81
107
  }
82
- manifest = params["dll reference " + manifest];
83
108
  }
84
- const name = this.options.name || manifest.name;
85
- const sourceType =
86
- this.options.sourceType || (manifest && manifest.type) || "var";
87
109
  const externals = {};
88
110
  const source = "dll-reference " + name;
89
111
  externals[source] = name;
90
112
  const normalModuleFactory = params.normalModuleFactory;
91
- new ExternalModuleFactoryPlugin(sourceType, externals).apply(
113
+ new ExternalModuleFactoryPlugin(sourceType || "var", externals).apply(
92
114
  normalModuleFactory
93
115
  );
94
116
  new DelegatedModuleFactoryPlugin({
@@ -96,7 +118,7 @@ class DllReferencePlugin {
96
118
  type: this.options.type,
97
119
  scope: this.options.scope,
98
120
  context: this.options.context || compiler.options.context,
99
- content: this.options.content || manifest.content,
121
+ content,
100
122
  extensions: this.options.extensions
101
123
  }).apply(normalModuleFactory);
102
124
  });
@@ -104,13 +126,15 @@ class DllReferencePlugin {
104
126
  compiler.hooks.compilation.tap(
105
127
  "DllReferencePlugin",
106
128
  (compilation, params) => {
107
- let manifest = this.options.manifest;
108
- if (typeof manifest === "string") {
109
- // If there was an error parsing the manifest file, add the
110
- // error as a compilation error to make the compilation fail.
111
- let e = params["dll reference parse error " + manifest];
112
- if (e) {
113
- compilation.errors.push(e);
129
+ if ("manifest" in this.options) {
130
+ let manifest = this.options.manifest;
131
+ if (typeof manifest === "string") {
132
+ // If there was an error parsing the manifest file, add the
133
+ // error as a compilation error to make the compilation fail.
134
+ let e = params["dll reference parse error " + manifest];
135
+ if (e) {
136
+ compilation.errors.push(e);
137
+ }
114
138
  }
115
139
  }
116
140
  }
@@ -10,13 +10,14 @@ const MultiModuleFactory = require("./MultiModuleFactory");
10
10
  const MultiEntryPlugin = require("./MultiEntryPlugin");
11
11
  const SingleEntryPlugin = require("./SingleEntryPlugin");
12
12
 
13
+ /** @typedef {import("../declarations/WebpackOptions").EntryDynamic} EntryDynamic */
14
+ /** @typedef {import("../declarations/WebpackOptions").EntryStatic} EntryStatic */
13
15
  /** @typedef {import("./Compiler")} Compiler */
14
- /** @typedef {import("./Compiler").EntryOptionValuesFunction} EntryOptionValuesFunction */
15
16
 
16
17
  class DynamicEntryPlugin {
17
18
  /**
18
19
  * @param {string} context the context path
19
- * @param {EntryOptionValuesFunction} entry the entry value
20
+ * @param {EntryDynamic} entry the entry value
20
21
  */
21
22
  constructor(context, entry) {
22
23
  this.context = context;
@@ -50,7 +51,7 @@ class DynamicEntryPlugin {
50
51
  /**
51
52
  * @param {string|string[]} entry entry value or array of entry values
52
53
  * @param {string} name name of entry
53
- * @returns {Promise<any>} returns the promise resolving the Compilation#addEntry function
54
+ * @returns {Promise<EntryStatic>} returns the promise resolving the Compilation#addEntry function
54
55
  */
55
56
  const addEntry = (entry, name) => {
56
57
  const dep = DynamicEntryPlugin.createDependency(entry, name);
@@ -8,11 +8,12 @@ const SingleEntryPlugin = require("./SingleEntryPlugin");
8
8
  const MultiEntryPlugin = require("./MultiEntryPlugin");
9
9
  const DynamicEntryPlugin = require("./DynamicEntryPlugin");
10
10
 
11
+ /** @typedef {import("../declarations/WebpackOptions").EntryItem} EntryItem */
11
12
  /** @typedef {import("./Compiler")} Compiler */
12
13
 
13
14
  /**
14
15
  * @param {string} context context path
15
- * @param {string | string[]} item entry array or single path
16
+ * @param {EntryItem} item entry array or single path
16
17
  * @param {string} name entry key name
17
18
  * @returns {SingleEntryPlugin | MultiEntryPlugin} returns either a single or multi entry plugin
18
19
  */
@@ -74,7 +74,9 @@ class ExternalModule extends Module {
74
74
  .slice(1)
75
75
  .map(r => `[${JSON.stringify(r)}]`)
76
76
  .join("");
77
- return `module.exports = require(${moduleName})${objectLookup};`;
77
+ return `module.exports = require(${JSON.stringify(
78
+ moduleName
79
+ )})${objectLookup};`;
78
80
  }
79
81
 
80
82
  checkExternalVariable(variableToCheck, request) {
@@ -94,15 +96,25 @@ class ExternalModule extends Module {
94
96
  }
95
97
 
96
98
  getSourceForDefaultCase(optional, request) {
99
+ if (!Array.isArray(request)) {
100
+ // make it an array as the look up works the same basically
101
+ request = [request];
102
+ }
103
+
104
+ const variableName = request[0];
97
105
  const missingModuleError = optional
98
- ? this.checkExternalVariable(request, request)
106
+ ? this.checkExternalVariable(variableName, request.join("."))
99
107
  : "";
100
- return `${missingModuleError}module.exports = ${request};`;
108
+ const objectLookup = request
109
+ .slice(1)
110
+ .map(r => `[${JSON.stringify(r)}]`)
111
+ .join("");
112
+ return `${missingModuleError}module.exports = ${variableName}${objectLookup};`;
101
113
  }
102
114
 
103
115
  getSourceString(runtime) {
104
116
  const request =
105
- typeof this.request === "object"
117
+ typeof this.request === "object" && !Array.isArray(this.request)
106
118
  ? this.request[this.externalType]
107
119
  : this.request;
108
120
  switch (this.externalType) {
@@ -8,10 +8,18 @@ const createHash = require("./util/createHash");
8
8
  const validateOptions = require("schema-utils");
9
9
  const schema = require("../schemas/plugins/HashedModuleIdsPlugin.json");
10
10
 
11
+ /** @typedef {import("../declarations/plugins/HashedModuleIdsPlugin").HashedModuleIdsPluginOptions} HashedModuleIdsPluginOptions */
12
+
11
13
  class HashedModuleIdsPlugin {
14
+ /**
15
+ * @param {HashedModuleIdsPluginOptions=} options options object
16
+ */
12
17
  constructor(options) {
13
- validateOptions(schema, options || {}, "Hashed Module Ids Plugin");
18
+ if (!options) options = {};
19
+
20
+ validateOptions(schema, options, "Hashed Module Ids Plugin");
14
21
 
22
+ /** @type {HashedModuleIdsPluginOptions} */
15
23
  this.options = Object.assign(
16
24
  {
17
25
  context: null,
@@ -9,6 +9,7 @@ const Chunk = require("./Chunk");
9
9
  class HotUpdateChunk extends Chunk {
10
10
  constructor() {
11
11
  super();
12
+ /** @type {(string|number)[]} */
12
13
  this.removedModules = undefined;
13
14
  }
14
15
  }
@@ -7,15 +7,12 @@
7
7
  const validateOptions = require("schema-utils");
8
8
  const schema = require("../schemas/plugins/IgnorePlugin.json");
9
9
 
10
+ /** @typedef {import("../declarations/plugins/IgnorePlugin").IgnorePluginOptions} IgnorePluginOptions */
10
11
  /** @typedef {import("./Compiler")} Compiler */
11
12
 
12
13
  class IgnorePlugin {
13
14
  /**
14
- * @param {object} options IgnorePlugin options
15
- * @param {RegExp} options.resourceRegExp - A RegExp to test the request against
16
- * @param {RegExp} options.contextRegExp - A RegExp to test the context (directory) against
17
- * @param {function(string): boolean=} options.checkResource - A filter function for resource
18
- * @param {function(string): boolean=} options.checkContext - A filter function for context
15
+ * @param {IgnorePluginOptions} options IgnorePlugin options
19
16
  */
20
17
  constructor(options) {
21
18
  // TODO webpack 5 remove this compat-layer
@@ -39,13 +36,13 @@ class IgnorePlugin {
39
36
  * and the resource given matches the regexp.
40
37
  */
41
38
  checkResource(resource) {
42
- if (this.options.checkResource) {
39
+ if ("checkResource" in this.options && this.options.checkResource) {
43
40
  return this.options.checkResource(resource);
44
41
  }
45
- if (!this.options.resourceRegExp) {
46
- return false;
42
+ if ("resourceRegExp" in this.options && this.options.resourceRegExp) {
43
+ return this.options.resourceRegExp.test(resource);
47
44
  }
48
- return this.options.resourceRegExp.test(resource);
45
+ return false;
49
46
  }
50
47
 
51
48
  /**
@@ -54,14 +51,13 @@ class IgnorePlugin {
54
51
  * or if context matches the given regexp.
55
52
  */
56
53
  checkContext(context) {
57
- if (this.options.checkContext) {
54
+ if ("checkContext" in this.options && this.options.checkContext) {
58
55
  return this.options.checkContext(context);
59
56
  }
60
-
61
- if (!this.options.contextRegExp) {
62
- return true;
57
+ if ("contextRegExp" in this.options && this.options.contextRegExp) {
58
+ return this.options.contextRegExp.test(context);
63
59
  }
64
- return this.options.contextRegExp.test(context);
60
+ return true;
65
61
  }
66
62
 
67
63
  /**
@@ -6,6 +6,7 @@
6
6
 
7
7
  const SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
8
8
 
9
+ /** @typedef {import("../declarations/WebpackOptions").LibraryCustomUmdObject} LibraryCustomUmdObject */
9
10
  /** @typedef {import("./Compiler")} Compiler */
10
11
 
11
12
  /**
@@ -18,12 +19,17 @@ const accessorToObjectAccess = accessor => {
18
19
 
19
20
  /**
20
21
  * @param {string=} base the path prefix
21
- * @param {string|string[]} accessor the accessor
22
+ * @param {string|string[]|LibraryCustomUmdObject} accessor the accessor
23
+ * @param {"amd" | "commonjs" | "root"} umdProperty property used when a custom umd object is provided
22
24
  * @param {string=} joinWith the element separator
23
25
  * @returns {string} the path
24
26
  */
25
- const accessorAccess = (base, accessor, joinWith = "; ") => {
26
- const accessors = Array.isArray(accessor) ? accessor : [accessor];
27
+ const accessorAccess = (base, accessor, umdProperty, joinWith = "; ") => {
28
+ const normalizedAccessor =
29
+ typeof accessor === "object" ? accessor[umdProperty] : accessor;
30
+ const accessors = Array.isArray(normalizedAccessor)
31
+ ? normalizedAccessor
32
+ : [normalizedAccessor];
27
33
  return accessors
28
34
  .map((_, idx) => {
29
35
  const a = base
@@ -40,7 +46,7 @@ const accessorAccess = (base, accessor, joinWith = "; ") => {
40
46
 
41
47
  class LibraryTemplatePlugin {
42
48
  /**
43
- * @param {string} name name of library
49
+ * @param {string|string[]|LibraryCustomUmdObject} name name of library
44
50
  * @param {string} target type of library
45
51
  * @param {boolean} umdNamedDefine setting this to true will name the UMD module
46
52
  * @param {string|TODO} auxiliaryComment comment in the UMD wrapper
@@ -68,14 +74,22 @@ class LibraryTemplatePlugin {
68
74
  }
69
75
  switch (this.target) {
70
76
  case "var":
77
+ if (
78
+ !this.name ||
79
+ (typeof this.name === "object" && !Array.isArray(this.name))
80
+ ) {
81
+ throw new Error(
82
+ "library name must be set and not an UMD custom object for non-UMD target"
83
+ );
84
+ }
71
85
  new SetVarMainTemplatePlugin(
72
- `var ${accessorAccess(undefined, this.name)}`,
86
+ `var ${accessorAccess(undefined, this.name, "root")}`,
73
87
  false
74
88
  ).apply(compilation);
75
89
  break;
76
90
  case "assign":
77
91
  new SetVarMainTemplatePlugin(
78
- accessorAccess(undefined, this.name),
92
+ accessorAccess(undefined, this.name, "root"),
79
93
  false
80
94
  ).apply(compilation);
81
95
  break;
@@ -84,7 +98,7 @@ class LibraryTemplatePlugin {
84
98
  case "window":
85
99
  if (this.name) {
86
100
  new SetVarMainTemplatePlugin(
87
- accessorAccess(this.target, this.name),
101
+ accessorAccess(this.target, this.name, "root"),
88
102
  false
89
103
  ).apply(compilation);
90
104
  } else {
@@ -96,7 +110,8 @@ class LibraryTemplatePlugin {
96
110
  new SetVarMainTemplatePlugin(
97
111
  accessorAccess(
98
112
  compilation.runtimeTemplate.outputOptions.globalObject,
99
- this.name
113
+ this.name,
114
+ "root"
100
115
  ),
101
116
  false
102
117
  ).apply(compilation);
@@ -110,7 +125,7 @@ class LibraryTemplatePlugin {
110
125
  case "commonjs":
111
126
  if (this.name) {
112
127
  new SetVarMainTemplatePlugin(
113
- accessorAccess("exports", this.name),
128
+ accessorAccess("exports", this.name, "commonjs"),
114
129
  false
115
130
  ).apply(compilation);
116
131
  } else {
@@ -125,7 +140,13 @@ class LibraryTemplatePlugin {
125
140
  break;
126
141
  case "amd": {
127
142
  const AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
128
- new AmdMainTemplatePlugin(this.name).apply(compilation);
143
+ if (this.name) {
144
+ if (typeof this.name !== "string")
145
+ throw new Error("library name must be a string for amd target");
146
+ new AmdMainTemplatePlugin(this.name).apply(compilation);
147
+ } else {
148
+ new AmdMainTemplatePlugin().apply(compilation);
149
+ }
129
150
  break;
130
151
  }
131
152
  case "umd":
@@ -140,6 +161,8 @@ class LibraryTemplatePlugin {
140
161
  }
141
162
  case "jsonp": {
142
163
  const JsonpExportMainTemplatePlugin = require("./web/JsonpExportMainTemplatePlugin");
164
+ if (typeof this.name !== "string")
165
+ throw new Error("library name must be a string for jsonp target");
143
166
  new JsonpExportMainTemplatePlugin(this.name).apply(compilation);
144
167
  break;
145
168
  }
@@ -9,7 +9,12 @@ const ModuleFilenameHelpers = require("./ModuleFilenameHelpers");
9
9
  const validateOptions = require("schema-utils");
10
10
  const schema = require("../schemas/plugins/LoaderOptionsPlugin.json");
11
11
 
12
+ /** @typedef {import("../declarations/plugins/LoaderOptionsPlugin").LoaderOptionsPluginOptions} LoaderOptionsPluginOptions */
13
+
12
14
  class LoaderOptionsPlugin {
15
+ /**
16
+ * @param {LoaderOptionsPluginOptions} options options object
17
+ */
13
18
  constructor(options) {
14
19
  validateOptions(schema, options || {}, "Loader Options Plugin");
15
20