webpack 4.15.0 → 4.16.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 (271) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +761 -758
  3. package/SECURITY.md +9 -9
  4. package/buildin/amd-define.js +3 -3
  5. package/buildin/amd-options.js +2 -2
  6. package/buildin/global.js +20 -20
  7. package/buildin/harmony-module.js +24 -24
  8. package/buildin/module.js +22 -22
  9. package/buildin/system.js +7 -7
  10. package/hot/dev-server.js +61 -61
  11. package/hot/emitter.js +2 -2
  12. package/hot/log-apply-result.js +44 -44
  13. package/hot/log.js +45 -45
  14. package/hot/only-dev-server.js +105 -105
  15. package/hot/poll.js +40 -37
  16. package/hot/signal.js +62 -62
  17. package/lib/APIPlugin.js +84 -84
  18. package/lib/AmdMainTemplatePlugin.js +87 -87
  19. package/lib/AsyncDependencyToInitialChunkError.js +31 -31
  20. package/lib/AutomaticPrefetchPlugin.js +1 -1
  21. package/lib/BannerPlugin.js +117 -117
  22. package/lib/BasicEvaluatedExpression.js +211 -211
  23. package/lib/CachePlugin.js +102 -102
  24. package/lib/CaseSensitiveModulesWarning.js +67 -67
  25. package/lib/Chunk.js +833 -811
  26. package/lib/ChunkGroup.js +4 -4
  27. package/lib/ChunkRenderError.js +32 -32
  28. package/lib/CommentCompilationWarning.js +2 -2
  29. package/lib/CompatibilityPlugin.js +70 -70
  30. package/lib/Compilation.js +46 -15
  31. package/lib/ConcurrentCompilationError.js +19 -19
  32. package/lib/ConstPlugin.js +258 -258
  33. package/lib/ContextExclusionPlugin.js +28 -17
  34. package/lib/ContextModule.js +844 -739
  35. package/lib/ContextModuleFactory.js +262 -256
  36. package/lib/ContextReplacementPlugin.js +133 -133
  37. package/lib/DefinePlugin.js +49 -0
  38. package/lib/DelegatedModule.js +5 -0
  39. package/lib/DelegatedModuleFactoryPlugin.js +95 -89
  40. package/lib/DelegatedPlugin.js +39 -39
  41. package/lib/DependenciesBlock.js +1 -1
  42. package/lib/Dependency.js +10 -4
  43. package/lib/DllModule.js +60 -54
  44. package/lib/DllModuleFactory.js +29 -29
  45. package/lib/DllPlugin.js +44 -44
  46. package/lib/DllReferencePlugin.js +132 -84
  47. package/lib/EntryModuleNotFoundError.js +21 -21
  48. package/lib/Entrypoint.js +54 -54
  49. package/lib/EnvironmentPlugin.js +72 -65
  50. package/lib/ErrorHelpers.js +60 -60
  51. package/lib/EvalDevToolModulePlugin.js +27 -27
  52. package/lib/EvalSourceMapDevToolModuleTemplatePlugin.js +115 -115
  53. package/lib/EvalSourceMapDevToolPlugin.js +41 -41
  54. package/lib/ExportPropertyMainTemplatePlugin.js +53 -53
  55. package/lib/ExternalModule.js +165 -159
  56. package/lib/ExternalsPlugin.js +23 -23
  57. package/lib/FlagDependencyExportsPlugin.js +146 -146
  58. package/lib/FlagInitialModulesAsUsedPlugin.js +36 -36
  59. package/lib/FunctionModuleTemplatePlugin.js +100 -100
  60. package/lib/Generator.js +60 -52
  61. package/lib/HarmonyLinkingError.js +17 -17
  62. package/lib/HashedModuleIdsPlugin.js +53 -53
  63. package/lib/HotModuleReplacementPlugin.js +411 -413
  64. package/lib/IgnorePlugin.js +90 -90
  65. package/lib/JavascriptGenerator.js +229 -229
  66. package/lib/JavascriptModulesPlugin.js +179 -179
  67. package/lib/JsonGenerator.js +55 -55
  68. package/lib/JsonModulesPlugin.js +30 -30
  69. package/lib/JsonParser.js +27 -27
  70. package/lib/LibManifestPlugin.js +86 -86
  71. package/lib/LibraryTemplatePlugin.js +153 -153
  72. package/lib/LoaderOptionsPlugin.js +53 -53
  73. package/lib/LoaderTargetPlugin.js +24 -24
  74. package/lib/MemoryOutputFileSystem.js +5 -5
  75. package/lib/Module.js +431 -391
  76. package/lib/ModuleBuildError.js +52 -52
  77. package/lib/ModuleDependencyError.js +35 -35
  78. package/lib/ModuleDependencyWarning.js +25 -25
  79. package/lib/ModuleError.js +36 -36
  80. package/lib/ModuleFilenameHelpers.js +178 -178
  81. package/lib/ModuleNotFoundError.js +23 -23
  82. package/lib/ModuleParseError.js +57 -57
  83. package/lib/ModuleTemplate.js +93 -93
  84. package/lib/ModuleWarning.js +36 -36
  85. package/lib/MultiCompiler.js +283 -283
  86. package/lib/MultiModule.js +87 -81
  87. package/lib/MultiModuleFactory.js +23 -23
  88. package/lib/MultiStats.js +92 -92
  89. package/lib/MultiWatching.js +38 -38
  90. package/lib/NamedChunksPlugin.js +29 -29
  91. package/lib/NamedModulesPlugin.js +57 -57
  92. package/lib/NoEmitOnErrorsPlugin.js +20 -20
  93. package/lib/NoModeWarning.js +23 -23
  94. package/lib/NodeStuffPlugin.js +197 -179
  95. package/lib/NormalModule.js +542 -536
  96. package/lib/NormalModuleFactory.js +526 -526
  97. package/lib/NormalModuleReplacementPlugin.js +51 -51
  98. package/lib/NullFactory.js +12 -12
  99. package/lib/OptionsApply.js +10 -10
  100. package/lib/OptionsDefaulter.js +84 -84
  101. package/lib/Parser.js +2202 -2193
  102. package/lib/ParserHelpers.js +103 -103
  103. package/lib/PrefetchPlugin.js +37 -37
  104. package/lib/ProgressPlugin.js +246 -246
  105. package/lib/ProvidePlugin.js +86 -86
  106. package/lib/RawModule.js +56 -56
  107. package/lib/RecordIdsPlugin.js +230 -230
  108. package/lib/RemovedPluginError.js +11 -11
  109. package/lib/RequestShortener.js +83 -83
  110. package/lib/RequireJsStuffPlugin.js +69 -69
  111. package/lib/ResolverFactory.js +64 -64
  112. package/lib/RuntimeTemplate.js +12 -0
  113. package/lib/SetVarMainTemplatePlugin.js +69 -69
  114. package/lib/SingleEntryPlugin.js +6 -1
  115. package/lib/SizeFormatHelpers.js +24 -24
  116. package/lib/SourceMapDevToolModuleOptionsPlugin.js +49 -49
  117. package/lib/SourceMapDevToolPlugin.js +301 -301
  118. package/lib/Stats.js +28 -5
  119. package/lib/TemplatedPathPlugin.js +173 -173
  120. package/lib/UnsupportedFeatureWarning.js +22 -22
  121. package/lib/UseStrictPlugin.js +54 -54
  122. package/lib/WarnCaseSensitiveModulesPlugin.js +37 -37
  123. package/lib/WarnNoModeSetPlugin.js +17 -17
  124. package/lib/WatchIgnorePlugin.js +100 -100
  125. package/lib/Watching.js +194 -194
  126. package/lib/WebpackOptionsApply.js +92 -10
  127. package/lib/WebpackOptionsDefaulter.js +368 -354
  128. package/lib/debug/ProfilingPlugin.js +430 -430
  129. package/lib/dependencies/AMDPlugin.js +250 -250
  130. package/lib/dependencies/AMDRequireArrayDependency.js +49 -49
  131. package/lib/dependencies/AMDRequireContextDependency.js +20 -20
  132. package/lib/dependencies/AMDRequireDependency.js +135 -135
  133. package/lib/dependencies/AMDRequireItemDependency.js +22 -22
  134. package/lib/dependencies/CommonJsPlugin.js +161 -161
  135. package/lib/dependencies/CommonJsRequireContextDependency.js +23 -23
  136. package/lib/dependencies/CommonJsRequireDependency.js +22 -22
  137. package/lib/dependencies/CommonJsRequireDependencyParserPlugin.js +6 -0
  138. package/lib/dependencies/ConstDependency.js +33 -33
  139. package/lib/dependencies/ContextDependency.js +68 -68
  140. package/lib/dependencies/ContextDependencyHelpers.js +142 -142
  141. package/lib/dependencies/ContextDependencyTemplateAsId.js +42 -42
  142. package/lib/dependencies/ContextDependencyTemplateAsRequireCall.js +38 -38
  143. package/lib/dependencies/ContextElementDependency.js +21 -21
  144. package/lib/dependencies/CriticalDependencyWarning.js +20 -20
  145. package/lib/dependencies/DelegatedSourceDependency.js +18 -18
  146. package/lib/dependencies/DllEntryDependency.js +20 -20
  147. package/lib/dependencies/HarmonyAcceptDependency.js +45 -45
  148. package/lib/dependencies/HarmonyCompatibilityDependency.js +31 -31
  149. package/lib/dependencies/HarmonyDetectionParserPlugin.js +92 -92
  150. package/lib/dependencies/HarmonyExportDependencyParserPlugin.js +139 -139
  151. package/lib/dependencies/HarmonyExportHeaderDependency.js +30 -30
  152. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +2 -1
  153. package/lib/dependencies/HarmonyImportDependency.js +109 -109
  154. package/lib/dependencies/HarmonyImportDependencyParserPlugin.js +222 -222
  155. package/lib/dependencies/HarmonyImportSideEffectDependency.js +31 -31
  156. package/lib/dependencies/HarmonyImportSpecifierDependency.js +167 -167
  157. package/lib/dependencies/HarmonyInitDependency.js +60 -60
  158. package/lib/dependencies/HarmonyModulesPlugin.js +146 -146
  159. package/lib/dependencies/HarmonyTopLevelThisParserPlugin.js +26 -26
  160. package/lib/dependencies/ImportContextDependency.js +23 -23
  161. package/lib/dependencies/ImportDependenciesBlock.js +18 -18
  162. package/lib/dependencies/ImportDependency.js +34 -34
  163. package/lib/dependencies/ImportEagerDependency.js +32 -32
  164. package/lib/dependencies/ImportParserPlugin.js +263 -263
  165. package/lib/dependencies/ImportPlugin.js +82 -82
  166. package/lib/dependencies/ImportWeakDependency.js +34 -34
  167. package/lib/dependencies/LoaderPlugin.js +18 -1
  168. package/lib/dependencies/LocalModule.js +23 -23
  169. package/lib/dependencies/LocalModulesHelpers.js +52 -52
  170. package/lib/dependencies/ModuleDependencyTemplateAsId.js +17 -17
  171. package/lib/dependencies/ModuleDependencyTemplateAsRequireId.js +17 -17
  172. package/lib/dependencies/ModuleHotAcceptDependency.js +23 -23
  173. package/lib/dependencies/ModuleHotDeclineDependency.js +23 -23
  174. package/lib/dependencies/NullDependency.js +20 -20
  175. package/lib/dependencies/PrefetchDependency.js +18 -18
  176. package/lib/dependencies/RequireContextDependency.js +22 -22
  177. package/lib/dependencies/RequireContextDependencyParserPlugin.js +56 -56
  178. package/lib/dependencies/RequireContextPlugin.js +143 -143
  179. package/lib/dependencies/RequireEnsureDependenciesBlock.js +33 -33
  180. package/lib/dependencies/RequireEnsureDependenciesBlockParserPlugin.js +116 -116
  181. package/lib/dependencies/RequireEnsureDependency.js +58 -58
  182. package/lib/dependencies/RequireEnsureItemDependency.js +21 -21
  183. package/lib/dependencies/RequireEnsurePlugin.js +74 -74
  184. package/lib/dependencies/RequireHeaderDependency.js +26 -26
  185. package/lib/dependencies/RequireIncludeDependencyParserPlugin.js +23 -23
  186. package/lib/dependencies/RequireIncludePlugin.js +61 -61
  187. package/lib/dependencies/RequireResolveContextDependency.js +23 -23
  188. package/lib/dependencies/RequireResolveDependency.js +22 -22
  189. package/lib/dependencies/RequireResolveDependencyParserPlugin.js +85 -85
  190. package/lib/dependencies/RequireResolveHeaderDependency.js +26 -26
  191. package/lib/dependencies/SystemPlugin.js +125 -125
  192. package/lib/dependencies/UnsupportedDependency.js +27 -27
  193. package/lib/dependencies/WebAssemblyExportImportedDependency.js +29 -29
  194. package/lib/dependencies/WebAssemblyImportDependency.js +48 -48
  195. package/lib/dependencies/WebpackMissingModule.js +20 -20
  196. package/lib/formatLocation.js +75 -61
  197. package/lib/node/NodeChunkTemplatePlugin.js +31 -31
  198. package/lib/node/NodeEnvironmentPlugin.js +28 -28
  199. package/lib/node/NodeHotUpdateChunkTemplatePlugin.js +36 -36
  200. package/lib/node/NodeMainTemplate.runtime.js +27 -27
  201. package/lib/node/NodeMainTemplatePlugin.js +323 -323
  202. package/lib/node/NodeOutputFileSystem.js +22 -22
  203. package/lib/node/NodeSourcePlugin.js +144 -144
  204. package/lib/node/NodeTargetPlugin.js +18 -18
  205. package/lib/node/NodeTemplatePlugin.js +31 -31
  206. package/lib/node/NodeWatchFileSystem.js +99 -99
  207. package/lib/node/ReadFileCompileWasmTemplatePlugin.js +61 -61
  208. package/lib/optimize/AggressiveMergingPlugin.js +87 -87
  209. package/lib/optimize/AggressiveSplittingPlugin.js +287 -287
  210. package/lib/optimize/ConcatenatedModule.js +5 -0
  211. package/lib/optimize/EnsureChunkConditionsPlugin.js +70 -70
  212. package/lib/optimize/FlagIncludedChunksPlugin.js +99 -99
  213. package/lib/optimize/LimitChunkCountPlugin.js +66 -66
  214. package/lib/optimize/MergeDuplicateChunksPlugin.js +78 -78
  215. package/lib/optimize/MinChunkSizePlugin.js +77 -77
  216. package/lib/optimize/NaturalChunkOrderPlugin.js +41 -0
  217. package/lib/optimize/OccurrenceChunkOrderPlugin.js +61 -0
  218. package/lib/optimize/OccurrenceModuleOrderPlugin.js +103 -0
  219. package/lib/optimize/OccurrenceOrderPlugin.js +135 -133
  220. package/lib/optimize/RemoveEmptyChunksPlugin.js +42 -42
  221. package/lib/optimize/RemoveParentModulesPlugin.js +127 -127
  222. package/lib/optimize/RuntimeChunkPlugin.js +41 -41
  223. package/lib/optimize/SideEffectsFlagPlugin.js +168 -168
  224. package/lib/optimize/SplitChunksPlugin.js +866 -850
  225. package/lib/performance/AssetsOverSizeLimitWarning.js +30 -30
  226. package/lib/performance/EntrypointsOverSizeLimitWarning.js +30 -30
  227. package/lib/performance/NoAsyncChunksWarning.js +21 -21
  228. package/lib/performance/SizeLimitsPlugin.js +105 -105
  229. package/lib/util/SortableSet.js +1 -0
  230. package/lib/util/StackedSetMap.js +144 -135
  231. package/lib/util/TrackingSet.js +35 -35
  232. package/lib/util/cachedMerge.js +35 -35
  233. package/lib/util/deterministicGrouping.js +251 -251
  234. package/lib/util/identifier.js +103 -103
  235. package/lib/util/objectToMap.js +16 -16
  236. package/lib/validateSchema.js +67 -67
  237. package/lib/wasm/UnsupportedWebAssemblyFeatureError.js +17 -17
  238. package/lib/wasm/WasmFinalizeExportsPlugin.js +1 -1
  239. package/lib/wasm/WebAssemblyGenerator.js +16 -2
  240. package/lib/wasm/WebAssemblyJavascriptGenerator.js +147 -133
  241. package/lib/wasm/WebAssemblyParser.js +174 -174
  242. package/lib/wasm/WebAssemblyUtils.js +59 -59
  243. package/lib/web/FetchCompileWasmTemplatePlugin.js +37 -37
  244. package/lib/web/JsonpExportMainTemplatePlugin.js +47 -47
  245. package/lib/web/JsonpHotUpdateChunkTemplatePlugin.js +39 -39
  246. package/lib/web/JsonpMainTemplate.runtime.js +65 -65
  247. package/lib/web/JsonpMainTemplatePlugin.js +1 -1
  248. package/lib/web/JsonpTemplatePlugin.js +23 -23
  249. package/lib/web/WebEnvironmentPlugin.js +18 -18
  250. package/lib/webpack.js +5 -0
  251. package/lib/webworker/WebWorkerChunkTemplatePlugin.js +35 -35
  252. package/lib/webworker/WebWorkerHotUpdateChunkTemplatePlugin.js +40 -40
  253. package/lib/webworker/WebWorkerMainTemplate.runtime.js +65 -65
  254. package/lib/webworker/WebWorkerMainTemplatePlugin.js +196 -196
  255. package/lib/webworker/WebWorkerTemplatePlugin.js +25 -25
  256. package/package.json +22 -13
  257. package/schemas/WebpackOptions.json +2062 -2036
  258. package/schemas/ajv.absolutePath.js +55 -55
  259. package/schemas/plugins/BannerPlugin.json +96 -96
  260. package/schemas/plugins/DllPlugin.json +32 -32
  261. package/schemas/plugins/DllReferencePlugin.json +99 -99
  262. package/schemas/plugins/HashedModuleIdsPlugin.json +24 -24
  263. package/schemas/plugins/LoaderOptionsPlugin.json +26 -26
  264. package/schemas/plugins/SourceMapDevToolPlugin.json +187 -187
  265. package/schemas/plugins/WatchIgnorePlugin.json +16 -16
  266. package/schemas/plugins/debug/ProfilingPlugin.json +12 -12
  267. package/schemas/plugins/optimize/AggressiveSplittingPlugin.json +22 -22
  268. package/schemas/plugins/optimize/LimitChunkCountPlugin.json +15 -15
  269. package/schemas/plugins/optimize/MinChunkSizePlugin.json +13 -13
  270. package/schemas/plugins/optimize/OccurrenceOrderChunkIdsPlugin.json +10 -0
  271. package/schemas/plugins/optimize/OccurrenceOrderModuleIdsPlugin.json +10 -0
