webpack 4.36.1 → 4.39.1

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.
@@ -0,0 +1,131 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ /**
9
+ * @enum {string}
10
+ */
11
+ const LogType = Object.freeze({
12
+ error: "error", // message, c style arguments
13
+ warn: "warn", // message, c style arguments
14
+ info: "info", // message, c style arguments
15
+ log: "log", // message, c style arguments
16
+ debug: "debug", // message, c style arguments
17
+
18
+ trace: "trace", // no arguments
19
+
20
+ group: "group", // [label]
21
+ groupCollapsed: "groupCollapsed", // [label]
22
+ groupEnd: "groupEnd", // [label]
23
+
24
+ profile: "profile", // [profileName]
25
+ profileEnd: "profileEnd", // [profileName]
26
+
27
+ time: "time", // name, time as [seconds, nanoseconds]
28
+
29
+ clear: "clear", // no arguments
30
+ status: "status" // message, arguments
31
+ });
32
+
33
+ exports.LogType = LogType;
34
+
35
+ /** @typedef {LogType} LogTypeEnum */
36
+
37
+ const LOG_SYMBOL = Symbol("webpack logger raw log method");
38
+ const TIMERS_SYMBOL = Symbol("webpack logger times");
39
+
40
+ class WebpackLogger {
41
+ /**
42
+ * @param {function(LogType, any[]=): void} log log function
43
+ */
44
+ constructor(log) {
45
+ this[LOG_SYMBOL] = log;
46
+ }
47
+
48
+ error(...args) {
49
+ this[LOG_SYMBOL](LogType.error, args);
50
+ }
51
+
52
+ warn(...args) {
53
+ this[LOG_SYMBOL](LogType.warn, args);
54
+ }
55
+
56
+ info(...args) {
57
+ this[LOG_SYMBOL](LogType.info, args);
58
+ }
59
+
60
+ log(...args) {
61
+ this[LOG_SYMBOL](LogType.log, args);
62
+ }
63
+
64
+ debug(...args) {
65
+ this[LOG_SYMBOL](LogType.debug, args);
66
+ }
67
+
68
+ assert(assertion, ...args) {
69
+ if (!assertion) {
70
+ this[LOG_SYMBOL](LogType.error, args);
71
+ }
72
+ }
73
+
74
+ trace() {
75
+ this[LOG_SYMBOL](LogType.trace, ["Trace"]);
76
+ }
77
+
78
+ clear() {
79
+ this[LOG_SYMBOL](LogType.clear);
80
+ }
81
+
82
+ status(...args) {
83
+ this[LOG_SYMBOL](LogType.status, args);
84
+ }
85
+
86
+ group(...args) {
87
+ this[LOG_SYMBOL](LogType.group, args);
88
+ }
89
+
90
+ groupCollapsed(...args) {
91
+ this[LOG_SYMBOL](LogType.groupCollapsed, args);
92
+ }
93
+
94
+ groupEnd(...args) {
95
+ this[LOG_SYMBOL](LogType.groupEnd, args);
96
+ }
97
+
98
+ profile(label) {
99
+ this[LOG_SYMBOL](LogType.profile, [label]);
100
+ }
101
+
102
+ profileEnd(label) {
103
+ this[LOG_SYMBOL](LogType.profileEnd, [label]);
104
+ }
105
+
106
+ time(label) {
107
+ this[TIMERS_SYMBOL] = this[TIMERS_SYMBOL] || new Map();
108
+ this[TIMERS_SYMBOL].set(label, process.hrtime());
109
+ }
110
+
111
+ timeLog(label) {
112
+ const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
113
+ if (!prev) {
114
+ throw new Error(`No such label '${label}' for WebpackLogger.timeLog()`);
115
+ }
116
+ const time = process.hrtime(prev);
117
+ this[LOG_SYMBOL](LogType.time, [label, ...time]);
118
+ }
119
+
120
+ timeEnd(label) {
121
+ const prev = this[TIMERS_SYMBOL] && this[TIMERS_SYMBOL].get(label);
122
+ if (!prev) {
123
+ throw new Error(`No such label '${label}' for WebpackLogger.timeEnd()`);
124
+ }
125
+ const time = process.hrtime(prev);
126
+ this[TIMERS_SYMBOL].delete(label);
127
+ this[LOG_SYMBOL](LogType.time, [label, ...time]);
128
+ }
129
+ }
130
+
131
+ exports.Logger = WebpackLogger;
@@ -0,0 +1,209 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const { LogType } = require("./Logger");
9
+
10
+ /** @typedef {import("./Logger").LogTypeEnum} LogTypeEnum */
11
+ /** @typedef {import("../../declarations/WebpackOptions").FilterTypes} FilterTypes */
12
+ /** @typedef {import("../../declarations/WebpackOptions").FilterItemTypes} FilterItemTypes */
13
+
14
+ /** @typedef {function(string): boolean} FilterFunction */
15
+
16
+ /**
17
+ * @typedef {Object} LoggerOptions
18
+ * @property {false|true|"none"|"error"|"warn"|"info"|"log"|"verbose"} level loglevel
19
+ * @property {FilterTypes|boolean} debug filter for debug logging
20
+ * @property {Console & { status?: Function, logTime?: Function }} console the console to log to
21
+ */
22
+
23
+ /**
24
+ * @param {FilterItemTypes} item an input item
25
+ * @returns {FilterFunction} filter funtion
26
+ */
27
+ const filterToFunction = item => {
28
+ if (typeof item === "string") {
29
+ const regExp = new RegExp(
30
+ `[\\\\/]${item.replace(
31
+ // eslint-disable-next-line no-useless-escape
32
+ /[-[\]{}()*+?.\\^$|]/g,
33
+ "\\$&"
34
+ )}([\\\\/]|$|!|\\?)`
35
+ );
36
+ return ident => regExp.test(ident);
37
+ }
38
+ if (item && typeof item === "object" && typeof item.test === "function") {
39
+ return ident => item.test(ident);
40
+ }
41
+ if (typeof item === "function") {
42
+ return item;
43
+ }
44
+ if (typeof item === "boolean") {
45
+ return () => item;
46
+ }
47
+ };
48
+
49
+ /**
50
+ * @enum {number} */
51
+ const LogLevel = {
52
+ none: 6,
53
+ false: 6,
54
+ error: 5,
55
+ warn: 4,
56
+ info: 3,
57
+ log: 2,
58
+ true: 2,
59
+ verbose: 1
60
+ };
61
+
62
+ /**
63
+ * @param {LoggerOptions} options options object
64
+ * @returns {function(string, LogTypeEnum, any[]): void} logging function
65
+ */
66
+ module.exports = ({ level = "info", debug = false, console }) => {
67
+ const debugFilters =
68
+ typeof debug === "boolean"
69
+ ? [() => debug]
70
+ : /** @type {FilterItemTypes[]} */ ([])
71
+ .concat(debug)
72
+ .map(filterToFunction);
73
+ /** @type {number} */
74
+ const loglevel = LogLevel[`${level}`] || 0;
75
+
76
+ /**
77
+ * @param {string} name name of the logger
78
+ * @param {LogTypeEnum} type type of the log entry
79
+ * @param {any[]} args arguments of the log entry
80
+ * @returns {void}
81
+ */
82
+ const logger = (name, type, args) => {
83
+ const labeledArgs = () => {
84
+ if (Array.isArray(args)) {
85
+ if (args.length > 0 && typeof args[0] === "string") {
86
+ return [`[${name}] ${args[0]}`, ...args.slice(1)];
87
+ } else {
88
+ return [`[${name}]`, ...args];
89
+ }
90
+ } else {
91
+ return [];
92
+ }
93
+ };
94
+ const debug = debugFilters.some(f => f(name));
95
+ switch (type) {
96
+ case LogType.debug:
97
+ if (!debug) return;
98
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
99
+ if (typeof console.debug === "function") {
100
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
101
+ console.debug(...labeledArgs());
102
+ } else {
103
+ console.log(...labeledArgs());
104
+ }
105
+ break;
106
+ case LogType.log:
107
+ if (!debug && loglevel > LogLevel.log) return;
108
+ console.log(...labeledArgs());
109
+ break;
110
+ case LogType.info:
111
+ if (!debug && loglevel > LogLevel.info) return;
112
+ console.info(...labeledArgs());
113
+ break;
114
+ case LogType.warn:
115
+ if (!debug && loglevel > LogLevel.warn) return;
116
+ console.warn(...labeledArgs());
117
+ break;
118
+ case LogType.error:
119
+ if (!debug && loglevel > LogLevel.error) return;
120
+ console.error(...labeledArgs());
121
+ break;
122
+ case LogType.trace:
123
+ if (!debug) return;
124
+ console.trace();
125
+ break;
126
+ case LogType.groupCollapsed:
127
+ if (!debug && loglevel > LogLevel.log) return;
128
+ if (!debug && loglevel > LogLevel.verbose) {
129
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
130
+ if (typeof console.groupCollapsed === "function") {
131
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
132
+ console.groupCollapsed(...labeledArgs());
133
+ } else {
134
+ console.log(...labeledArgs());
135
+ }
136
+ break;
137
+ }
138
+ // falls through
139
+ case LogType.group:
140
+ if (!debug && loglevel > LogLevel.log) return;
141
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
142
+ if (typeof console.group === "function") {
143
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
144
+ console.group(...labeledArgs());
145
+ } else {
146
+ console.log(...labeledArgs());
147
+ }
148
+ break;
149
+ case LogType.groupEnd:
150
+ if (!debug && loglevel > LogLevel.log) return;
151
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
152
+ if (typeof console.groupEnd === "function") {
153
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
154
+ console.groupEnd();
155
+ }
156
+ break;
157
+ case LogType.time: {
158
+ if (!debug && loglevel > LogLevel.log) return;
159
+ const ms = args[1] * 1000 + args[2] / 1000000;
160
+ const msg = `[${name}] ${args[0]}: ${ms}ms`;
161
+ if (typeof console.logTime === "function") {
162
+ console.logTime(msg);
163
+ } else {
164
+ console.log(msg);
165
+ }
166
+ break;
167
+ }
168
+ case LogType.profile:
169
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
170
+ if (typeof console.profile === "function") {
171
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
172
+ console.profile(...labeledArgs());
173
+ }
174
+ break;
175
+ case LogType.profileEnd:
176
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
177
+ if (typeof console.profileEnd === "function") {
178
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
179
+ console.profileEnd(...labeledArgs());
180
+ }
181
+ break;
182
+ case LogType.clear:
183
+ if (!debug && loglevel > LogLevel.log) return;
184
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
185
+ if (typeof console.clear === "function") {
186
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
187
+ console.clear();
188
+ }
189
+ break;
190
+ case LogType.status:
191
+ if (!debug && loglevel > LogLevel.info) return;
192
+ if (typeof console.status === "function") {
193
+ if (args.length === 0) {
194
+ console.status();
195
+ } else {
196
+ console.status(...labeledArgs());
197
+ }
198
+ } else {
199
+ if (args.length !== 0) {
200
+ console.info(...labeledArgs());
201
+ }
202
+ }
203
+ break;
204
+ default:
205
+ throw new Error(`Unexpected LogType ${type}`);
206
+ }
207
+ };
208
+ return logger;
209
+ };
@@ -0,0 +1,36 @@
1
+ const SyncBailHook = require("tapable/lib/SyncBailHook");
2
+ const { Logger } = require("./Logger");
3
+ const createConsoleLogger = require("./createConsoleLogger");
4
+
5
+ /** @type {createConsoleLogger.LoggerOptions} */
6
+ let currentDefaultLoggerOptions = {
7
+ level: "info",
8
+ debug: false,
9
+ console
10
+ };
11
+ let currentDefaultLogger = createConsoleLogger(currentDefaultLoggerOptions);
12
+
13
+ /**
14
+ * @param {string} name name of the logger
15
+ * @returns {Logger} a logger
16
+ */
17
+ exports.getLogger = name => {
18
+ return new Logger((type, args) => {
19
+ if (exports.hooks.log.call(name, type, args) === undefined) {
20
+ currentDefaultLogger(name, type, args);
21
+ }
22
+ });
23
+ };
24
+
25
+ /**
26
+ * @param {createConsoleLogger.LoggerOptions} options new options, merge with old options
27
+ * @returns {void}
28
+ */
29
+ exports.configureDefaultLogger = options => {
30
+ Object.assign(currentDefaultLoggerOptions, options);
31
+ currentDefaultLogger = createConsoleLogger(currentDefaultLoggerOptions);
32
+ };
33
+
34
+ exports.hooks = {
35
+ log: new SyncBailHook(["origin", "type", "args"])
36
+ };
@@ -0,0 +1,76 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ /**
9
+ * @param {any[]} args items to be truncated
10
+ * @param {number} maxLength maximum length of args including spaces between
11
+ * @returns {string[]} truncated args
12
+ */
13
+ const truncateArgs = (args, maxLength) => {
14
+ const lengths = args.map(a => `${a}`.length);
15
+ const availableLength = maxLength - lengths.length + 1;
16
+
17
+ if (availableLength > 0 && args.length === 1) {
18
+ if (availableLength >= args[0].length) {
19
+ return args;
20
+ } else if (availableLength > 3) {
21
+ return ["..." + args[0].slice(-availableLength + 3)];
22
+ } else {
23
+ return [args[0].slice(-availableLength)];
24
+ }
25
+ }
26
+
27
+ // Check if there is space for at least 4 chars per arg
28
+ if (availableLength < lengths.reduce((s, i) => s + Math.min(i, 6), 0)) {
29
+ // remove args
30
+ if (args.length > 1)
31
+ return truncateArgs(args.slice(0, args.length - 1), maxLength);
32
+ return [];
33
+ }
34
+
35
+ let currentLength = lengths.reduce((a, b) => a + b, 0);
36
+
37
+ // Check if all fits into maxLength
38
+ if (currentLength <= availableLength) return args;
39
+
40
+ // Try to remove chars from the longest items until it fits
41
+ while (currentLength > availableLength) {
42
+ const maxLength = Math.max(...lengths);
43
+ const shorterItems = lengths.filter(l => l !== maxLength);
44
+ const nextToMaxLength =
45
+ shorterItems.length > 0 ? Math.max(...shorterItems) : 0;
46
+ const maxReduce = maxLength - nextToMaxLength;
47
+ let maxItems = lengths.length - shorterItems.length;
48
+ let overrun = currentLength - availableLength;
49
+ for (let i = 0; i < lengths.length; i++) {
50
+ if (lengths[i] === maxLength) {
51
+ const reduce = Math.min(Math.floor(overrun / maxItems), maxReduce);
52
+ lengths[i] -= reduce;
53
+ currentLength -= reduce;
54
+ overrun -= reduce;
55
+ maxItems--;
56
+ }
57
+ }
58
+ }
59
+
60
+ // Return args reduced to length in lengths
61
+ return args.map((a, i) => {
62
+ const str = `${a}`;
63
+ const length = lengths[i];
64
+ if (str.length === length) {
65
+ return str;
66
+ } else if (length > 5) {
67
+ return "..." + str.slice(-length + 3);
68
+ } else if (length > 0) {
69
+ return str.slice(-length);
70
+ } else {
71
+ return "";
72
+ }
73
+ });
74
+ };
75
+
76
+ module.exports = truncateArgs;
@@ -8,9 +8,25 @@ const NodeWatchFileSystem = require("./NodeWatchFileSystem");
8
8
  const NodeOutputFileSystem = require("./NodeOutputFileSystem");
