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
@@ -1,606 +1,620 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- "use strict";
6
- const HarmonyImportDependency = require("./HarmonyImportDependency");
7
- const Template = require("../Template");
8
-
9
- class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
10
- constructor(
11
- request,
12
- originModule,
13
- sourceOrder,
14
- parserScope,
15
- id,
16
- name,
17
- activeExports,
18
- otherStarExports,
19
- strictExportPresence
20
- ) {
21
- super(request, originModule, sourceOrder, parserScope);
22
- this.id = id;
23
- this.name = name;
24
- this.activeExports = activeExports;
25
- this.otherStarExports = otherStarExports;
26
- this.strictExportPresence = strictExportPresence;
27
- }
28
-
29
- get type() {
30
- return "harmony export imported specifier";
31
- }
32
-
33
- getMode(ignoreUnused) {
34
- const name = this.name;
35
- const id = this.id;
36
- const used = this.originModule.isUsed(name);
37
- const importedModule = this.module;
38
-
39
- if (!importedModule) {
40
- return {
41
- type: "missing",
42
- userRequest: this.userRequest
43
- };
44
- }
45
-
46
- if (
47
- !ignoreUnused &&
48
- (name ? !used : this.originModule.usedExports === false)
49
- ) {
50
- return {
51
- type: "unused",
52
- name: name || "*"
53
- };
54
- }
55
-
56
- const isNotAHarmonyModule =
57
- importedModule.buildMeta && !importedModule.buildMeta.exportsType;
58
- const strictHarmonyModule = this.originModule.buildMeta.strictHarmonyModule;
59
- if (name && id === "default" && isNotAHarmonyModule) {
60
- if (strictHarmonyModule) {
61
- return {
62
- type: "reexport-non-harmony-default-strict",
63
- module: importedModule,
64
- name
65
- };
66
- } else {
67
- return {
68
- type: "reexport-non-harmony-default",
69
- module: importedModule,
70
- name
71
- };
72
- }
73
- }
74
-
75
- if (name) {
76
- // export { name as name }
77
- if (id) {
78
- if (isNotAHarmonyModule && strictHarmonyModule) {
79
- return {
80
- type: "rexport-non-harmony-undefined",
81
- module: importedModule,
82
- name
83
- };
84
- } else {
85
- return {
86
- type: "safe-reexport",
87
- module: importedModule,
88
- map: new Map([[name, id]])
89
- };
90
- }
91
- }
92
-
93
- // export { * as name }
94
- if (isNotAHarmonyModule && strictHarmonyModule) {
95
- return {
96
- type: "reexport-fake-namespace-object",
97
- module: importedModule,
98
- name
99
- };
100
- } else {
101
- return {
102
- type: "reexport-namespace-object",
103
- module: importedModule,
104
- name
105
- };
106
- }
107
- }
108
-
109
- const hasUsedExports = Array.isArray(this.originModule.usedExports);
110
- const hasProvidedExports = Array.isArray(
111
- importedModule.buildMeta.providedExports
112
- );
113
- const activeFromOtherStarExports = this._discoverActiveExportsFromOtherStartExports();
114
-
115
- // export *
116
- if (hasUsedExports) {
117
- // reexport * with known used exports
118
- if (hasProvidedExports) {
119
- const map = new Map(
120
- this.originModule.usedExports
121
- .filter(id => {
122
- if (id === "default") return false;
123
- if (this.activeExports.has(id)) return false;
124
- if (activeFromOtherStarExports.has(id)) return false;
125
- if (!importedModule.buildMeta.providedExports.includes(id))
126
- return false;
127
- return true;
128
- })
129
- .map(item => [item, item])
130
- );
131
-
132
- if (map.size === 0) {
133
- return {
134
- type: "empty-star"
135
- };
136
- }
137
-
138
- return {
139
- type: "safe-reexport",
140
- module: importedModule,
141
- map
142
- };
143
- }
144
-
145
- const map = new Map(
146
- this.originModule.usedExports
147
- .filter(id => {
148
- if (id === "default") return false;
149
- if (this.activeExports.has(id)) return false;
150
- if (activeFromOtherStarExports.has(id)) return false;
151
-
152
- return true;
153
- })
154
- .map(item => [item, item])
155
- );
156
-
157
- if (map.size === 0) {
158
- return {
159
- type: "empty-star"
160
- };
161
- }
162
-
163
- return {
164
- type: "checked-reexport",
165
- module: importedModule,
166
- map
167
- };
168
- }
169
-
170
- if (hasProvidedExports) {
171
- const map = new Map(
172
- importedModule.buildMeta.providedExports
173
- .filter(id => {
174
- if (id === "default") return false;
175
- if (this.activeExports.has(id)) return false;
176
- if (activeFromOtherStarExports.has(id)) return false;
177
-
178
- return true;
179
- })
180
- .map(item => [item, item])
181
- );
182
-
183
- if (map.size === 0) {
184
- return {
185
- type: "empty-star"
186
- };
187
- }
188
-
189
- return {
190
- type: "safe-reexport",
191
- module: importedModule,
192
- map
193
- };
194
- }
195
-
196
- return {
197
- type: "dynamic-reexport",
198
- module: importedModule
199
- };
200
- }
201
-
202
- getReference() {
203
- const mode = this.getMode();
204
-
205
- switch (mode.type) {
206
- case "missing":
207
- case "unused":
208
- case "empty-star":
209
- return null;
210
-
211
- case "reexport-non-harmony-default":
212
- return {
213
- module: mode.module,
214
- importedNames: ["default"]
215
- };
216
-
217
- case "reexport-namespace-object":
218
- case "reexport-non-harmony-default-strict":
219
- case "reexport-fake-namespace-object":
220
- case "rexport-non-harmony-undefined":
221
- return {
222
- module: mode.module,
223
- importedNames: true
224
- };
225
-
226
- case "safe-reexport":
227
- case "checked-reexport":
228
- return {
229
- module: mode.module,
230
- importedNames: Array.from(mode.map.values())
231
- };
232
-
233
- case "dynamic-reexport":
234
- return {
235
- module: mode.module,
236
- importedNames: true
237
- };
238
-
239
- default:
240
- throw new Error(`Unknown mode ${mode.type}`);
241
- }
242
- }
243
-
244
- _discoverActiveExportsFromOtherStartExports() {
245
- if (!this.otherStarExports) return new Set();
246
- const result = new Set();
247
- // try to learn impossible exports from other star exports with provided exports
248
- for (const otherStarExport of this.otherStarExports) {
249
- const otherImportedModule = otherStarExport.module;
250
- if (
251
- otherImportedModule &&
252
- Array.isArray(otherImportedModule.buildMeta.providedExports)
253
- ) {
254
- for (const exportName of otherImportedModule.buildMeta.providedExports)
255
- result.add(exportName);
256
- }
257
- }
258
- return result;
259
- }
260
-
261
- getExports() {
262
- if (this.name) {
263
- return {
264
- exports: [this.name]
265
- };
266
- }
267
-
268
- const importedModule = this.module;
269
-
270
- if (!importedModule) {
271
- // no imported module available
272
- return {
273
- exports: null
274
- };
275
- }
276
-
277
- if (Array.isArray(importedModule.buildMeta.providedExports)) {
278
- return {
279
- exports: importedModule.buildMeta.providedExports.filter(
280
- id => id !== "default"
281
- ),
282
- dependencies: [importedModule]
283
- };
284
- }
285
-
286
- if (importedModule.buildMeta.providedExports) {
287
- return {
288
- exports: true
289
- };
290
- }
291
-
292
- return {
293
- exports: null,
294
- dependencies: [importedModule]
295
- };
296
- }
297
-
298
- getWarnings() {
299
- if (
300
- this.strictExportPresence ||
301
- this.originModule.buildMeta.strictHarmonyModule
302
- ) {
303
- return [];
304
- }
305
- return this._getErrors();
306
- }
307
-
308
- getErrors() {
309
- if (
310
- this.strictExportPresence ||
311
- this.originModule.buildMeta.strictHarmonyModule
312
- ) {
313
- return this._getErrors();
314
- }
315
- return [];
316
- }
317
-
318
- _getErrors() {
319
- const importedModule = this.module;
320
- if (!importedModule) {
321
- return;
322
- }
323
-
324
- if (!importedModule.buildMeta || !importedModule.buildMeta.exportsType) {
325
- // It's not an harmony module
326
- if (
327
- this.originModule.buildMeta.strictHarmonyModule &&
328
- this.id !== "default"
329
- ) {
330
- // In strict harmony modules we only support the default export
331
- const exportName = this.id
332
- ? `the named export '${this.id}'`
333
- : "the namespace object";
334
- const err = new Error(
335
- `Can't reexport ${
336
- exportName
337
- } from non EcmaScript module (only default export is available)`
338
- );
339
- err.hideStack = true;
340
- return [err];
341
- }
342
- return;
343
- }
344
-
345
- if (!this.id) {
346
- return;
347
- }
348
-
349
- if (importedModule.isProvided(this.id) !== false) {
350
- // It's provided or we are not sure
351
- return;
352
- }
353
-
354
- // We are sure that it's not provided
355
- const idIsNotNameMessage =
356
- this.id !== this.name ? ` (reexported as '${this.name}')` : "";
357
- const errorMessage = `"export '${this.id}'${
358
- idIsNotNameMessage
359
- } was not found in '${this.userRequest}'`;
360
- const err = new Error(errorMessage);
361
- err.hideStack = true;
362
- return [err];
363
- }
364
-
365
- updateHash(hash) {
366
- super.updateHash(hash);
367
- const hashValue = this.getHashValue(this.module);
368
- hash.update(hashValue);
369
- }
370
-
371
- getHashValue(importedModule) {
372
- if (!importedModule) {
373
- return "";
374
- }
375
-
376
- const stringifiedUsedExport = JSON.stringify(importedModule.usedExports);
377
- const stringifiedProvidedExport = JSON.stringify(
378
- importedModule.buildMeta.providedExports
379
- );
380
- return (
381
- importedModule.used + stringifiedUsedExport + stringifiedProvidedExport
382
- );
383
- }
384
- }
385
-
386
- module.exports = HarmonyExportImportedSpecifierDependency;
387
-
388
- HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedSpecifierDependencyTemplate extends HarmonyImportDependency.Template {
389
- harmonyInit(dep, source, runtime, dependencyTemplates) {
390
- super.harmonyInit(dep, source, runtime, dependencyTemplates);
391
- const content = this.getContent(dep);
392
- source.insert(-1, content);
393
- }
394
-
395
- getHarmonyInitOrder(dep) {
396
- if (dep.name) {
397
- const used = dep.originModule.isUsed(dep.name);
398
- if (!used) return NaN;
399
- } else {
400
- const importedModule = dep.module;
401
-
402
- const activeFromOtherStarExports = dep._discoverActiveExportsFromOtherStartExports();
403
-
404
- if (Array.isArray(dep.originModule.usedExports)) {
405
- // we know which exports are used
406
-
407
- const unused = dep.originModule.usedExports.every(id => {
408
- if (id === "default") return true;
409
- if (dep.activeExports.has(id)) return true;
410
- if (importedModule.isProvided(id) === false) return true;
411
- if (activeFromOtherStarExports.has(id)) return true;
412
- return false;
413
- });
414
- if (unused) return NaN;
415
- } else if (
416
- dep.originModule.usedExports &&
417
- importedModule &&
418
- Array.isArray(importedModule.buildMeta.providedExports)
419
- ) {
420
- // not sure which exports are used, but we know which are provided
421
-
422
- const unused = importedModule.buildMeta.providedExports.every(id => {
423
- if (id === "default") return true;
424
- if (dep.activeExports.has(id)) return true;
425
- if (activeFromOtherStarExports.has(id)) return true;
426
- return false;
427
- });
428
- if (unused) return NaN;
429
- }
430
- }
431
- return super.getHarmonyInitOrder(dep);
432
- }
433
-
434
- getContent(dep) {
435
- const mode = dep.getMode();
436
- const module = dep.originModule;
437
- const importedModule = dep.module;
438
- const importVar = dep.getImportVar();
439
-
440
- switch (mode.type) {
441
- case "missing":
442
- return `throw new Error(${JSON.stringify(
443
- `Cannot find module '${mode.userRequest}'`
444
- )});\n`;
445
-
446
- case "unused":
447
- return `${Template.toNormalComment(
448
- `unused harmony reexport ${mode.name}`
449
- )}\n`;
450
-
451
- case "reexport-non-harmony-default":
452
- return (
453
- "/* harmony reexport (default from non-harmony) */ " +
454
- this.getReexportStatement(
455
- module,
456
- module.isUsed(mode.name),
457
- importVar,
458
- null
459
- )
460
- );
461
-
462
- case "reexport-fake-namespace-object":
463
- return (
464
- "/* harmony reexport (fake namespace object from non-harmony) */ " +
465
- this.getReexportFakeNamespaceObjectStatement(
466
- module,
467
- module.isUsed(mode.name),
468
- importVar
469
- )
470
- );
471
-
472
- case "rexport-non-harmony-undefined":
473
- return (
474
- "/* harmony reexport (non default export from non-harmony) */ " +
475
- this.getReexportStatement(
476
- module,
477
- module.isUsed(mode.name),
478
- "undefined",
479
- ""
480
- )
481
- );
482
-
483
- case "reexport-non-harmony-default-strict":
484
- return (
485
- "/* harmony reexport (default from non-harmony) */ " +
486
- this.getReexportStatement(
487
- module,
488
- module.isUsed(mode.name),
489
- importVar,
490
- ""
491
- )
492
- );
493
-
494
- case "reexport-namespace-object":
495
- return (
496
- "/* harmony reexport (module object) */ " +
497
- this.getReexportStatement(
498
- module,
499
- module.isUsed(mode.name),
500
- importVar,
501
- ""
502
- )
503
- );
504
-
505
- case "empty-star":
506
- return "/* empty/unused harmony star reexport */";
507
-
508
- case "safe-reexport":
509
- return Array.from(mode.map.entries())
510
- .map(item => {
511
- return (
512
- "/* harmony reexport (safe) */ " +
513
- this.getReexportStatement(
514
- module,
515
- module.isUsed(item[0]),
516
- importVar,
517
- importedModule.isUsed(item[1])
518
- ) +
519
- "\n"
520
- );
521
- })
522
- .join("");
523
-
524
- case "checked-reexport":
525
- return Array.from(mode.map.entries())
526
- .map(item => {
527
- return (
528
- "/* harmony reexport (checked) */ " +
529
- this.getConditionalReexportStatement(
530
- module,
531
- item[0],
532
- importVar,
533
- item[1]
534
- ) +
535
- "\n"
536
- );
537
- })
538
- .join("");
539
-
540
- case "dynamic-reexport": {
541
- const activeExports = new Set([
542
- ...dep.activeExports,
543
- ...dep._discoverActiveExportsFromOtherStartExports()
544
- ]);
545
- let content =
546
- "/* harmony reexport (unknown) */ for(var __WEBPACK_IMPORT_KEY__ in " +
547
- importVar +
548
- ") ";
549
-
550
- // Filter out exports which are defined by other exports
551
- // and filter out default export because it cannot be reexported with *
552
- if (activeExports.length > 0)
553
- content +=
554
- "if(" +
555
- JSON.stringify(activeExports.concat("default")) +
556
- ".indexOf(__WEBPACK_IMPORT_KEY__) < 0) ";
557
- else content += "if(__WEBPACK_IMPORT_KEY__ !== 'default') ";
558
- const exportsName = dep.originModule.exportsArgument;
559
- return (
560
- content +
561
- `(function(key) { __webpack_require__.d(${
562
- exportsName
563
- }, key, function() { return ${
564
- importVar
565
- }[key]; }) }(__WEBPACK_IMPORT_KEY__));\n`
566
- );
567
- }
568
-
569
- default:
570
- throw new Error(`Unknown mode ${mode.type}`);
571
- }
572
- }
573
-
574
- getReexportStatement(module, key, name, valueKey) {
575
- const exportsName = module.exportsArgument;
576
- const returnValue = this.getReturnValue(valueKey);
577
- return `__webpack_require__.d(${exportsName}, ${JSON.stringify(
578
- key
579
- )}, function() { return ${name}${returnValue}; });\n`;
580
- }
581
-
582
- getReexportFakeNamespaceObjectStatement(module, key, name) {
583
- const exportsName = module.exportsArgument;
584
- return `__webpack_require__.d(${exportsName}, ${JSON.stringify(
585
- key
586
- )}, function() { return { "default": ${name} }; });\n`;
587
- }
588
-
589
- getConditionalReexportStatement(module, key, name, valueKey) {
590
- const exportsName = module.exportsArgument;
591
- const returnValue = this.getReturnValue(valueKey);
592
- return `if(__webpack_require__.o(${name}, ${JSON.stringify(
593
- valueKey
594
- )})) __webpack_require__.d(${exportsName}, ${JSON.stringify(
595
- key
596
- )}, function() { return ${name}${returnValue}; });\n`;
597
- }
598
-
599
- getReturnValue(valueKey) {
600
- if (valueKey === null) {
601
- return "_default.a";
602
- }
603
-
604
- return valueKey && "[" + JSON.stringify(valueKey) + "]";
605
- }
606
- };
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+ "use strict";
6
+ const HarmonyImportDependency = require("./HarmonyImportDependency");
7
+ const Template = require("../Template");
8
+
9
+ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
10
+ constructor(
11
+ request,
12
+ originModule,
13
+ sourceOrder,
14
+ parserScope,
15
+ id,
16
+ name,
17
+ activeExports,
18
+ otherStarExports,
19
+ strictExportPresence
20
+ ) {
21
+ super(request, originModule, sourceOrder, parserScope);
22
+ this.id = id;
23
+ this.name = name;
24
+ this.activeExports = activeExports;
25
+ this.otherStarExports = otherStarExports;
26
+ this.strictExportPresence = strictExportPresence;
27
+ }
28
+
29
+ get type() {
30
+ return "harmony export imported specifier";
31
+ }
32
+
33
+ getMode(ignoreUnused) {
34
+ const name = this.name;
35
+ const id = this.id;
36
+ const used = this.originModule.isUsed(name);
37
+ const importedModule = this.module;
38
+
39
+ if (!importedModule) {
40
+ return {
41
+ type: "missing",
42
+ userRequest: this.userRequest
43
+ };
44
+ }
45
+
46
+ if (
47
+ !ignoreUnused &&
48
+ (name ? !used : this.originModule.usedExports === false)
49
+ ) {
50
+ return {
51
+ type: "unused",
52
+ name: name || "*"
53
+ };
54
+ }
55
+
56
+ const strictHarmonyModule = this.originModule.buildMeta.strictHarmonyModule;
57
+ if (name && id === "default" && importedModule.buildMeta) {
58
+ if (!importedModule.buildMeta.exportsType) {
59
+ if (strictHarmonyModule) {
60
+ return {
61
+ type: "reexport-non-harmony-default-strict",
62
+ module: importedModule,
63
+ name
64
+ };
65
+ } else {
66
+ return {
67
+ type: "reexport-non-harmony-default",
68
+ module: importedModule,
69
+ name
70
+ };
71
+ }
72
+ } else if (importedModule.buildMeta.exportsType === "named") {
73
+ return {
74
+ type: "reexport-named-default",
75
+ module: importedModule,
76
+ name
77
+ };
78
+ }
79
+ }
80
+
81
+ const isNotAHarmonyModule =
82
+ importedModule.buildMeta && !importedModule.buildMeta.exportsType;
83
+ if (name) {
84
+ // export { name as name }
85
+ if (id) {
86
+ if (isNotAHarmonyModule && strictHarmonyModule) {
87
+ return {
88
+ type: "rexport-non-harmony-undefined",
89
+ module: importedModule,
90
+ name
91
+ };
92
+ } else {
93
+ return {
94
+ type: "safe-reexport",
95
+ module: importedModule,
96
+ map: new Map([[name, id]])
97
+ };
98
+ }
99
+ }
100
+
101
+ // export { * as name }
102
+ if (isNotAHarmonyModule && strictHarmonyModule) {
103
+ return {
104
+ type: "reexport-fake-namespace-object",
105
+ module: importedModule,
106
+ name
107
+ };
108
+ } else {
109
+ return {
110
+ type: "reexport-namespace-object",
111
+ module: importedModule,
112
+ name
113
+ };
114
+ }
115
+ }
116
+
117
+ const hasUsedExports = Array.isArray(this.originModule.usedExports);
118
+ const hasProvidedExports = Array.isArray(
119
+ importedModule.buildMeta.providedExports
120
+ );
121
+ const activeFromOtherStarExports = this._discoverActiveExportsFromOtherStartExports();
122
+
123
+ // export *
124
+ if (hasUsedExports) {
125
+ // reexport * with known used exports
126
+ if (hasProvidedExports) {
127
+ const map = new Map(
128
+ this.originModule.usedExports
129
+ .filter(id => {
130
+ if (id === "default") return false;
131
+ if (this.activeExports.has(id)) return false;
132
+ if (activeFromOtherStarExports.has(id)) return false;
133
+ if (!importedModule.buildMeta.providedExports.includes(id))
134
+ return false;
135
+ return true;
136
+ })
137
+ .map(item => [item, item])
138
+ );
139
+
140
+ if (map.size === 0) {
141
+ return {
142
+ type: "empty-star"
143
+ };
144
+ }
145
+
146
+ return {
147
+ type: "safe-reexport",
148
+ module: importedModule,
149
+ map
150
+ };
151
+ }
152
+
153
+ const map = new Map(
154
+ this.originModule.usedExports
155
+ .filter(id => {
156
+ if (id === "default") return false;
157
+ if (this.activeExports.has(id)) return false;
158
+ if (activeFromOtherStarExports.has(id)) return false;
159
+
160
+ return true;
161
+ })
162
+ .map(item => [item, item])
163
+ );
164
+
165
+ if (map.size === 0) {
166
+ return {
167
+ type: "empty-star"
168
+ };
169
+ }
170
+
171
+ return {
172
+ type: "checked-reexport",
173
+ module: importedModule,
174
+ map
175
+ };
176
+ }
177
+
178
+ if (hasProvidedExports) {
179
+ const map = new Map(
180
+ importedModule.buildMeta.providedExports
181
+ .filter(id => {
182
+ if (id === "default") return false;
183
+ if (this.activeExports.has(id)) return false;
184
+ if (activeFromOtherStarExports.has(id)) return false;
185
+
186
+ return true;
187
+ })
188
+ .map(item => [item, item])
189
+ );
190
+
191
+ if (map.size === 0) {
192
+ return {
193
+ type: "empty-star"
194
+ };
195
+ }
196
+
197
+ return {
198
+ type: "safe-reexport",
199
+ module: importedModule,
200
+ map
201
+ };
202
+ }
203
+
204
+ return {
205
+ type: "dynamic-reexport",
206
+ module: importedModule
207
+ };
208
+ }
209
+
210
+ getReference() {
211
+ const mode = this.getMode();
212
+
213
+ switch (mode.type) {
214
+ case "missing":
215
+ case "unused":
216
+ case "empty-star":
217
+ return null;
218
+
219
+ case "reexport-non-harmony-default":
220
+ case "reexport-named-default":
221
+ return {
222
+ module: mode.module,
223
+ importedNames: ["default"]
224
+ };
225
+
226
+ case "reexport-namespace-object":
227
+ case "reexport-non-harmony-default-strict":
228
+ case "reexport-fake-namespace-object":
229
+ case "rexport-non-harmony-undefined":
230
+ return {
231
+ module: mode.module,
232
+ importedNames: true
233
+ };
234
+
235
+ case "safe-reexport":
236
+ case "checked-reexport":
237
+ return {
238
+ module: mode.module,
239
+ importedNames: Array.from(mode.map.values())
240
+ };
241
+
242
+ case "dynamic-reexport":
243
+ return {
244
+ module: mode.module,
245
+ importedNames: true
246
+ };
247
+
248
+ default:
249
+ throw new Error(`Unknown mode ${mode.type}`);
250
+ }
251
+ }
252
+
253
+ _discoverActiveExportsFromOtherStartExports() {
254
+ if (!this.otherStarExports) return new Set();
255
+ const result = new Set();
256
+ // try to learn impossible exports from other star exports with provided exports
257
+ for (const otherStarExport of this.otherStarExports) {
258
+ const otherImportedModule = otherStarExport.module;
259
+ if (
260
+ otherImportedModule &&
261
+ Array.isArray(otherImportedModule.buildMeta.providedExports)
262
+ ) {
263
+ for (const exportName of otherImportedModule.buildMeta.providedExports)
264
+ result.add(exportName);
265
+ }
266
+ }
267
+ return result;
268
+ }
269
+
270
+ getExports() {
271
+ if (this.name) {
272
+ return {
273
+ exports: [this.name]
274
+ };
275
+ }
276
+
277
+ const importedModule = this.module;
278
+
279
+ if (!importedModule) {
280
+ // no imported module available
281
+ return {
282
+ exports: null
283
+ };
284
+ }
285
+
286
+ if (Array.isArray(importedModule.buildMeta.providedExports)) {
287
+ return {
288
+ exports: importedModule.buildMeta.providedExports.filter(
289
+ id => id !== "default"
290
+ ),
291
+ dependencies: [importedModule]
292
+ };
293
+ }
294
+
295
+ if (importedModule.buildMeta.providedExports) {
296
+ return {
297
+ exports: true
298
+ };
299
+ }
300
+
301
+ return {
302
+ exports: null,
303
+ dependencies: [importedModule]
304
+ };
305
+ }
306
+
307
+ getWarnings() {
308
+ if (
309
+ this.strictExportPresence ||
310
+ this.originModule.buildMeta.strictHarmonyModule
311
+ ) {
312
+ return [];
313
+ }
314
+ return this._getErrors();
315
+ }
316
+
317
+ getErrors() {
318
+ if (
319
+ this.strictExportPresence ||
320
+ this.originModule.buildMeta.strictHarmonyModule
321
+ ) {
322
+ return this._getErrors();
323
+ }
324
+ return [];
325
+ }
326
+
327
+ _getErrors() {
328
+ const importedModule = this.module;
329
+ if (!importedModule) {
330
+ return;
331
+ }
332
+
333
+ if (!importedModule.buildMeta || !importedModule.buildMeta.exportsType) {
334
+ // It's not an harmony module
335
+ if (
336
+ this.originModule.buildMeta.strictHarmonyModule &&
337
+ this.id !== "default"
338
+ ) {
339
+ // In strict harmony modules we only support the default export
340
+ const exportName = this.id
341
+ ? `the named export '${this.id}'`
342
+ : "the namespace object";
343
+ const err = new Error(
344
+ `Can't reexport ${exportName} from non EcmaScript module (only default export is available)`
345
+ );
346
+ err.hideStack = true;
347
+ return [err];
348
+ }
349
+ return;
350
+ }
351
+
352
+ if (!this.id) {
353
+ return;
354
+ }
355
+
356
+ if (importedModule.isProvided(this.id) !== false) {
357
+ // It's provided or we are not sure
358
+ return;
359
+ }
360
+
361
+ // We are sure that it's not provided
362
+ const idIsNotNameMessage =
363
+ this.id !== this.name ? ` (reexported as '${this.name}')` : "";
364
+ const errorMessage = `"export '${
365
+ this.id
366
+ }'${idIsNotNameMessage} was not found in '${this.userRequest}'`;
367
+ const err = new Error(errorMessage);
368
+ err.hideStack = true;
369
+ return [err];
370
+ }
371
+
372
+ updateHash(hash) {
373
+ super.updateHash(hash);
374
+ const hashValue = this.getHashValue(this.module);
375
+ hash.update(hashValue);
376
+ }
377
+
378
+ getHashValue(importedModule) {
379
+ if (!importedModule) {
380
+ return "";
381
+ }
382
+
383
+ const stringifiedUsedExport = JSON.stringify(importedModule.usedExports);
384
+ const stringifiedProvidedExport = JSON.stringify(
385
+ importedModule.buildMeta.providedExports
386
+ );
387
+ return (
388
+ importedModule.used + stringifiedUsedExport + stringifiedProvidedExport
389
+ );
390
+ }
391
+ }
392
+
393
+ module.exports = HarmonyExportImportedSpecifierDependency;
394
+
395
+ HarmonyExportImportedSpecifierDependency.Template = class HarmonyExportImportedSpecifierDependencyTemplate extends HarmonyImportDependency.Template {
396
+ harmonyInit(dep, source, runtime, dependencyTemplates) {
397
+ super.harmonyInit(dep, source, runtime, dependencyTemplates);
398
+ const content = this.getContent(dep);
399
+ source.insert(-1, content);
400
+ }
401
+
402
+ getHarmonyInitOrder(dep) {
403
+ if (dep.name) {
404
+ const used = dep.originModule.isUsed(dep.name);
405
+ if (!used) return NaN;
406
+ } else {
407
+ const importedModule = dep.module;
408
+
409
+ const activeFromOtherStarExports = dep._discoverActiveExportsFromOtherStartExports();
410
+
411
+ if (Array.isArray(dep.originModule.usedExports)) {
412
+ // we know which exports are used
413
+
414
+ const unused = dep.originModule.usedExports.every(id => {
415
+ if (id === "default") return true;
416
+ if (dep.activeExports.has(id)) return true;
417
+ if (importedModule.isProvided(id) === false) return true;
418
+ if (activeFromOtherStarExports.has(id)) return true;
419
+ return false;
420
+ });
421
+ if (unused) return NaN;
422
+ } else if (
423
+ dep.originModule.usedExports &&
424
+ importedModule &&
425
+ Array.isArray(importedModule.buildMeta.providedExports)
426
+ ) {
427
+ // not sure which exports are used, but we know which are provided
428
+
429
+ const unused = importedModule.buildMeta.providedExports.every(id => {
430
+ if (id === "default") return true;
431
+ if (dep.activeExports.has(id)) return true;
432
+ if (activeFromOtherStarExports.has(id)) return true;
433
+ return false;
434
+ });
435
+ if (unused) return NaN;
436
+ }
437
+ }
438
+ return super.getHarmonyInitOrder(dep);
439
+ }
440
+
441
+ getContent(dep) {
442
+ const mode = dep.getMode();
443
+ const module = dep.originModule;
444
+ const importedModule = dep.module;
445
+ const importVar = dep.getImportVar();
446
+
447
+ switch (mode.type) {
448
+ case "missing":
449
+ return `throw new Error(${JSON.stringify(
450
+ `Cannot find module '${mode.userRequest}'`
451
+ )});\n`;
452
+
453
+ case "unused":
454
+ return `${Template.toNormalComment(
455
+ `unused harmony reexport ${mode.name}`
456
+ )}\n`;
457
+
458
+ case "reexport-non-harmony-default":
459
+ return (
460
+ "/* harmony reexport (default from non-harmony) */ " +
461
+ this.getReexportStatement(
462
+ module,
463
+ module.isUsed(mode.name),
464
+ importVar,
465
+ null
466
+ )
467
+ );
468
+
469
+ case "reexport-named-default":
470
+ return (
471
+ "/* harmony reexport (default from named exports) */ " +
472
+ this.getReexportStatement(
473
+ module,
474
+ module.isUsed(mode.name),
475
+ importVar,
476
+ ""
477
+ )
478
+ );
479
+
480
+ case "reexport-fake-namespace-object":
481
+ return (
482
+ "/* harmony reexport (fake namespace object from non-harmony) */ " +
483
+ this.getReexportFakeNamespaceObjectStatement(
484
+ module,
485
+ module.isUsed(mode.name),
486
+ importVar
487
+ )
488
+ );
489
+
490
+ case "rexport-non-harmony-undefined":
491
+ return (
492
+ "/* harmony reexport (non default export from non-harmony) */ " +
493
+ this.getReexportStatement(
494
+ module,
495
+ module.isUsed(mode.name),
496
+ "undefined",
497
+ ""
498
+ )
499
+ );
500
+
501
+ case "reexport-non-harmony-default-strict":
502
+ return (
503
+ "/* harmony reexport (default from non-harmony) */ " +
504
+ this.getReexportStatement(
505
+ module,
506
+ module.isUsed(mode.name),
507
+ importVar,
508
+ ""
509
+ )
510
+ );
511
+
512
+ case "reexport-namespace-object":
513
+ return (
514
+ "/* harmony reexport (module object) */ " +
515
+ this.getReexportStatement(
516
+ module,
517
+ module.isUsed(mode.name),
518
+ importVar,
519
+ ""
520
+ )
521
+ );
522
+
523
+ case "empty-star":
524
+ return "/* empty/unused harmony star reexport */";
525
+
526
+ case "safe-reexport":
527
+ return Array.from(mode.map.entries())
528
+ .map(item => {
529
+ return (
530
+ "/* harmony reexport (safe) */ " +
531
+ this.getReexportStatement(
532
+ module,
533
+ module.isUsed(item[0]),
534
+ importVar,
535
+ importedModule.isUsed(item[1])
536
+ ) +
537
+ "\n"
538
+ );
539
+ })
540
+ .join("");
541
+
542
+ case "checked-reexport":
543
+ return Array.from(mode.map.entries())
544
+ .map(item => {
545
+ return (
546
+ "/* harmony reexport (checked) */ " +
547
+ this.getConditionalReexportStatement(
548
+ module,
549
+ item[0],
550
+ importVar,
551
+ item[1]
552
+ ) +
553
+ "\n"
554
+ );
555
+ })
556
+ .join("");
557
+
558
+ case "dynamic-reexport": {
559
+ const activeExports = new Set([
560
+ ...dep.activeExports,
561
+ ...dep._discoverActiveExportsFromOtherStartExports()
562
+ ]);
563
+ let content =
564
+ "/* harmony reexport (unknown) */ for(var __WEBPACK_IMPORT_KEY__ in " +
565
+ importVar +
566
+ ") ";
567
+
568
+ // Filter out exports which are defined by other exports
569
+ // and filter out default export because it cannot be reexported with *
570
+ if (activeExports.length > 0)
571
+ content +=
572
+ "if(" +
573
+ JSON.stringify(activeExports.concat("default")) +
574
+ ".indexOf(__WEBPACK_IMPORT_KEY__) < 0) ";
575
+ else content += "if(__WEBPACK_IMPORT_KEY__ !== 'default') ";
576
+ const exportsName = dep.originModule.exportsArgument;
577
+ return (
578
+ content +
579
+ `(function(key) { __webpack_require__.d(${exportsName}, key, function() { return ${importVar}[key]; }) }(__WEBPACK_IMPORT_KEY__));\n`
580
+ );
581
+ }
582
+
583
+ default:
584
+ throw new Error(`Unknown mode ${mode.type}`);
585
+ }
586
+ }
587
+
588
+ getReexportStatement(module, key, name, valueKey) {
589
+ const exportsName = module.exportsArgument;
590
+ const returnValue = this.getReturnValue(valueKey);
591
+ return `__webpack_require__.d(${exportsName}, ${JSON.stringify(
592
+ key
593
+ )}, function() { return ${name}${returnValue}; });\n`;
594
+ }
595
+
596
+ getReexportFakeNamespaceObjectStatement(module, key, name) {
597
+ const exportsName = module.exportsArgument;
598
+ return `__webpack_require__.d(${exportsName}, ${JSON.stringify(
599
+ key
600
+ )}, function() { return { "default": ${name} }; });\n`;
601
+ }
602
+
603
+ getConditionalReexportStatement(module, key, name, valueKey) {
604
+ const exportsName = module.exportsArgument;
605
+ const returnValue = this.getReturnValue(valueKey);
606
+ return `if(__webpack_require__.o(${name}, ${JSON.stringify(
607
+ valueKey
608
+ )})) __webpack_require__.d(${exportsName}, ${JSON.stringify(
609
+ key
610
+ )}, function() { return ${name}${returnValue}; });\n`;
611
+ }
612
+
613
+ getReturnValue(valueKey) {
614
+ if (valueKey === null) {
615
+ return "_default.a";
616
+ }
617
+
618
+ return valueKey && "[" + JSON.stringify(valueKey) + "]";
619
+ }
620
+ };