@@ -1,536 +1,542 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- "use strict";
6
-
7
- const NativeModule = require("module");
8
-
9
- const {
10
- CachedSource,
11
- LineToLineMappedSource,
12
- OriginalSource,
13
- RawSource,
14
- SourceMapSource
15
- } = require("webpack-sources");
16
- const { getContext, runLoaders } = require("loader-runner");
17
-
18
- const WebpackError = require("./WebpackError");
19
- const Module = require("./Module");
20
- const ModuleParseError = require("./ModuleParseError");
21
- const ModuleBuildError = require("./ModuleBuildError");
22
- const ModuleError = require("./ModuleError");
23
- const ModuleWarning = require("./ModuleWarning");
24
- const createHash = require("./util/createHash");
25
- const contextify = require("./util/identifier").contextify;
26
-
27
- const asString = buf => {
28
- if (Buffer.isBuffer(buf)) {
29
- return buf.toString("utf-8");
30
- }
31
- return buf;
32
- };
33
-
34
- const asBuffer = str => {
35
- if (!Buffer.isBuffer(str)) {
36
- return Buffer.from(str, "utf-8");
37
- }
38
- return str;
39
- };
40
-
41
- class NonErrorEmittedError extends WebpackError {
42
- constructor(error) {
43
- super();
44
-
45
- this.name = "NonErrorEmittedError";
46
- this.message = "(Emitted value instead of an instance of Error) " + error;
47
-
48
- Error.captureStackTrace(this, this.constructor);
49
- }
50
- }
51
-
52
- /**
53
- * @typedef {Object} CachedSourceEntry
54
- * @property {TODO} source the generated source
55
- * @property {string} hash the hash value
56
- */
57
-
58
- class NormalModule extends Module {
59
- constructor({
60
- type,
61
- request,
62
- userRequest,
63
- rawRequest,
64
- loaders,
65
- resource,
66
- matchResource,
67
- parser,
68
- generator,
69
- resolveOptions
70
- }) {
71
- super(type, getContext(resource));
72
-
73
- // Info from Factory
74
- this.request = request;
75
- this.userRequest = userRequest;
76
- this.rawRequest = rawRequest;
77
- this.binary = type.startsWith("webassembly");
78
- this.parser = parser;
79
- this.generator = generator;
80
- this.resource = resource;
81
- this.matchResource = matchResource;
82
- this.loaders = loaders;
83
- if (resolveOptions !== undefined) this.resolveOptions = resolveOptions;
84
-
85
- // Info from Build
86
- this.error = null;
87
- this._source = null;
88
- this._buildHash = "";
89
- this.buildTimestamp = undefined;
90
- /** @private @type {Map<string, CachedSourceEntry>} */
91
- this._cachedSources = new Map();
92
-
93
- // Options for the NormalModule set by plugins
94
- // TODO refactor this -> options object filled from Factory
95
- this.useSourceMap = false;
96
- this.lineToLine = false;
97
-
98
- // Cache
99
- this._lastSuccessfulBuildMeta = {};
100
- }
101
-
102
- identifier() {
103
- return this.request;
104
- }
105
-
106
- readableIdentifier(requestShortener) {
107
- return requestShortener.shorten(this.userRequest);
108
- }
109
-
110
- libIdent(options) {
111
- return contextify(options.context, this.userRequest);
112
- }
113
-
114
- nameForCondition() {
115
- const resource = this.matchResource || this.resource;
116
- const idx = resource.indexOf("?");
117
- if (idx >= 0) return resource.substr(0, idx);
118
- return resource;
119
- }
120
-
121
- updateCacheModule(module) {
122
- this.type = module.type;
123
- this.request = module.request;
124
- this.userRequest = module.userRequest;
125
- this.rawRequest = module.rawRequest;
126
- this.parser = module.parser;
127
- this.generator = module.generator;
128
- this.resource = module.resource;
129
- this.matchResource = module.matchResource;
130
- this.loaders = module.loaders;
131
- this.resolveOptions = module.resolveOptions;
132
- }
133
-
134
- createSourceForAsset(name, content, sourceMap) {
135
- if (!sourceMap) {
136
- return new RawSource(content);
137
- }
138
-
139
- if (typeof sourceMap === "string") {
140
- return new OriginalSource(content, sourceMap);
141
- }
142
-
143
- return new SourceMapSource(content, name, sourceMap);
144
- }
145
-
146
- createLoaderContext(resolver, options, compilation, fs) {
147
- const requestShortener = compilation.runtimeTemplate.requestShortener;
148
- const loaderContext = {
149
- version: 2,
150
- emitWarning: warning => {
151
- if (!(warning instanceof Error)) {
152
- warning = new NonErrorEmittedError(warning);
153
- }
154
- const currentLoader = this.getCurrentLoader(loaderContext);
155
- this.warnings.push(
156
- new ModuleWarning(this, warning, {
157
- from: requestShortener.shorten(currentLoader.loader)
158
- })
159
- );
160
- },
161
- emitError: error => {
162
- if (!(error instanceof Error)) {
163
- error = new NonErrorEmittedError(error);
164
- }
165
- const currentLoader = this.getCurrentLoader(loaderContext);
166
- this.errors.push(
167
- new ModuleError(this, error, {
168
- from: requestShortener.shorten(currentLoader.loader)
169
- })
170
- );
171
- },
172
- // TODO remove in webpack 5
173
- exec: (code, filename) => {
174
- // @ts-ignore Argument of type 'this' is not assignable to parameter of type 'Module'.
175
- const module = new NativeModule(filename, this);
176
- // @ts-ignore _nodeModulePaths is deprecated and undocumented Node.js API
177
- module.paths = NativeModule._nodeModulePaths(this.context);
178
- module.filename = filename;
179
- module._compile(code, filename);
180
- return module.exports;
181
- },
182
- resolve(context, request, callback) {
183
- resolver.resolve({}, context, request, {}, callback);
184
- },
185
- emitFile: (name, content, sourceMap) => {
186
- if (!this.buildInfo.assets) {
187
- this.buildInfo.assets = Object.create(null);
188
- }
189
- this.buildInfo.assets[name] = this.createSourceForAsset(
190
- name,
191
- content,
192
- sourceMap
193
- );
194
- },
195
- rootContext: options.context,
196
- webpack: true,
197
- sourceMap: !!this.useSourceMap,
198
- _module: this,
199
- _compilation: compilation,
200
- _compiler: compilation.compiler,
201
- fs: fs
202
- };
203
-
204
- compilation.hooks.normalModuleLoader.call(loaderContext, this);
205
- if (options.loader) {
206
- Object.assign(loaderContext, options.loader);
207
- }
208
-
209
- return loaderContext;
210
- }
211
-
212
- getCurrentLoader(loaderContext, index = loaderContext.loaderIndex) {
213
- if (
214
- this.loaders &&
215
- this.loaders.length &&
216
- index < this.loaders.length &&
217
- index >= 0 &&
218
- this.loaders[index]
219
- ) {
220
- return this.loaders[index];
221
- }
222
- return null;
223
- }
224
-
225
- createSource(source, resourceBuffer, sourceMap) {
226
- // if there is no identifier return raw source
227
- if (!this.identifier) {
228
- return new RawSource(source);
229
- }
230
-
231
- // from here on we assume we have an identifier
232
- const identifier = this.identifier();
233
-
234
- if (this.lineToLine && resourceBuffer) {
235
- return new LineToLineMappedSource(
236
- source,
237
- identifier,
238
- asString(resourceBuffer)
239
- );
240
- }
241
-
242
- if (this.useSourceMap && sourceMap) {
243
- return new SourceMapSource(source, identifier, sourceMap);
244
- }
245
-
246
- if (Buffer.isBuffer(source)) {
247
- // @ts-ignore
248
- // TODO We need to fix @types/webpack-sources to allow RawSource to take a Buffer | string
249
- return new RawSource(source);
250
- }
251
-
252
- return new OriginalSource(source, identifier);
253
- }
254
-
255
- doBuild(options, compilation, resolver, fs, callback) {
256
- const loaderContext = this.createLoaderContext(
257
- resolver,
258
- options,
259
- compilation,
260
- fs
261
- );
262
-
263
- runLoaders(
264
- {
265
- resource: this.resource,
266
- loaders: this.loaders,
267
- context: loaderContext,
268
- readResource: fs.readFile.bind(fs)
269
- },
270
- (err, result) => {
271
- if (result) {
272
- this.buildInfo.cacheable = result.cacheable;
273
- this.buildInfo.fileDependencies = new Set(result.fileDependencies);
274
- this.buildInfo.contextDependencies = new Set(
275
- result.contextDependencies
276
- );
277
- }
278
-
279
- if (err) {
280
- if (!(err instanceof Error)) {
281
- err = new NonErrorEmittedError(err);
282
- }
283
- const currentLoader = this.getCurrentLoader(loaderContext);
284
- const error = new ModuleBuildError(this, err, {
285
- from:
286
- currentLoader &&
287
- compilation.runtimeTemplate.requestShortener.shorten(
288
- currentLoader.loader
289
- )
290
- });
291
- return callback(error);
292
- }
293
-
294
- const resourceBuffer = result.resourceBuffer;
295
- const source = result.result[0];
296
- const sourceMap = result.result.length >= 1 ? result.result[1] : null;
297
- const extraInfo = result.result.length >= 2 ? result.result[2] : null;
298
-
299
- if (!Buffer.isBuffer(source) && typeof source !== "string") {
300
- const currentLoader = this.getCurrentLoader(loaderContext, 0);
301
- const err = new Error(
302
- `Final loader (${
303
- currentLoader
304
- ? compilation.runtimeTemplate.requestShortener.shorten(
305
- currentLoader.loader
306
- )
307
- : "unknown"
308
- }) didn't return a Buffer or String`
309
- );
310
- const error = new ModuleBuildError(this, err);
311
- return callback(error);
312
- }
313
-
314
- this._source = this.createSource(
315
- this.binary ? asBuffer(source) : asString(source),
316
- resourceBuffer,
317
- sourceMap
318
- );
319
- this._ast =
320
- typeof extraInfo === "object" &&
321
- extraInfo !== null &&
322
- extraInfo.webpackAST !== undefined
323
- ? extraInfo.webpackAST
324
- : null;
325
- return callback();
326
- }
327
- );
328
- }
329
-
330
- markModuleAsErrored(error) {
331
- // Restore build meta from successful build to keep importing state
332
- this.buildMeta = Object.assign({}, this._lastSuccessfulBuildMeta);
333
-
334
- this.error = error;
335
- this.errors.push(this.error);
336
- this._source = new RawSource(
337
- "throw new Error(" + JSON.stringify(this.error.message) + ");"
338
- );
339
- this._ast = null;
340
- }
341
-
342
- applyNoParseRule(rule, content) {
343
- // must start with "rule" if rule is a string
344
- if (typeof rule === "string") {
345
- return content.indexOf(rule) === 0;
346
- }
347
-
348
- if (typeof rule === "function") {
349
- return rule(content);
350
- }
351
- // we assume rule is a regexp
352
- return rule.test(content);
353
- }
354
-
355
- // check if module should not be parsed
356
- // returns "true" if the module should !not! be parsed
357
- // returns "false" if the module !must! be parsed
358
- shouldPreventParsing(noParseRule, request) {
359
- // if no noParseRule exists, return false
360
- // the module !must! be parsed.
361
- if (!noParseRule) {
362
- return false;
363
- }
364
-
365
- // we only have one rule to check
366
- if (!Array.isArray(noParseRule)) {
367
- // returns "true" if the module is !not! to be parsed
368
- return this.applyNoParseRule(noParseRule, request);
369
- }
370
-
371
- for (let i = 0; i < noParseRule.length; i++) {
372
- const rule = noParseRule[i];
373
- // early exit on first truthy match
374
- // this module is !not! to be parsed
375
- if (this.applyNoParseRule(rule, request)) {
376
- return true;
377
- }
378
- }
379
- // no match found, so this module !should! be parsed
380
- return false;
381
- }
382
-
383
- _initBuildHash(compilation) {
384
- const hash = createHash(compilation.outputOptions.hashFunction);
385
- if (this._source) {
386
- hash.update("source");
387
- this._source.updateHash(hash);
388
- }
389
- hash.update("meta");
390
- hash.update(JSON.stringify(this.buildMeta));
391
- this._buildHash = hash.digest("hex");
392
- }
393
-
394
- build(options, compilation, resolver, fs, callback) {
395
- this.buildTimestamp = Date.now();
396
- this.built = true;
397
- this._source = null;
398
- this._ast = null;
399
- this._buildHash = "";
400
- this.error = null;
401
- this.errors.length = 0;
402
- this.warnings.length = 0;
403
- this.buildMeta = {};
404
- this.buildInfo = {
405
- cacheable: false,
406
- fileDependencies: new Set(),
407
- contextDependencies: new Set()
408
- };
409
-
410
- return this.doBuild(options, compilation, resolver, fs, err => {
411
- this._cachedSources.clear();
412
-
413
- // if we have an error mark module as failed and exit
414
- if (err) {
415
- this.markModuleAsErrored(err);
416
- this._initBuildHash(compilation);
417
- return callback();
418
- }
419
-
420
- // check if this module should !not! be parsed.
421
- // if so, exit here;
422
- const noParseRule = options.module && options.module.noParse;
423
- if (this.shouldPreventParsing(noParseRule, this.request)) {
424
- this._initBuildHash(compilation);
425
- return callback();
426
- }
427
-
428
- const handleParseError = e => {
429
- const source = this._source.source();
430
- const error = new ModuleParseError(this, source, e);
431
- this.markModuleAsErrored(error);
432
- this._initBuildHash(compilation);
433
- return callback();
434
- };
435
-
436
- const handleParseResult = result => {
437
- this._lastSuccessfulBuildMeta = this.buildMeta;
438
- this._initBuildHash(compilation);
439
- return callback();
440
- };
441
-
442
- try {
443
- const result = this.parser.parse(
444
- this._ast || this._source.source(),
445
- {
446
- current: this,
447
- module: this,
448
- compilation: compilation,
449
- options: options
450
- },
451
- (err, result) => {
452
- if (err) {
453
- handleParseError(err);
454
- } else {
455
- handleParseResult(result);
456
- }
457
- }
458
- );
459
- if (result !== undefined) {
460
- // parse is sync
461
- handleParseResult(result);
462
- }
463
- } catch (e) {
464
- handleParseError(e);
465
- }
466
- });
467
- }
468
-
469
- getHashDigest(dependencyTemplates) {
470
- // TODO webpack 5 refactor
471
- let dtHash = dependencyTemplates.get("hash");
472
- return `${this.hash}-${dtHash}`;
473
- }
474
-
475
- source(dependencyTemplates, runtimeTemplate, type = "javascript") {
476
- const hashDigest = this.getHashDigest(dependencyTemplates);
477
- const cacheEntry = this._cachedSources.get(type);
478
- if (cacheEntry !== undefined && cacheEntry.hash === hashDigest) {
479
- // We can reuse the cached source
480
- return cacheEntry.source;
481
- }
482
-
483
- const source = this.generator.generate(
484
- this,
485
- dependencyTemplates,
486
- runtimeTemplate,
487
- type
488
- );
489
-
490
- const cachedSource = new CachedSource(source);
491
- this._cachedSources.set(type, {
492
- source: cachedSource,
493
- hash: hashDigest
494
- });
495
- return cachedSource;
496
- }
497
-
498
- originalSource() {
499
- return this._source;
500
- }
501
-
502
- needRebuild(fileTimestamps, contextTimestamps) {
503
- // always try to rebuild in case of an error
504
- if (this.error) return true;
505
-
506
- // always rebuild when module is not cacheable
507
- if (!this.buildInfo.cacheable) return true;
508
-
509
- // Check timestamps of all dependencies
510
- // Missing timestamp -> need rebuild
511
- // Timestamp bigger than buildTimestamp -> need rebuild
512
- for (const file of this.buildInfo.fileDependencies) {
513
- const timestamp = fileTimestamps.get(file);
514
- if (!timestamp) return true;
515
- if (timestamp >= this.buildTimestamp) return true;
516
- }
517
- for (const file of this.buildInfo.contextDependencies) {
518
- const timestamp = contextTimestamps.get(file);
519
- if (!timestamp) return true;
520
- if (timestamp >= this.buildTimestamp) return true;
521
- }
522
- // elsewise -> no rebuild needed
523
- return false;
524
- }
525
-
526
- size() {
527
- return this._source ? this._source.size() : -1;
528
- }
529
-
530
- updateHash(hash) {
531
- hash.update(this._buildHash);
532
- super.updateHash(hash);
533
- }
534
- }
535
-
536
- module.exports = NormalModule;
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+ "use strict";
6
+
7
+ const NativeModule = require("module");
8
+
9
+ const {
10
+ CachedSource,
11
+ LineToLineMappedSource,
12
+ OriginalSource,
13
+ RawSource,
14
+ SourceMapSource
15
+ } = require("webpack-sources");
16
+ const { getContext, runLoaders } = require("loader-runner");
17
+
18
+ const WebpackError = require("./WebpackError");
19
+ const Module = require("./Module");
20
+ const ModuleParseError = require("./ModuleParseError");
21
+ const ModuleBuildError = require("./ModuleBuildError");
22
+ const ModuleError = require("./ModuleError");
23
+ const ModuleWarning = require("./ModuleWarning");
24
+ const createHash = require("./util/createHash");
25
+ const contextify = require("./util/identifier").contextify;
26
+
27
+ /** @typedef {import("./util/createHash").Hash} Hash */
28
+
29
+ const asString = buf => {
30
+ if (Buffer.isBuffer(buf)) {
31
+ return buf.toString("utf-8");
32
+ }
33
+ return buf;
34
+ };
35
+
36
+ const asBuffer = str => {
37
+ if (!Buffer.isBuffer(str)) {
38
+ return Buffer.from(str, "utf-8");
39
+ }
40
+ return str;
41
+ };
42
+
43
+ class NonErrorEmittedError extends WebpackError {
44
+ constructor(error) {
45
+ super();
46
+
47
+ this.name = "NonErrorEmittedError";
48
+ this.message = "(Emitted value instead of an instance of Error) " + error;
49
+
50
+ Error.captureStackTrace(this, this.constructor);
51
+ }
52
+ }
53
+
54
+ /**
55
+ * @typedef {Object} CachedSourceEntry
56
+ * @property {TODO} source the generated source
57
+ * @property {string} hash the hash value
58
+ */
59
+
60
+ class NormalModule extends Module {
61
+ constructor({
62
+ type,
63
+ request,
64
+ userRequest,
65
+ rawRequest,
66
+ loaders,
67
+ resource,
68
+ matchResource,
69
+ parser,
70
+ generator,
71
+ resolveOptions
72
+ }) {
73
+ super(type, getContext(resource));
74
+
75
+ // Info from Factory
76
+ this.request = request;
77
+ this.userRequest = userRequest;
78
+ this.rawRequest = rawRequest;
79
+ this.binary = type.startsWith("webassembly");
80
+ this.parser = parser;
81
+ this.generator = generator;
82
+ this.resource = resource;
83
+ this.matchResource = matchResource;
84
+ this.loaders = loaders;
85
+ if (resolveOptions !== undefined) this.resolveOptions = resolveOptions;
86
+
87
+ // Info from Build
88
+ this.error = null;
89
+ this._source = null;
90
+ this._buildHash = "";
91
+ this.buildTimestamp = undefined;
92
+ /** @private @type {Map<string, CachedSourceEntry>} */
93
+ this._cachedSources = new Map();
94
+
95
+ // Options for the NormalModule set by plugins
96
+ // TODO refactor this -> options object filled from Factory
97
+ this.useSourceMap = false;
98
+ this.lineToLine = false;
99
+
100
+ // Cache
101
+ this._lastSuccessfulBuildMeta = {};
102
+ }
103
+
104
+ identifier() {
105
+ return this.request;
106
+ }
107
+
108
+ readableIdentifier(requestShortener) {
109
+ return requestShortener.shorten(this.userRequest);
110
+ }
111
+
112
+ libIdent(options) {
113
+ return contextify(options.context, this.userRequest);
114
+ }
115
+
116
+ nameForCondition() {
117
+ const resource = this.matchResource || this.resource;
118
+ const idx = resource.indexOf("?");
119
+ if (idx >= 0) return resource.substr(0, idx);
120
+ return resource;
121
+ }
122
+
123
+ updateCacheModule(module) {
124
+ this.type = module.type;
125
+ this.request = module.request;
126
+ this.userRequest = module.userRequest;
127
+ this.rawRequest = module.rawRequest;
128
+ this.parser = module.parser;
129
+ this.generator = module.generator;
130
+ this.resource = module.resource;
131
+ this.matchResource = module.matchResource;
132
+ this.loaders = module.loaders;
133
+ this.resolveOptions = module.resolveOptions;
134
+ }
135
+
136
+ createSourceForAsset(name, content, sourceMap) {
137
+ if (!sourceMap) {
138
+ return new RawSource(content);
139
+ }
140
+
141
+ if (typeof sourceMap === "string") {
142
+ return new OriginalSource(content, sourceMap);
143
+ }
144
+
145
+ return new SourceMapSource(content, name, sourceMap);
146
+ }
147
+
148
+ createLoaderContext(resolver, options, compilation, fs) {
149
+ const requestShortener = compilation.runtimeTemplate.requestShortener;
150
+ const loaderContext = {
151
+ version: 2,
152
+ emitWarning: warning => {
153
+ if (!(warning instanceof Error)) {
154
+ warning = new NonErrorEmittedError(warning);
155
+ }
156
+ const currentLoader = this.getCurrentLoader(loaderContext);
157
+ this.warnings.push(
158
+ new ModuleWarning(this, warning, {
159
+ from: requestShortener.shorten(currentLoader.loader)
160
+ })
161
+ );
162
+ },
163
+ emitError: error => {
164
+ if (!(error instanceof Error)) {
165
+ error = new NonErrorEmittedError(error);
166
+ }
167
+ const currentLoader = this.getCurrentLoader(loaderContext);
168
+ this.errors.push(
169
+ new ModuleError(this, error, {
170
+ from: requestShortener.shorten(currentLoader.loader)
171
+ })
172
+ );
173
+ },
174
+ // TODO remove in webpack 5
175
+ exec: (code, filename) => {
176
+ // @ts-ignore Argument of type 'this' is not assignable to parameter of type 'Module'.
177
+ const module = new NativeModule(filename, this);
178
+ // @ts-ignore _nodeModulePaths is deprecated and undocumented Node.js API
179
+ module.paths = NativeModule._nodeModulePaths(this.context);
180
+ module.filename = filename;
181
+ module._compile(code, filename);
182
+ return module.exports;
183
+ },
184
+ resolve(context, request, callback) {
185
+ resolver.resolve({}, context, request, {}, callback);
186
+ },
187
+ emitFile: (name, content, sourceMap) => {
188
+ if (!this.buildInfo.assets) {
189
+ this.buildInfo.assets = Object.create(null);
190
+ }
191
+ this.buildInfo.assets[name] = this.createSourceForAsset(
192
+ name,
193
+ content,
194
+ sourceMap
195
+ );
196
+ },
197
+ rootContext: options.context,
198
+ webpack: true,
199
+ sourceMap: !!this.useSourceMap,
200
+ _module: this,
201
+ _compilation: compilation,
202
+ _compiler: compilation.compiler,
203
+ fs: fs
204
+ };
205
+
206
+ compilation.hooks.normalModuleLoader.call(loaderContext, this);
207
+ if (options.loader) {
208
+ Object.assign(loaderContext, options.loader);
209
+ }
210
+
211
+ return loaderContext;
212
+ }
213
+
214
+ getCurrentLoader(loaderContext, index = loaderContext.loaderIndex) {
215
+ if (
216
+ this.loaders &&
217
+ this.loaders.length &&
218
+ index < this.loaders.length &&
219
+ index >= 0 &&
220
+ this.loaders[index]
221
+ ) {
222
+ return this.loaders[index];
223
+ }
224
+ return null;
225
+ }
226
+
227
+ createSource(source, resourceBuffer, sourceMap) {
228
+ // if there is no identifier return raw source
229
+ if (!this.identifier) {
230
+ return new RawSource(source);
231
+ }
232
+
233
+ // from here on we assume we have an identifier
234
+ const identifier = this.identifier();
235
+
236
+ if (this.lineToLine && resourceBuffer) {
237
+ return new LineToLineMappedSource(
238
+ source,
239
+ identifier,
240
+ asString(resourceBuffer)
241
+ );
242
+ }
243
+
244
+ if (this.useSourceMap && sourceMap) {
245
+ return new SourceMapSource(source, identifier, sourceMap);
246
+ }
247
+
248
+ if (Buffer.isBuffer(source)) {
249
+ // @ts-ignore
250
+ // TODO We need to fix @types/webpack-sources to allow RawSource to take a Buffer | string
251
+ return new RawSource(source);
252
+ }
253
+
254
+ return new OriginalSource(source, identifier);
255
+ }
256
+
257
+ doBuild(options, compilation, resolver, fs, callback) {
258
+ const loaderContext = this.createLoaderContext(
259
+ resolver,
260
+ options,
261
+ compilation,
262
+ fs
263
+ );
264
+
265
+ runLoaders(
266
+ {
267
+ resource: this.resource,
268
+ loaders: this.loaders,
269
+ context: loaderContext,
270
+ readResource: fs.readFile.bind(fs)
271
+ },
272
+ (err, result) => {
273
+ if (result) {
274
+ this.buildInfo.cacheable = result.cacheable;
275
+ this.buildInfo.fileDependencies = new Set(result.fileDependencies);
276
+ this.buildInfo.contextDependencies = new Set(
277
+ result.contextDependencies
278
+ );
279
+ }
280
+
281
+ if (err) {
282
+ if (!(err instanceof Error)) {
283
+ err = new NonErrorEmittedError(err);
284
+ }
285
+ const currentLoader = this.getCurrentLoader(loaderContext);
286
+ const error = new ModuleBuildError(this, err, {
287
+ from:
288
+ currentLoader &&
289
+ compilation.runtimeTemplate.requestShortener.shorten(
290
+ currentLoader.loader
291
+ )
292
+ });
293
+ return callback(error);
294
+ }
295
+
296
+ const resourceBuffer = result.resourceBuffer;
297
+ const source = result.result[0];
298
+ const sourceMap = result.result.length >= 1 ? result.result[1] : null;
299
+ const extraInfo = result.result.length >= 2 ? result.result[2] : null;
300
+
301
+ if (!Buffer.isBuffer(source) && typeof source !== "string") {
302
+ const currentLoader = this.getCurrentLoader(loaderContext, 0);
303
+ const err = new Error(
304
+ `Final loader (${
305
+ currentLoader
306
+ ? compilation.runtimeTemplate.requestShortener.shorten(
307
+ currentLoader.loader
308
+ )
309
+ : "unknown"
310
+ }) didn't return a Buffer or String`
311
+ );
312
+ const error = new ModuleBuildError(this, err);
313
+ return callback(error);
314
+ }
315
+
316
+ this._source = this.createSource(
317
+ this.binary ? asBuffer(source) : asString(source),
318
+ resourceBuffer,
319
+ sourceMap
320
+ );
321
+ this._ast =
322
+ typeof extraInfo === "object" &&
323
+ extraInfo !== null &&
324
+ extraInfo.webpackAST !== undefined
325
+ ? extraInfo.webpackAST
326
+ : null;
327
+ return callback();
328
+ }
329
+ );
330
+ }
331
+
332
+ markModuleAsErrored(error) {
333
+ // Restore build meta from successful build to keep importing state
334
+ this.buildMeta = Object.assign({}, this._lastSuccessfulBuildMeta);
335
+
336
+ this.error = error;
337
+ this.errors.push(this.error);
338
+ this._source = new RawSource(
339
+ "throw new Error(" + JSON.stringify(this.error.message) + ");"
340
+ );
341
+ this._ast = null;
342
+ }
343
+
344
+ applyNoParseRule(rule, content) {
345
+ // must start with "rule" if rule is a string
346
+ if (typeof rule === "string") {
347
+ return content.indexOf(rule) === 0;
348
+ }
349
+
350
+ if (typeof rule === "function") {
351
+ return rule(content);
352
+ }
353
+ // we assume rule is a regexp
354
+ return rule.test(content);
355
+ }
356
+
357
+ // check if module should not be parsed
358
+ // returns "true" if the module should !not! be parsed
359
+ // returns "false" if the module !must! be parsed
360
+ shouldPreventParsing(noParseRule, request) {
361
+ // if no noParseRule exists, return false
362
+ // the module !must! be parsed.
363
+ if (!noParseRule) {
364
+ return false;
365
+ }
366
+
367
+ // we only have one rule to check
368
+ if (!Array.isArray(noParseRule)) {
369
+ // returns "true" if the module is !not! to be parsed
370
+ return this.applyNoParseRule(noParseRule, request);
371
+ }
372
+
373
+ for (let i = 0; i < noParseRule.length; i++) {
374
+ const rule = noParseRule[i];
375
+ // early exit on first truthy match
376
+ // this module is !not! to be parsed
377
+ if (this.applyNoParseRule(rule, request)) {
378
+ return true;
379
+ }
380
+ }
381
+ // no match found, so this module !should! be parsed
382
+ return false;
383
+ }
384
+
385
+ _initBuildHash(compilation) {
386
+ const hash = createHash(compilation.outputOptions.hashFunction);
387
+ if (this._source) {
388
+ hash.update("source");
389
+ this._source.updateHash(hash);
390
+ }
391
+ hash.update("meta");
392
+ hash.update(JSON.stringify(this.buildMeta));
393
+ this._buildHash = hash.digest("hex");
394
+ }
395
+
396
+ build(options, compilation, resolver, fs, callback) {
397
+ this.buildTimestamp = Date.now();
398
+ this.built = true;
399
+ this._source = null;
400
+ this._ast = null;
401
+ this._buildHash = "";
402
+ this.error = null;
403
+ this.errors.length = 0;
404
+ this.warnings.length = 0;
405
+ this.buildMeta = {};
406
+ this.buildInfo = {
407
+ cacheable: false,
408
+ fileDependencies: new Set(),
409
+ contextDependencies: new Set()
410
+ };
411
+
412
+ return this.doBuild(options, compilation, resolver, fs, err => {
413
+ this._cachedSources.clear();
414
+
415
+ // if we have an error mark module as failed and exit
416
+ if (err) {
417
+ this.markModuleAsErrored(err);
418
+ this._initBuildHash(compilation);
419
+ return callback();
420
+ }
421
+
422
+ // check if this module should !not! be parsed.
423
+ // if so, exit here;
424
+ const noParseRule = options.module && options.module.noParse;
425
+ if (this.shouldPreventParsing(noParseRule, this.request)) {
426
+ this._initBuildHash(compilation);
427
+ return callback();
428
+ }
429
+
430
+ const handleParseError = e => {
431
+ const source = this._source.source();
432
+ const error = new ModuleParseError(this, source, e);
433
+ this.markModuleAsErrored(error);
434
+ this._initBuildHash(compilation);
435
+ return callback();
436
+ };
437
+
438
+ const handleParseResult = result => {
439
+ this._lastSuccessfulBuildMeta = this.buildMeta;
440
+ this._initBuildHash(compilation);
441
+ return callback();
442
+ };
443
+
444
+ try {
445
+ const result = this.parser.parse(
446
+ this._ast || this._source.source(),
447
+ {
448
+ current: this,
449
+ module: this,
450
+ compilation: compilation,
451
+ options: options
452
+ },
453
+ (err, result) => {
454
+ if (err) {
455
+ handleParseError(err);
456
+ } else {
457
+ handleParseResult(result);
458
+ }
459
+ }
460
+ );
461
+ if (result !== undefined) {
462
+ // parse is sync
463
+ handleParseResult(result);
464
+ }
465
+ } catch (e) {
466
+ handleParseError(e);
467
+ }
468
+ });
469
+ }
470
+
471
+ getHashDigest(dependencyTemplates) {
472
+ // TODO webpack 5 refactor
473
+ let dtHash = dependencyTemplates.get("hash");
474
+ return `${this.hash}-${dtHash}`;
475
+ }
476
+
477
+ source(dependencyTemplates, runtimeTemplate, type = "javascript") {
478
+ const hashDigest = this.getHashDigest(dependencyTemplates);
479
+ const cacheEntry = this._cachedSources.get(type);
480
+ if (cacheEntry !== undefined && cacheEntry.hash === hashDigest) {
481
+ // We can reuse the cached source
482
+ return cacheEntry.source;
483
+ }
484
+
485
+ const source = this.generator.generate(
486
+ this,
487
+ dependencyTemplates,
488
+ runtimeTemplate,
489
+ type
490
+ );
491
+
492
+ const cachedSource = new CachedSource(source);
493
+ this._cachedSources.set(type, {
494
+ source: cachedSource,
495
+ hash: hashDigest
496
+ });
497
+ return cachedSource;
498
+ }
499
+
500
+ originalSource() {
501
+ return this._source;
502
+ }
503
+
504
+ needRebuild(fileTimestamps, contextTimestamps) {
505
+ // always try to rebuild in case of an error
506
+ if (this.error) return true;
507
+
508
+ // always rebuild when module is not cacheable
509
+ if (!this.buildInfo.cacheable) return true;
510
+
511
+ // Check timestamps of all dependencies
512
+ // Missing timestamp -> need rebuild
513
+ // Timestamp bigger than buildTimestamp -> need rebuild
514
+ for (const file of this.buildInfo.fileDependencies) {
515
+ const timestamp = fileTimestamps.get(file);
516
+ if (!timestamp) return true;
517
+ if (timestamp >= this.buildTimestamp) return true;
518
+ }
519
+ for (const file of this.buildInfo.contextDependencies) {
520
+ const timestamp = contextTimestamps.get(file);
521
+ if (!timestamp) return true;
522
+ if (timestamp >= this.buildTimestamp) return true;
523
+ }
524
+ // elsewise -> no rebuild needed
525
+ return false;
526
+ }
527
+
528
+ size() {
529
+ return this._source ? this._source.size() : -1;
530
+ }
531
+
532
+ /**
533
+ * @param {Hash} hash the hash used to track dependencies
534
+ * @returns {void}
535
+ */
536
+ updateHash(hash) {
537
+ hash.update(this._buildHash);
538
+ super.updateHash(hash);
539
+ }
540
+ }
541
+
542
+ module.exports = NormalModule;