9
9
  const NodeJsInputFileSystem = require("enhanced-resolve/lib/NodeJsInputFileSystem");
10
10
  const CachedInputFileSystem = require("enhanced-resolve/lib/CachedInputFileSystem");
11
+ const createConsoleLogger = require("../logging/createConsoleLogger");
12
+ const nodeConsole = require("./nodeConsole");
11
13
 
12
14
  class NodeEnvironmentPlugin {
15
+ constructor(options) {
16
+ this.options = options || {};
17
+ }
18
+
13
19
  apply(compiler) {
20
+ compiler.infrastructureLogger = createConsoleLogger(
21
+ Object.assign(
22
+ {
23
+ level: "info",
24
+ debug: false,
25
+ console: nodeConsole
26
+ },
27
+ this.options.infrastructureLogging
28
+ )
29
+ );
14
30
  compiler.inputFileSystem = new CachedInputFileSystem(
15
31
  new NodeJsInputFileSystem(),
16
32
  60000
@@ -0,0 +1,135 @@
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+
6
+ "use strict";
7
+
8
+ const truncateArgs = require("../logging/truncateArgs");
9
+ const util = require("util");
10
+
11
+ const tty = process.stderr.isTTY && process.env.TERM !== "dumb";
12
+
13
+ let currentStatusMessage = undefined;
14
+ let hasStatusMessage = false;
15
+ let currentIndent = "";
16
+ let currentCollapsed = 0;
17
+
18
+ const indent = (str, prefix, colorPrefix, colorSuffix) => {
19
+ if (str === "") return str;
20
+ prefix = currentIndent + prefix;
21
+ if (tty) {
22
+ return (
23
+ prefix +
24
+ colorPrefix +
25
+ str.replace(/\n/g, colorSuffix + "\n" + prefix + colorPrefix) +
26
+ colorSuffix
27
+ );
28
+ } else {
29
+ return prefix + str.replace(/\n/g, "\n" + prefix);
30
+ }
31
+ };
32
+
33
+ const clearStatusMessage = () => {
34
+ if (hasStatusMessage) {
35
+ process.stderr.write("\x1b[2K\r");
36
+ hasStatusMessage = false;
37
+ }
38
+ };
39
+
40
+ const writeStatusMessage = () => {
41
+ if (!currentStatusMessage) return;
42
+ const l = process.stderr.columns;
43
+ const args = l
44
+ ? truncateArgs(currentStatusMessage, l - 1)
45
+ : currentStatusMessage;
46
+ const str = args.join(" ");
47
+ const coloredStr = `\u001b[1m${str}\u001b[39m\u001b[22m`;
48
+ process.stderr.write(`\x1b[2K\r${coloredStr}`);
49
+ hasStatusMessage = true;
50
+ };
51
+
52
+ const writeColored = (prefix, colorPrefix, colorSuffix) => {
53
+ return (...args) => {
54
+ if (currentCollapsed > 0) return;
55
+ clearStatusMessage();
56
+ // @ts-ignore
57
+ const str = indent(util.format(...args), prefix, colorPrefix, colorSuffix);
58
+ process.stderr.write(str + "\n");
59
+ writeStatusMessage();
60
+ };
61
+ };
62
+
63
+ const writeGroupMessage = writeColored(
64
+ "<-> ",
65
+ "\u001b[1m\u001b[36m",
66
+ "\u001b[39m\u001b[22m"
67
+ );
68
+
69
+ const writeGroupCollapsedMessage = writeColored(
70
+ "<+> ",
71
+ "\u001b[1m\u001b[36m",
72
+ "\u001b[39m\u001b[22m"
73
+ );
74
+
75
+ module.exports = {
76
+ log: writeColored(" ", "\u001b[1m", "\u001b[22m"),
77
+ debug: writeColored(" ", "", ""),
78
+ trace: writeColored(" ", "", ""),
79
+ info: writeColored("<i> ", "\u001b[1m\u001b[32m", "\u001b[39m\u001b[22m"),
80
+ warn: writeColored("<w> ", "\u001b[1m\u001b[33m", "\u001b[39m\u001b[22m"),
81
+ error: writeColored("<e> ", "\u001b[1m\u001b[31m", "\u001b[39m\u001b[22m"),
82
+ logTime: writeColored("<t> ", "\u001b[1m\u001b[35m", "\u001b[39m\u001b[22m"),
83
+ group: (...args) => {
84
+ writeGroupMessage(...args);
85
+ if (currentCollapsed > 0) {
86
+ currentCollapsed++;
87
+ } else {
88
+ currentIndent += " ";
89
+ }
90
+ },
91
+ groupCollapsed: (...args) => {
92
+ writeGroupCollapsedMessage(...args);
93
+ currentCollapsed++;
94
+ },
95
+ groupEnd: () => {
96
+ if (currentCollapsed > 0) currentCollapsed--;
97
+ else if (currentIndent.length >= 2)
98
+ currentIndent = currentIndent.slice(0, currentIndent.length - 2);
99
+ },
100
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
101
+ profile: console.profile && (name => console.profile(name)),
102
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
103
+ profileEnd: console.profileEnd && (name => console.profileEnd(name)),
104
+ clear:
105
+ tty &&
106
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
107
+ console.clear &&
108
+ (() => {
109
+ clearStatusMessage();
110
+ // eslint-disable-next-line node/no-unsupported-features/node-builtins
111
+ console.clear();
112
+ writeStatusMessage();
113
+ }),
114
+ status: tty
115
+ ? (name, ...args) => {
116
+ args = args.filter(Boolean);
117
+ if (name === undefined && args.length === 0) {
118
+ clearStatusMessage();
119
+ currentStatusMessage = undefined;
120
+ } else if (
121
+ typeof name === "string" &&
122
+ name.startsWith("[webpack.Progress] ")
123
+ ) {
124
+ currentStatusMessage = [name.slice(19), ...args];
125
+ writeStatusMessage();
126
+ } else if (name === "[webpack.Progress]") {
127
+ currentStatusMessage = [...args];
128
+ writeStatusMessage();
129
+ } else {
130
+ currentStatusMessage = [name, ...args];
131
+ writeStatusMessage();
132
+ }
133
+ }
134
+ : writeColored("<s> ", "", "")
135
+ };
@@ -367,7 +367,7 @@ class JsonpMainTemplatePlugin {
367
367
  "for(;i < chunkIds.length; i++) {",
368
368
  Template.indent([
369
369
  "chunkId = chunkIds[i];",
370
- "if(installedChunks[chunkId]) {",
370
+ "if(Object.prototype.hasOwnProperty.call(installedChunks, chunkId) && installedChunks[chunkId]) {",
371
371
  Template.indent("resolves.push(installedChunks[chunkId][0]);"),
372
372
  "}",
373
373
  "installedChunks[chunkId] = 0;"
package/lib/webpack.js CHANGED
@@ -40,7 +40,9 @@ const webpack = (options, callback) => {
40
40
 
41
41
  compiler = new Compiler(options.context);
42
42
  compiler.options = options;
43
- new NodeEnvironmentPlugin().apply(compiler);
43
+ new NodeEnvironmentPlugin({
44
+ infrastructureLogging: options.infrastructureLogging
45
+ }).apply(compiler);
44
46
  if (options.plugins && Array.isArray(options.plugins)) {
45
47
  for (const plugin of options.plugins) {
46
48
  if (typeof plugin === "function") {