webpack 3.5.2 → 3.5.6

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.
@@ -1,555 +1,556 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- "use strict";
6
-
7
- const path = require("path");
8
- const NativeModule = require("module");
9
- const crypto = require("crypto");
10
-
11
- const SourceMapSource = require("webpack-sources").SourceMapSource;
12
- const OriginalSource = require("webpack-sources").OriginalSource;
13
- const RawSource = require("webpack-sources").RawSource;
14
- const ReplaceSource = require("webpack-sources").ReplaceSource;
15
- const CachedSource = require("webpack-sources").CachedSource;
16
- const LineToLineMappedSource = require("webpack-sources").LineToLineMappedSource;
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
-
25
- const runLoaders = require("loader-runner").runLoaders;
26
- const getContext = require("loader-runner").getContext;
27
-
28
- function asString(buf) {
29
- if(Buffer.isBuffer(buf)) {
30
- return buf.toString("utf-8");
31
- }
32
- return buf;
33
- }
34
-
35
- function contextify(context, request) {
36
- return request.split("!").map(function(r) {
37
- let rp = path.relative(context, r);
38
- if(path.sep === "\\")
39
- rp = rp.replace(/\\/g, "/");
40
- if(rp.indexOf("../") !== 0)
41
- rp = "./" + rp;
42
- return rp;
43
- }).join("!");
44
- }
45
-
46
- class NonErrorEmittedError extends WebpackError {
47
- constructor(error) {
48
- super();
49
-
50
- this.name = "NonErrorEmittedError";
51
- this.message = "(Emitted value instead of an instance of Error) " + error;
52
-
53
- Error.captureStackTrace(this, this.constructor);
54
- }
55
- }
56
-
57
- const dependencyTemplatesHashMap = new WeakMap();
58
-
59
- class NormalModule extends Module {
60
-
61
- constructor(request, userRequest, rawRequest, loaders, resource, parser) {
62
- super();
63
- this.request = request;
64
- this.userRequest = userRequest;
65
- this.rawRequest = rawRequest;
66
- this.parser = parser;
67
- this.resource = resource;
68
- this.context = getContext(resource);
69
- this.loaders = loaders;
70
- this.fileDependencies = [];
71
- this.contextDependencies = [];
72
- this.warnings = [];
73
- this.errors = [];
74
- this.error = null;
75
- this._source = null;
76
- this.assets = {};
77
- this.built = false;
78
- this._cachedSource = null;
79
- }
80
-
81
- identifier() {
82
- return this.request;
83
- }
84
-
85
- readableIdentifier(requestShortener) {
86
- return requestShortener.shorten(this.userRequest);
87
- }
88
-
89
- libIdent(options) {
90
- return contextify(options.context, this.userRequest);
91
- }
92
-
93
- nameForCondition() {
94
- const idx = this.resource.indexOf("?");
95
- if(idx >= 0) return this.resource.substr(0, idx);
96
- return this.resource;
97
- }
98
-
99
- createSourceForAsset(name, content, sourceMap) {
100
- if(!sourceMap) {
101
- return new RawSource(content);
102
- }
103
-
104
- if(typeof sourceMap === "string") {
105
- return new OriginalSource(content, sourceMap);
106
- }
107
-
108
- return new SourceMapSource(content, name, sourceMap);
109
- }
110
-
111
- createLoaderContext(resolver, options, compilation, fs) {
112
- const loaderContext = {
113
- version: 2,
114
- emitWarning: (warning) => {
115
- if(!(warning instanceof Error))
116
- warning = new NonErrorEmittedError(warning);
117
- this.warnings.push(new ModuleWarning(this, warning));
118
- },
119
- emitError: (error) => {
120
- if(!(error instanceof Error))
121
- error = new NonErrorEmittedError(error);
122
- this.errors.push(new ModuleError(this, error));
123
- },
124
- exec: (code, filename) => {
125
- const module = new NativeModule(filename, this);
126
- module.paths = NativeModule._nodeModulePaths(this.context);
127
- module.filename = filename;
128
- module._compile(code, filename);
129
- return module.exports;
130
- },
131
- resolve(context, request, callback) {
132
- resolver.resolve({}, context, request, callback);
133
- },
134
- resolveSync(context, request) {
135
- return resolver.resolveSync({}, context, request);
136
- },
137
- emitFile: (name, content, sourceMap) => {
138
- this.assets[name] = this.createSourceForAsset(name, content, sourceMap);
139
- },
140
- options: options,
141
- webpack: true,
142
- sourceMap: !!this.useSourceMap,
143
- _module: this,
144
- _compilation: compilation,
145
- _compiler: compilation.compiler,
146
- fs: fs,
147
- };
148
-
149
- compilation.applyPlugins("normal-module-loader", loaderContext, this);
150
- if(options.loader)
151
- Object.assign(loaderContext, options.loader);
152
-
153
- return loaderContext;
154
- }
155
-
156
- createSource(source, resourceBuffer, sourceMap) {
157
- // if there is no identifier return raw source
158
- if(!this.identifier) {
159
- return new RawSource(source);
160
- }
161
-
162
- // from here on we assume we have an identifier
163
- const identifier = this.identifier();
164
-
165
- if(this.lineToLine && resourceBuffer) {
166
- return new LineToLineMappedSource(
167
- source, identifier, asString(resourceBuffer));
168
- }
169
-
170
- if(this.useSourceMap && sourceMap) {
171
- return new SourceMapSource(source, identifier, sourceMap);
172
- }
173
-
174
- return new OriginalSource(source, identifier);
175
- }
176
-
177
- doBuild(options, compilation, resolver, fs, callback) {
178
- this.cacheable = false;
179
- const loaderContext = this.createLoaderContext(resolver, options, compilation, fs);
180
-
181
- runLoaders({
182
- resource: this.resource,
183
- loaders: this.loaders,
184
- context: loaderContext,
185
- readResource: fs.readFile.bind(fs)
186
- }, (err, result) => {
187
- if(result) {
188
- this.cacheable = result.cacheable;
189
- this.fileDependencies = result.fileDependencies;
190
- this.contextDependencies = result.contextDependencies;
191
- }
192
-
193
- if(err) {
194
- const error = new ModuleBuildError(this, err);
195
- return callback(error);
196
- }
197
-
198
- const resourceBuffer = result.resourceBuffer;
199
- const source = result.result[0];
200
- const sourceMap = result.result[1];
201
-
202
- if(!Buffer.isBuffer(source) && typeof source !== "string") {
203
- const error = new ModuleBuildError(this, new Error("Final loader didn't return a Buffer or String"));
204
- return callback(error);
205
- }
206
-
207
- this._source = this.createSource(asString(source), resourceBuffer, sourceMap);
208
- return callback();
209
- });
210
- }
211
-
212
- disconnect() {
213
- this.built = false;
214
- super.disconnect();
215
- }
216
-
217
- markModuleAsErrored(error) {
218
- this.meta = null;
219
- this.error = error;
220
- this.errors.push(this.error);
221
- this._source = new RawSource("throw new Error(" + JSON.stringify(this.error.message) + ");");
222
- }
223
-
224
- applyNoParseRule(rule, content) {
225
- // must start with "rule" if rule is a string
226
- if(typeof rule === "string") {
227
- return content.indexOf(rule) === 0;
228
- }
229
-
230
- if(typeof rule === "function") {
231
- return rule(content);
232
- }
233
- // we assume rule is a regexp
234
- return rule.test(content);
235
- }
236
-
237
- // check if module should not be parsed
238
- // returns "true" if the module should !not! be parsed
239
- // returns "false" if the module !must! be parsed
240
- shouldPreventParsing(noParseRule, request) {
241
- // if no noParseRule exists, return false
242
- // the module !must! be parsed.
243
- if(!noParseRule) {
244
- return false;
245
- }
246
-
247
- // we only have one rule to check
248
- if(!Array.isArray(noParseRule)) {
249
- // returns "true" if the module is !not! to be parsed
250
- return this.applyNoParseRule(noParseRule, request);
251
- }
252
-
253
- for(let i = 0; i < noParseRule.length; i++) {
254
- const rule = noParseRule[i];
255
- // early exit on first truthy match
256
- // this module is !not! to be parsed
257
- if(this.applyNoParseRule(rule, request)) {
258
- return true;
259
- }
260
- }
261
- // no match found, so this module !should! be parsed
262
- return false;
263
- }
264
-
265
- build(options, compilation, resolver, fs, callback) {
266
- this.buildTimestamp = Date.now();
267
- this.built = true;
268
- this._source = null;
269
- this.error = null;
270
- this.errors.length = 0;
271
- this.warnings.length = 0;
272
- this.meta = {};
273
-
274
- return this.doBuild(options, compilation, resolver, fs, (err) => {
275
- this.dependencies.length = 0;
276
- this.variables.length = 0;
277
- this.blocks.length = 0;
278
- this._cachedSource = null;
279
-
280
- // if we have an error mark module as failed and exit
281
- if(err) {
282
- this.markModuleAsErrored(err);
283
- return callback();
284
- }
285
-
286
- // check if this module should !not! be parsed.
287
- // if so, exit here;
288
- const noParseRule = options.module && options.module.noParse;
289
- if(this.shouldPreventParsing(noParseRule, this.request)) {
290
- return callback();
291
- }
292
-
293
- try {
294
- this.parser.parse(this._source.source(), {
295
- current: this,
296
- module: this,
297
- compilation: compilation,
298
- options: options
299
- });
300
- } catch(e) {
301
- const source = this._source.source();
302
- const error = new ModuleParseError(this, source, e);
303
- this.markModuleAsErrored(error);
304
- return callback();
305
- }
306
- return callback();
307
- });
308
- }
309
-
310
- getHashDigest(dependencyTemplates) {
311
- let dtHash = dependencyTemplatesHashMap.get("hash");
312
- const hash = crypto.createHash("md5");
313
- this.updateHash(hash);
314
- hash.update(`${dtHash}`);
315
- return hash.digest("hex");
316
- }
317
-
318
- sourceDependency(dependency, dependencyTemplates, source, outputOptions, requestShortener) {
319
- const template = dependencyTemplates.get(dependency.constructor);
320
- if(!template) throw new Error("No template for dependency: " + dependency.constructor.name);
321
- template.apply(dependency, source, outputOptions, requestShortener, dependencyTemplates);
322
- }
323
-
324
- sourceVariables(variable, availableVars, dependencyTemplates, outputOptions, requestShortener) {
325
- const name = variable.name;
326
- const expr = variable.expressionSource(dependencyTemplates, outputOptions, requestShortener);
327
-
328
- if(availableVars.some(v => v.name === name && v.expression.source() === expr.source())) {
329
- return;
330
- }
331
- return {
332
- name: name,
333
- expression: expr
334
- };
335
- }
336
-
337
- /*
338
- * creates the start part of a IIFE around the module to inject a variable name
339
- * (function(...){ <- this part
340
- * }.call(...))
341
- */
342
- variableInjectionFunctionWrapperStartCode(varNames) {
343
- const args = varNames.join(", ");
344
- return `/* WEBPACK VAR INJECTION */(function(${args}) {`;
345
- }
346
-
347
- contextArgument(block) {
348
- if(this === block) {
349
- return this.exportsArgument || "exports";
350
- }
351
- return "this";
352
- }
353
-
354
- /*
355
- * creates the end part of a IIFE around the module to inject a variable name
356
- * (function(...){
357
- * }.call(...)) <- this part
358
- */
359
- variableInjectionFunctionWrapperEndCode(varExpressions, block) {
360
- const firstParam = this.contextArgument(block);
361
- const furtherParams = varExpressions.map(e => e.source()).join(", ");
362
- return `}.call(${firstParam}, ${furtherParams}))`;
363
- }
364
-
365
- splitVariablesInUniqueNamedChunks(vars) {
366
- const startState = [
367
- []
368
- ];
369
- return vars.reduce((chunks, variable) => {
370
- const current = chunks[chunks.length - 1];
371
- // check if variable with same name exists already
372
- // if so create a new chunk of variables.
373
- const variableNameAlreadyExists = current.some(v => v.name === variable.name);
374
-
375
- if(variableNameAlreadyExists) {
376
- // start new chunk with current variable
377
- chunks.push([variable]);
378
- } else {
379
- // else add it to current chunk
380
- current.push(variable);
381
- }
382
- return chunks;
383
- }, startState);
384
- }
385
-
386
- sourceBlock(block, availableVars, dependencyTemplates, source, outputOptions, requestShortener) {
387
- block.dependencies.forEach((dependency) => this.sourceDependency(
388
- dependency, dependencyTemplates, source, outputOptions, requestShortener));
389
-
390
- /**
391
- * Get the variables of all blocks that we need to inject.
392
- * These will contain the variable name and its expression.
393
- * The name will be added as a paramter in a IIFE the expression as its value.
394
- */
395
- const vars = block.variables.reduce((result, value) => {
396
- const variable = this.sourceVariables(
397
- value, availableVars, dependencyTemplates, outputOptions, requestShortener);
398
-
399
- if(variable) {
400
- result.push(variable);
401
- }
402
-
403
- return result;
404
- }, []);
405
-
406
- /**
407
- * if we actually have variables
408
- * this is important as how #splitVariablesInUniqueNamedChunks works
409
- * it will always return an array in an array which would lead to a IIFE wrapper around
410
- * a module if we do this with an empty vars array.
411
- */
412
- if(vars.length > 0) {
413
- /**
414
- * Split all variables up into chunks of unique names.
415
- * e.g. imagine you have the following variable names that need to be injected:
416
- * [foo, bar, baz, foo, some, more]
417
- * we can not inject "foo" twice, therefore we just make two IIFEs like so:
418
- * (function(foo, bar, baz){
419
- * (function(foo, some, more){
420
- * ...
421
- * }(...));
422
- * }(...));
423
- *
424
- * "splitVariablesInUniqueNamedChunks" splits the variables shown above up to this:
425
- * [[foo, bar, baz], [foo, some, more]]
426
- */
427
- const injectionVariableChunks = this.splitVariablesInUniqueNamedChunks(vars);
428
-
429
- // create all the beginnings of IIFEs
430
- const functionWrapperStarts = injectionVariableChunks.map((variableChunk) => {
431
- return this.variableInjectionFunctionWrapperStartCode(
432
- variableChunk.map(variable => variable.name)
433
- );
434
- });
435
-
436
- // and all the ends
437
- const functionWrapperEnds = injectionVariableChunks.map((variableChunk) => {
438
- return this.variableInjectionFunctionWrapperEndCode(
439
- variableChunk.map(variable => variable.expression), block
440
- );
441
- });
442
-
443
- // join them to one big string
444
- const varStartCode = functionWrapperStarts.join("");
445
-
446
- // reverse the ends first before joining them, as the last added must be the inner most
447
- const varEndCode = functionWrapperEnds.reverse().join("");
448
-
449
- // if we have anything, add it to the source
450
- if(varStartCode && varEndCode) {
451
- const start = block.range ? block.range[0] : -10;
452
- const end = block.range ? block.range[1] : (this._source.size() + 1);
453
- source.insert(start + 0.5, varStartCode);
454
- source.insert(end + 0.5, "\n/* WEBPACK VAR INJECTION */" + varEndCode);
455
- }
456
- }
457
-
458
- block.blocks.forEach((block) =>
459
- this.sourceBlock(
460
- block,
461
- availableVars.concat(vars),
462
- dependencyTemplates,
463
- source,
464
- outputOptions,
465
- requestShortener
466
- )
467
- );
468
- }
469
-
470
- source(dependencyTemplates, outputOptions, requestShortener) {
471
- const hashDigest = this.getHashDigest(dependencyTemplates);
472
- if(this._cachedSource && this._cachedSource.hash === hashDigest) {
473
- return this._cachedSource.source;
474
- }
475
-
476
- if(!this._source) {
477
- return new RawSource("throw new Error('No source available');");
478
- }
479
-
480
- const source = new ReplaceSource(this._source);
481
- this._cachedSource = {
482
- source: source,
483
- hash: hashDigest
484
- };
485
-
486
- this.sourceBlock(this, [], dependencyTemplates, source, outputOptions, requestShortener);
487
- return new CachedSource(source);
488
- }
489
-
490
- originalSource() {
491
- return this._source;
492
- }
493
-
494
- getHighestTimestamp(keys, timestampsByKey) {
495
- let highestTimestamp = 0;
496
- for(let i = 0; i < keys.length; i++) {
497
- const key = keys[i];
498
- const timestamp = timestampsByKey[key];
499
- // if there is no timestamp yet, early return with Infinity
500
- if(!timestamp) return Infinity;
501
- highestTimestamp = Math.max(highestTimestamp, timestamp);
502
- }
503
- return highestTimestamp;
504
- }
505
-
506
- needRebuild(fileTimestamps, contextTimestamps) {
507
- const highestFileDepTimestamp = this.getHighestTimestamp(
508
- this.fileDependencies, fileTimestamps);
509
- // if the hightest is Infinity, we need a rebuild
510
- // exit early here.
511
- if(highestFileDepTimestamp === Infinity) {
512
- return true;
513
- }
514
-
515
- const highestContextDepTimestamp = this.getHighestTimestamp(
516
- this.contextDependencies, contextTimestamps);
517
-
518
- // Again if the hightest is Infinity, we need a rebuild
519
- // exit early here.
520
- if(highestContextDepTimestamp === Infinity) {
521
- return true;
522
- }
523
-
524
- // else take the highest of file and context timestamps and compare
525
- // to last build timestamp
526
- return Math.max(highestContextDepTimestamp, highestFileDepTimestamp) >= this.buildTimestamp;
527
- }
528
-
529
- size() {
530
- return this._source ? this._source.size() : -1;
531
- }
532
-
533
- updateHashWithSource(hash) {
534
- if(!this._source) {
535
- hash.update("null");
536
- return;
537
- }
538
- hash.update("source");
539
- this._source.updateHash(hash);
540
- }
541
-
542
- updateHashWithMeta(hash) {
543
- hash.update("meta");
544
- hash.update(JSON.stringify(this.meta));
545
- }
546
-
547
- updateHash(hash) {
548
- this.updateHashWithSource(hash);
549
- this.updateHashWithMeta(hash);
550
- super.updateHash(hash);
551
- }
552
-
553
- }
554
-
555
- 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 path = require("path");
8
+ const NativeModule = require("module");
9
+ const crypto = require("crypto");
10
+
11
+ const SourceMapSource = require("webpack-sources").SourceMapSource;
12
+ const OriginalSource = require("webpack-sources").OriginalSource;
13
+ const RawSource = require("webpack-sources").RawSource;
14
+ const ReplaceSource = require("webpack-sources").ReplaceSource;
15
+ const CachedSource = require("webpack-sources").CachedSource;
16
+ const LineToLineMappedSource = require("webpack-sources").LineToLineMappedSource;
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
+
25
+ const runLoaders = require("loader-runner").runLoaders;
26
+ const getContext = require("loader-runner").getContext;
27
+
28
+ function asString(buf) {
29
+ if(Buffer.isBuffer(buf)) {
30
+ return buf.toString("utf-8");
31
+ }
32
+ return buf;
33
+ }
34
+
35
+ function contextify(context, request) {
36
+ return request.split("!").map(function(r) {
37
+ const splitPath = r.split("?");
38
+ splitPath[0] = path.relative(context, splitPath[0]);
39
+ if(path.sep === "\\")
40
+ splitPath[0] = splitPath[0].replace(/\\/g, "/");
41
+ if(splitPath[0].indexOf("../") !== 0)
42
+ splitPath[0] = "./" + splitPath[0];
43
+ return splitPath.join("?");
44
+ }).join("!");
45
+ }
46
+
47
+ class NonErrorEmittedError extends WebpackError {
48
+ constructor(error) {
49
+ super();
50
+
51
+ this.name = "NonErrorEmittedError";
52
+ this.message = "(Emitted value instead of an instance of Error) " + error;
53
+
54
+ Error.captureStackTrace(this, this.constructor);
55
+ }
56
+ }
57
+
58
+ const dependencyTemplatesHashMap = new WeakMap();
59
+
60
+ class NormalModule extends Module {
61
+
62
+ constructor(request, userRequest, rawRequest, loaders, resource, parser) {
63
+ super();
64
+ this.request = request;
65
+ this.userRequest = userRequest;
66
+ this.rawRequest = rawRequest;
67
+ this.parser = parser;
68
+ this.resource = resource;
69
+ this.context = getContext(resource);
70
+ this.loaders = loaders;
71
+ this.fileDependencies = [];
72
+ this.contextDependencies = [];
73
+ this.warnings = [];
74
+ this.errors = [];
75
+ this.error = null;
76
+ this._source = null;
77
+ this.assets = {};
78
+ this.built = false;
79
+ this._cachedSource = null;
80
+ }
81
+
82
+ identifier() {
83
+ return this.request;
84
+ }
85
+
86
+ readableIdentifier(requestShortener) {
87
+ return requestShortener.shorten(this.userRequest);
88
+ }
89
+
90
+ libIdent(options) {
91
+ return contextify(options.context, this.userRequest);
92
+ }
93
+
94
+ nameForCondition() {
95
+ const idx = this.resource.indexOf("?");
96
+ if(idx >= 0) return this.resource.substr(0, idx);
97
+ return this.resource;
98
+ }
99
+
100
+ createSourceForAsset(name, content, sourceMap) {
101
+ if(!sourceMap) {
102
+ return new RawSource(content);
103
+ }
104
+
105
+ if(typeof sourceMap === "string") {
106
+ return new OriginalSource(content, sourceMap);
107
+ }
108
+
109
+ return new SourceMapSource(content, name, sourceMap);
110
+ }
111
+
112
+ createLoaderContext(resolver, options, compilation, fs) {
113
+ const loaderContext = {
114
+ version: 2,
115
+ emitWarning: (warning) => {
116
+ if(!(warning instanceof Error))
117
+ warning = new NonErrorEmittedError(warning);
118
+ this.warnings.push(new ModuleWarning(this, warning));
119
+ },
120
+ emitError: (error) => {
121
+ if(!(error instanceof Error))
122
+ error = new NonErrorEmittedError(error);
123
+ this.errors.push(new ModuleError(this, error));
124
+ },
125
+ exec: (code, filename) => {
126
+ const module = new NativeModule(filename, this);
127
+ module.paths = NativeModule._nodeModulePaths(this.context);
128
+ module.filename = filename;
129
+ module._compile(code, filename);
130
+ return module.exports;
131
+ },
132
+ resolve(context, request, callback) {
133
+ resolver.resolve({}, context, request, callback);
134
+ },
135
+ resolveSync(context, request) {
136
+ return resolver.resolveSync({}, context, request);
137
+ },
138
+ emitFile: (name, content, sourceMap) => {
139
+ this.assets[name] = this.createSourceForAsset(name, content, sourceMap);
140
+ },
141
+ options: options,
142
+ webpack: true,
143
+ sourceMap: !!this.useSourceMap,
144
+ _module: this,
145
+ _compilation: compilation,
146
+ _compiler: compilation.compiler,
147
+ fs: fs,
148
+ };
149
+
150
+ compilation.applyPlugins("normal-module-loader", loaderContext, this);
151
+ if(options.loader)
152
+ Object.assign(loaderContext, options.loader);
153
+
154
+ return loaderContext;
155
+ }
156
+
157
+ createSource(source, resourceBuffer, sourceMap) {
158
+ // if there is no identifier return raw source
159
+ if(!this.identifier) {
160
+ return new RawSource(source);
161
+ }
162
+
163
+ // from here on we assume we have an identifier
164
+ const identifier = this.identifier();
165
+
166
+ if(this.lineToLine && resourceBuffer) {
167
+ return new LineToLineMappedSource(
168
+ source, identifier, asString(resourceBuffer));
169
+ }
170
+
171
+ if(this.useSourceMap && sourceMap) {
172
+ return new SourceMapSource(source, identifier, sourceMap);
173
+ }
174
+
175
+ return new OriginalSource(source, identifier);
176
+ }
177
+
178
+ doBuild(options, compilation, resolver, fs, callback) {
179
+ this.cacheable = false;
180
+ const loaderContext = this.createLoaderContext(resolver, options, compilation, fs);
181
+
182
+ runLoaders({
183
+ resource: this.resource,
184
+ loaders: this.loaders,
185
+ context: loaderContext,
186
+ readResource: fs.readFile.bind(fs)
187
+ }, (err, result) => {
188
+ if(result) {
189
+ this.cacheable = result.cacheable;
190
+ this.fileDependencies = result.fileDependencies;
191
+ this.contextDependencies = result.contextDependencies;
192
+ }
193
+
194
+ if(err) {
195
+ const error = new ModuleBuildError(this, err);
196
+ return callback(error);
197
+ }
198
+
199
+ const resourceBuffer = result.resourceBuffer;
200
+ const source = result.result[0];
201
+ const sourceMap = result.result[1];
202
+
203
+ if(!Buffer.isBuffer(source) && typeof source !== "string") {
204
+ const error = new ModuleBuildError(this, new Error("Final loader didn't return a Buffer or String"));
205
+ return callback(error);
206
+ }
207
+
208
+ this._source = this.createSource(asString(source), resourceBuffer, sourceMap);
209
+ return callback();
210
+ });
211
+ }
212
+
213
+ disconnect() {
214
+ this.built = false;
215
+ super.disconnect();
216
+ }
217
+
218
+ markModuleAsErrored(error) {
219
+ this.meta = null;
220
+ this.error = error;
221
+ this.errors.push(this.error);
222
+ this._source = new RawSource("throw new Error(" + JSON.stringify(this.error.message) + ");");
223
+ }
224
+
225
+ applyNoParseRule(rule, content) {
226
+ // must start with "rule" if rule is a string
227
+ if(typeof rule === "string") {
228
+ return content.indexOf(rule) === 0;
229
+ }
230
+
231
+ if(typeof rule === "function") {
232
+ return rule(content);
233
+ }
234
+ // we assume rule is a regexp
235
+ return rule.test(content);
236
+ }
237
+
238
+ // check if module should not be parsed
239
+ // returns "true" if the module should !not! be parsed
240
+ // returns "false" if the module !must! be parsed
241
+ shouldPreventParsing(noParseRule, request) {
242
+ // if no noParseRule exists, return false
243
+ // the module !must! be parsed.
244
+ if(!noParseRule) {
245
+ return false;
246
+ }
247
+
248
+ // we only have one rule to check
249
+ if(!Array.isArray(noParseRule)) {
250
+ // returns "true" if the module is !not! to be parsed
251
+ return this.applyNoParseRule(noParseRule, request);
252
+ }
253
+
254
+ for(let i = 0; i < noParseRule.length; i++) {
255
+ const rule = noParseRule[i];
256
+ // early exit on first truthy match
257
+ // this module is !not! to be parsed
258
+ if(this.applyNoParseRule(rule, request)) {
259
+ return true;
260
+ }
261
+ }
262
+ // no match found, so this module !should! be parsed
263
+ return false;
264
+ }
265
+
266
+ build(options, compilation, resolver, fs, callback) {
267
+ this.buildTimestamp = Date.now();
268
+ this.built = true;
269
+ this._source = null;
270
+ this.error = null;
271
+ this.errors.length = 0;
272
+ this.warnings.length = 0;
273
+ this.meta = {};
274
+
275
+ return this.doBuild(options, compilation, resolver, fs, (err) => {
276
+ this.dependencies.length = 0;
277
+ this.variables.length = 0;
278
+ this.blocks.length = 0;
279
+ this._cachedSource = null;
280
+
281
+ // if we have an error mark module as failed and exit
282
+ if(err) {
283
+ this.markModuleAsErrored(err);
284
+ return callback();
285
+ }
286
+
287
+ // check if this module should !not! be parsed.
288
+ // if so, exit here;
289
+ const noParseRule = options.module && options.module.noParse;
290
+ if(this.shouldPreventParsing(noParseRule, this.request)) {
291
+ return callback();
292
+ }
293
+
294
+ try {
295
+ this.parser.parse(this._source.source(), {
296
+ current: this,
297
+ module: this,
298
+ compilation: compilation,
299
+ options: options
300
+ });
301
+ } catch(e) {
302
+ const source = this._source.source();
303
+ const error = new ModuleParseError(this, source, e);
304
+ this.markModuleAsErrored(error);
305
+ return callback();
306
+ }
307
+ return callback();
308
+ });
309
+ }
310
+
311
+ getHashDigest(dependencyTemplates) {
312
+ let dtHash = dependencyTemplatesHashMap.get("hash");
313
+ const hash = crypto.createHash("md5");
314
+ this.updateHash(hash);
315
+ hash.update(`${dtHash}`);
316
+ return hash.digest("hex");
317
+ }
318
+
319
+ sourceDependency(dependency, dependencyTemplates, source, outputOptions, requestShortener) {
320
+ const template = dependencyTemplates.get(dependency.constructor);
321
+ if(!template) throw new Error("No template for dependency: " + dependency.constructor.name);
322
+ template.apply(dependency, source, outputOptions, requestShortener, dependencyTemplates);
323
+ }
324
+
325
+ sourceVariables(variable, availableVars, dependencyTemplates, outputOptions, requestShortener) {
326
+ const name = variable.name;
327
+ const expr = variable.expressionSource(dependencyTemplates, outputOptions, requestShortener);
328
+
329
+ if(availableVars.some(v => v.name === name && v.expression.source() === expr.source())) {
330
+ return;
331
+ }
332
+ return {
333
+ name: name,
334
+ expression: expr
335
+ };
336
+ }
337
+
338
+ /*
339
+ * creates the start part of a IIFE around the module to inject a variable name
340
+ * (function(...){ <- this part
341
+ * }.call(...))
342
+ */
343
+ variableInjectionFunctionWrapperStartCode(varNames) {
344
+ const args = varNames.join(", ");
345
+ return `/* WEBPACK VAR INJECTION */(function(${args}) {`;
346
+ }
347
+
348
+ contextArgument(block) {
349
+ if(this === block) {
350
+ return this.exportsArgument || "exports";
351
+ }
352
+ return "this";
353
+ }
354
+
355
+ /*
356
+ * creates the end part of a IIFE around the module to inject a variable name
357
+ * (function(...){
358
+ * }.call(...)) <- this part
359
+ */
360
+ variableInjectionFunctionWrapperEndCode(varExpressions, block) {
361
+ const firstParam = this.contextArgument(block);
362
+ const furtherParams = varExpressions.map(e => e.source()).join(", ");
363
+ return `}.call(${firstParam}, ${furtherParams}))`;
364
+ }
365
+
366
+ splitVariablesInUniqueNamedChunks(vars) {
367
+ const startState = [
368
+ []
369
+ ];
370
+ return vars.reduce((chunks, variable) => {
371
+ const current = chunks[chunks.length - 1];
372
+ // check if variable with same name exists already
373
+ // if so create a new chunk of variables.
374
+ const variableNameAlreadyExists = current.some(v => v.name === variable.name);
375
+
376
+ if(variableNameAlreadyExists) {
377
+ // start new chunk with current variable
378
+ chunks.push([variable]);
379
+ } else {
380
+ // else add it to current chunk
381
+ current.push(variable);
382
+ }
383
+ return chunks;
384
+ }, startState);
385
+ }
386
+
387
+ sourceBlock(block, availableVars, dependencyTemplates, source, outputOptions, requestShortener) {
388
+ block.dependencies.forEach((dependency) => this.sourceDependency(
389
+ dependency, dependencyTemplates, source, outputOptions, requestShortener));
390
+
391
+ /**
392
+ * Get the variables of all blocks that we need to inject.
393
+ * These will contain the variable name and its expression.
394
+ * The name will be added as a paramter in a IIFE the expression as its value.
395
+ */
396
+ const vars = block.variables.reduce((result, value) => {
397
+ const variable = this.sourceVariables(
398
+ value, availableVars, dependencyTemplates, outputOptions, requestShortener);
399
+
400
+ if(variable) {
401
+ result.push(variable);
402
+ }
403
+
404
+ return result;
405
+ }, []);
406
+
407
+ /**
408
+ * if we actually have variables
409
+ * this is important as how #splitVariablesInUniqueNamedChunks works
410
+ * it will always return an array in an array which would lead to a IIFE wrapper around
411
+ * a module if we do this with an empty vars array.
412
+ */
413
+ if(vars.length > 0) {
414
+ /**
415
+ * Split all variables up into chunks of unique names.
416
+ * e.g. imagine you have the following variable names that need to be injected:
417
+ * [foo, bar, baz, foo, some, more]
418
+ * we can not inject "foo" twice, therefore we just make two IIFEs like so:
419
+ * (function(foo, bar, baz){
420
+ * (function(foo, some, more){
421
+ * ...
422
+ * }(...));
423
+ * }(...));
424
+ *
425
+ * "splitVariablesInUniqueNamedChunks" splits the variables shown above up to this:
426
+ * [[foo, bar, baz], [foo, some, more]]
427
+ */
428
+ const injectionVariableChunks = this.splitVariablesInUniqueNamedChunks(vars);
429
+
430
+ // create all the beginnings of IIFEs
431
+ const functionWrapperStarts = injectionVariableChunks.map((variableChunk) => {
432
+ return this.variableInjectionFunctionWrapperStartCode(
433
+ variableChunk.map(variable => variable.name)
434
+ );
435
+ });
436
+
437
+ // and all the ends
438
+ const functionWrapperEnds = injectionVariableChunks.map((variableChunk) => {
439
+ return this.variableInjectionFunctionWrapperEndCode(
440
+ variableChunk.map(variable => variable.expression), block
441
+ );
442
+ });
443
+
444
+ // join them to one big string
445
+ const varStartCode = functionWrapperStarts.join("");
446
+
447
+ // reverse the ends first before joining them, as the last added must be the inner most
448
+ const varEndCode = functionWrapperEnds.reverse().join("");
449
+
450
+ // if we have anything, add it to the source
451
+ if(varStartCode && varEndCode) {
452
+ const start = block.range ? block.range[0] : -10;
453
+ const end = block.range ? block.range[1] : (this._source.size() + 1);
454
+ source.insert(start + 0.5, varStartCode);
455
+ source.insert(end + 0.5, "\n/* WEBPACK VAR INJECTION */" + varEndCode);
456
+ }
457
+ }
458
+
459
+ block.blocks.forEach((block) =>
460
+ this.sourceBlock(
461
+ block,
462
+ availableVars.concat(vars),
463
+ dependencyTemplates,
464
+ source,
465
+ outputOptions,
466
+ requestShortener
467
+ )
468
+ );
469
+ }
470
+
471
+ source(dependencyTemplates, outputOptions, requestShortener) {
472
+ const hashDigest = this.getHashDigest(dependencyTemplates);
473
+ if(this._cachedSource && this._cachedSource.hash === hashDigest) {
474
+ return this._cachedSource.source;
475
+ }
476
+
477
+ if(!this._source) {
478
+ return new RawSource("throw new Error('No source available');");
479
+ }
480
+
481
+ const source = new ReplaceSource(this._source);
482
+ this._cachedSource = {
483
+ source: source,
484
+ hash: hashDigest
485
+ };
486
+
487
+ this.sourceBlock(this, [], dependencyTemplates, source, outputOptions, requestShortener);
488
+ return new CachedSource(source);
489
+ }
490
+
491
+ originalSource() {
492
+ return this._source;
493
+ }
494
+
495
+ getHighestTimestamp(keys, timestampsByKey) {
496
+ let highestTimestamp = 0;
497
+ for(let i = 0; i < keys.length; i++) {
498
+ const key = keys[i];
499
+ const timestamp = timestampsByKey[key];
500
+ // if there is no timestamp yet, early return with Infinity
501
+ if(!timestamp) return Infinity;
502
+ highestTimestamp = Math.max(highestTimestamp, timestamp);
503
+ }
504
+ return highestTimestamp;
505
+ }
506
+
507
+ needRebuild(fileTimestamps, contextTimestamps) {
508
+ const highestFileDepTimestamp = this.getHighestTimestamp(
509
+ this.fileDependencies, fileTimestamps);
510
+ // if the hightest is Infinity, we need a rebuild
511
+ // exit early here.
512
+ if(highestFileDepTimestamp === Infinity) {
513
+ return true;
514
+ }
515
+
516
+ const highestContextDepTimestamp = this.getHighestTimestamp(
517
+ this.contextDependencies, contextTimestamps);
518
+
519
+ // Again if the hightest is Infinity, we need a rebuild
520
+ // exit early here.
521
+ if(highestContextDepTimestamp === Infinity) {
522
+ return true;
523
+ }
524
+
525
+ // else take the highest of file and context timestamps and compare
526
+ // to last build timestamp
527
+ return Math.max(highestContextDepTimestamp, highestFileDepTimestamp) >= this.buildTimestamp;
528
+ }
529
+
530
+ size() {
531
+ return this._source ? this._source.size() : -1;
532
+ }
533
+
534
+ updateHashWithSource(hash) {
535
+ if(!this._source) {
536
+ hash.update("null");
537
+ return;
538
+ }
539
+ hash.update("source");
540
+ this._source.updateHash(hash);
541
+ }
542
+
543
+ updateHashWithMeta(hash) {
544
+ hash.update("meta");
545
+ hash.update(JSON.stringify(this.meta));
546
+ }
547
+
548
+ updateHash(hash) {
549
+ this.updateHashWithSource(hash);
550
+ this.updateHashWithMeta(hash);
551
+ super.updateHash(hash);
552
+ }
553
+
554
+ }
555
+
556
+ module.exports = NormalModule;