Merge branch 'main' of https://github.com/webpack/webpack into tosmolka/14075

This commit is contained in:
Tobias Smolka 2021-09-17 09:26:44 +02:00
commit f26d9d4a88
24 changed files with 395 additions and 284 deletions

View File

@ -21,8 +21,8 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: 16.8.0
cache: "yarn"
node-version: 16.x
cache: 'yarn'
- run: yarn --frozen-lockfile
- uses: actions/cache@v1
with:
@ -37,8 +37,8 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: 16.8.0
cache: "yarn"
node-version: 16.x
cache: 'yarn'
- run: yarn --frozen-lockfile
- run: yarn link --frozen-lockfile || true
- run: yarn link webpack --frozen-lockfile
@ -54,8 +54,8 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v2
with:
node-version: 16.8.0
cache: "yarn"
node-version: 16.x
cache: 'yarn'
- run: yarn --frozen-lockfile
- run: yarn link --frozen-lockfile || true
- run: yarn link webpack --frozen-lockfile
@ -75,7 +75,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10.x, 16.8.0]
node-version: [10.x, 16.x]
part: [a, b]
include:
- os: ubuntu-latest
@ -91,7 +91,7 @@ jobs:
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: "yarn"
cache: 'yarn'
- run: yarn --frozen-lockfile
- run: yarn link --frozen-lockfile || true
- run: yarn link webpack --frozen-lockfile

View File

@ -8,7 +8,7 @@ jobs:
steps:
- task: NodeTool@0
inputs:
versionSpec: "16.8.0"
versionSpec: "^16.0.0"
displayName: "Install Node.js"
- script: |
curl -o- -L https://yarnpkg.com/install.sh | bash
@ -125,10 +125,10 @@ jobs:
node_version: ^12.4.0
part: b
node-16-a:
node_version: 16.8.0
node_version: ^16.0.0
part: a
node-16-b:
node_version: 16.8.0
node_version: ^16.0.0
part: b
steps:
- task: NodeTool@0
@ -189,10 +189,10 @@ jobs:
node_version: ^14.0.0
part: a
node-16-a:
node_version: 16.8.0
node_version: ^16.0.0
part: a
node-16-b:
node_version: 16.8.0
node_version: ^16.0.0
part: b
steps:
- task: NodeTool@0
@ -251,10 +251,10 @@ jobs:
node_version: ^12.4.0
part: b
node-16-a:
node_version: 16.8.0
node_version: ^16.0.0
part: a
node-16-b:
node_version: 16.8.0
node_version: ^16.0.0
part: b
steps:
- task: NodeTool@0

View File

@ -1106,6 +1106,10 @@ export interface Experiments {
* Build http(s): urls using a lockfile and resource content cache.
*/
buildHttp?: boolean | HttpUriOptions;
/**
* Apply defaults of next major version.
*/
futureDefaults?: boolean;
/**
* Enable module and chunk layers.
*/
@ -1629,15 +1633,15 @@ export interface NodeOptions {
/**
* Include a polyfill for the '__dirname' variable.
*/
__dirname?: false | true | "mock" | "eval-only";
__dirname?: false | true | "warn-mock" | "mock" | "eval-only";
/**
* Include a polyfill for the '__filename' variable.
*/
__filename?: false | true | "mock" | "eval-only";
__filename?: false | true | "warn-mock" | "mock" | "eval-only";
/**
* Include a polyfill for the 'global' variable.
*/
global?: boolean;
global?: false | true | "warn";
}
/**
* Enables/Disables integrated optimizations.

View File

@ -13,11 +13,11 @@ export type IgnorePluginOptions =
/**
* A RegExp to test the request against.
*/
resourceRegExp?: RegExp;
resourceRegExp: RegExp;
}
| {
/**
* A filter function for resource and context.
*/
checkResource?: (resource: string, context: string) => boolean;
checkResource: (resource: string, context: string) => boolean;
};

View File

@ -453,12 +453,14 @@ class SnapshotOptimization {
* @param {function(Snapshot): boolean} has has value
* @param {function(Snapshot): Map<string, T> | Set<string>} get get value
* @param {function(Snapshot, Map<string, T> | Set<string>): void} set set value
* @param {boolean=} useStartTime use the start time of snapshots
* @param {boolean=} isSet value is an Set instead of a Map
*/
constructor(has, get, set, isSet = false) {
constructor(has, get, set, useStartTime = true, isSet = false) {
this._has = has;
this._get = get;
this._set = set;
this._useStartTime = useStartTime;
this._isSet = isSet;
/** @type {Map<string, SnapshotOptimizationEntry>} */
this._map = new Map();
@ -488,24 +490,12 @@ class SnapshotOptimization {
this._statReusedSharedSnapshots = 0;
}
storeUnsharedSnapshot(snapshot, locations) {
if (locations === undefined) return;
const optimizationEntry = {
snapshot,
shared: 0,
snapshotContent: undefined,
children: undefined
};
for (const path of locations) {
this._map.set(path, optimizationEntry);
}
}
optimize(capturedFiles, startTime, children) {
/** @type {Set<string>} */
const unsetOptimizationEntries = new Set();
/** @type {Set<SnapshotOptimizationEntry>} */
const checkedOptimizationEntries = new Set();
/**
* @param {Snapshot} newSnapshot snapshot
* @param {Set<string>} capturedFiles files to snapshot/share
* @returns {void}
*/
optimize(newSnapshot, capturedFiles) {
/**
* @param {SnapshotOptimizationEntry} entry optimization entry
* @returns {void}
@ -530,22 +520,43 @@ class SnapshotOptimization {
capturedFiles.delete(path);
}
};
/** @type {SnapshotOptimizationEntry} */
let newOptimizationEntry = undefined;
const capturedFilesSize = capturedFiles.size;
capturedFiles: for (const path of capturedFiles) {
/** @type {Set<SnapshotOptimizationEntry> | undefined} */
const optimizationEntries = new Set();
for (const path of capturedFiles) {
const optimizationEntry = this._map.get(path);
if (optimizationEntry === undefined) {
unsetOptimizationEntries.add(path);
if (newOptimizationEntry === undefined) {
newOptimizationEntry = {
snapshot: newSnapshot,
shared: 0,
snapshotContent: undefined,
children: undefined
};
}
this._map.set(path, newOptimizationEntry);
continue;
} else {
optimizationEntries.add(optimizationEntry);
}
if (checkedOptimizationEntries.has(optimizationEntry)) continue;
}
optimizationEntries: for (const optimizationEntry of optimizationEntries) {
const snapshot = optimizationEntry.snapshot;
if (optimizationEntry.shared > 0) {
// It's a shared snapshot
// We can't change it, so we can only use it when all files match
// and startTime is compatible
if (
startTime &&
(!snapshot.startTime || snapshot.startTime > startTime)
this._useStartTime &&
newSnapshot.startTime &&
(!snapshot.startTime || snapshot.startTime > newSnapshot.startTime)
) {
continue;
}
@ -557,8 +568,7 @@ class SnapshotOptimization {
if (!snapshotEntries.has(path)) {
// File is not shared and can't be removed from the snapshot
// because it's in a child of the snapshot
checkedOptimizationEntries.add(optimizationEntry);
continue capturedFiles;
continue optimizationEntries;
}
nonSharedFiles.add(path);
continue;
@ -567,7 +577,7 @@ class SnapshotOptimization {
if (nonSharedFiles.size === 0) {
// The complete snapshot is shared
// add it as child
children.add(snapshot);
newSnapshot.addChild(snapshot);
increaseSharedAndStoreOptimizationEntry(optimizationEntry);
this._statReusedSharedSnapshots++;
} else {
@ -575,8 +585,7 @@ class SnapshotOptimization {
const sharedCount = snapshotContent.size - nonSharedFiles.size;
if (sharedCount < MIN_COMMON_SNAPSHOT_SIZE) {
// Common part it too small
checkedOptimizationEntries.add(optimizationEntry);
continue capturedFiles;
continue optimizationEntries;
}
// Extract common timestamps from both snapshots
let commonMap;
@ -598,9 +607,11 @@ class SnapshotOptimization {
}
// Create and attach snapshot
const commonSnapshot = new Snapshot();
commonSnapshot.setMergedStartTime(startTime, snapshot);
if (this._useStartTime) {
commonSnapshot.setMergedStartTime(newSnapshot.startTime, snapshot);
}
this._set(commonSnapshot, commonMap);
children.add(commonSnapshot);
newSnapshot.addChild(commonSnapshot);
snapshot.addChild(commonSnapshot);
// Create optimization entry
const newEntry = {
@ -620,6 +631,10 @@ class SnapshotOptimization {
// We can extract a common shared snapshot
// with all common files
const snapshotEntries = this._get(snapshot);
if (snapshotEntries === undefined) {
// Incomplete snapshot, that can't be used
continue optimizationEntries;
}
let commonMap;
if (this._isSet) {
commonMap = new Set();
@ -645,14 +660,15 @@ class SnapshotOptimization {
if (commonMap.size < MIN_COMMON_SNAPSHOT_SIZE) {
// Common part it too small
checkedOptimizationEntries.add(optimizationEntry);
continue capturedFiles;
continue optimizationEntries;
}
// Create and attach snapshot
const commonSnapshot = new Snapshot();
commonSnapshot.setMergedStartTime(startTime, snapshot);
if (this._useStartTime) {
commonSnapshot.setMergedStartTime(newSnapshot.startTime, snapshot);
}
this._set(commonSnapshot, commonMap);
children.add(commonSnapshot);
newSnapshot.addChild(commonSnapshot);
snapshot.addChild(commonSnapshot);
// Remove files from snapshot
for (const path of commonMap.keys()) snapshotEntries.delete(path);
@ -668,12 +684,10 @@ class SnapshotOptimization {
});
this._statSharedSnapshots++;
}
checkedOptimizationEntries.add(optimizationEntry);
}
const unshared = capturedFiles.size;
this._statItemsUnshared += unshared;
this._statItemsShared += capturedFilesSize - unshared;
return unsetOptimizationEntries;
}
}
@ -858,7 +872,8 @@ class FileSystemInfo {
this._fileHashesOptimization = new SnapshotOptimization(
s => s.hasFileHashes(),
s => s.fileHashes,
(s, v) => s.setFileHashes(v)
(s, v) => s.setFileHashes(v),
false
);
this._fileTshsOptimization = new SnapshotOptimization(
s => s.hasFileTshs(),
@ -873,7 +888,8 @@ class FileSystemInfo {
this._contextHashesOptimization = new SnapshotOptimization(
s => s.hasContextHashes(),
s => s.contextHashes,
(s, v) => s.setContextHashes(v)
(s, v) => s.setContextHashes(v),
false
);
this._contextTshsOptimization = new SnapshotOptimization(
s => s.hasContextTshs(),
@ -883,29 +899,34 @@ class FileSystemInfo {
this._missingExistenceOptimization = new SnapshotOptimization(
s => s.hasMissingExistence(),
s => s.missingExistence,
(s, v) => s.setMissingExistence(v)
(s, v) => s.setMissingExistence(v),
false
);
this._managedItemInfoOptimization = new SnapshotOptimization(
s => s.hasManagedItemInfo(),
s => s.managedItemInfo,
(s, v) => s.setManagedItemInfo(v)
(s, v) => s.setManagedItemInfo(v),
false
);
this._managedFilesOptimization = new SnapshotOptimization(
s => s.hasManagedFiles(),
s => s.managedFiles,
(s, v) => s.setManagedFiles(v),
false,
true
);
this._managedContextsOptimization = new SnapshotOptimization(
s => s.hasManagedContexts(),
s => s.managedContexts,
(s, v) => s.setManagedContexts(v),
false,
true
);
this._managedMissingOptimization = new SnapshotOptimization(
s => s.hasManagedMissing(),
s => s.managedMissing,
(s, v) => s.setManagedMissing(v),
false,
true
);
/** @type {StackedCacheMap<string, FileSystemInfoEntry | "ignore" | null>} */
@ -1869,22 +1890,8 @@ class FileSystemInfo {
/** @type {Set<Snapshot>} */
const children = new Set();
/** @type {Set<string>} */
let unsharedFileTimestamps;
/** @type {Set<string>} */
let unsharedFileHashes;
/** @type {Set<string>} */
let unsharedFileTshs;
/** @type {Set<string>} */
let unsharedContextTimestamps;
/** @type {Set<string>} */
let unsharedContextHashes;
/** @type {Set<string>} */
let unsharedContextTshs;
/** @type {Set<string>} */
let unsharedMissingExistence;
/** @type {Set<string>} */
let unsharedManagedItemInfo;
const snapshot = new Snapshot();
if (startTime) snapshot.setStartTime(startTime);
/** @type {Set<string>} */
const managedItems = new Set();
@ -1895,101 +1902,41 @@ class FileSystemInfo {
let jobs = 1;
const jobDone = () => {
if (--jobs === 0) {
const snapshot = new Snapshot();
if (startTime) snapshot.setStartTime(startTime);
if (fileTimestamps.size !== 0) {
snapshot.setFileTimestamps(fileTimestamps);
this._fileTimestampsOptimization.storeUnsharedSnapshot(
snapshot,
unsharedFileTimestamps
);
}
if (fileHashes.size !== 0) {
snapshot.setFileHashes(fileHashes);
this._fileHashesOptimization.storeUnsharedSnapshot(
snapshot,
unsharedFileHashes
);
}
if (fileTshs.size !== 0) {
snapshot.setFileTshs(fileTshs);
this._fileTshsOptimization.storeUnsharedSnapshot(
snapshot,
unsharedFileTshs
);
}
if (contextTimestamps.size !== 0) {
snapshot.setContextTimestamps(contextTimestamps);
this._contextTimestampsOptimization.storeUnsharedSnapshot(
snapshot,
unsharedContextTimestamps
);
}
if (contextHashes.size !== 0) {
snapshot.setContextHashes(contextHashes);
this._contextHashesOptimization.storeUnsharedSnapshot(
snapshot,
unsharedContextHashes
);
}
if (contextTshs.size !== 0) {
snapshot.setContextTshs(contextTshs);
this._contextTshsOptimization.storeUnsharedSnapshot(
snapshot,
unsharedContextTshs
);
}
if (missingExistence.size !== 0) {
snapshot.setMissingExistence(missingExistence);
this._missingExistenceOptimization.storeUnsharedSnapshot(
snapshot,
unsharedMissingExistence
);
}
if (managedItemInfo.size !== 0) {
snapshot.setManagedItemInfo(managedItemInfo);
this._managedItemInfoOptimization.storeUnsharedSnapshot(
snapshot,
unsharedManagedItemInfo
);
}
const unsharedManagedFiles = this._managedFilesOptimization.optimize(
managedFiles,
undefined,
children
);
this._managedFilesOptimization.optimize(snapshot, managedFiles);
if (managedFiles.size !== 0) {
snapshot.setManagedFiles(managedFiles);
this._managedFilesOptimization.storeUnsharedSnapshot(
snapshot,
unsharedManagedFiles
);
}
const unsharedManagedContexts =
this._managedContextsOptimization.optimize(
managedContexts,
undefined,
children
);
this._managedContextsOptimization.optimize(snapshot, managedContexts);
if (managedContexts.size !== 0) {
snapshot.setManagedContexts(managedContexts);
this._managedContextsOptimization.storeUnsharedSnapshot(
snapshot,
unsharedManagedContexts
);
}
const unsharedManagedMissing =
this._managedMissingOptimization.optimize(
managedMissing,
undefined,
children
);
this._managedMissingOptimization.optimize(snapshot, managedMissing);
if (managedMissing.size !== 0) {
snapshot.setManagedMissing(managedMissing);
this._managedMissingOptimization.storeUnsharedSnapshot(
snapshot,
unsharedManagedMissing
);
}
if (children.size !== 0) {
snapshot.setChildren(children);
@ -2037,11 +1984,7 @@ class FileSystemInfo {
const capturedFiles = captureNonManaged(files, managedFiles);
switch (mode) {
case 3:
unsharedFileTshs = this._fileTshsOptimization.optimize(
capturedFiles,
undefined,
children
);
this._fileTshsOptimization.optimize(snapshot, capturedFiles);
for (const path of capturedFiles) {
const cache = this._fileTshs.get(path);
if (cache !== undefined) {
@ -2065,11 +2008,7 @@ class FileSystemInfo {
}
break;
case 2:
unsharedFileHashes = this._fileHashesOptimization.optimize(
capturedFiles,
undefined,
children
);
this._fileHashesOptimization.optimize(snapshot, capturedFiles);
for (const path of capturedFiles) {
const cache = this._fileHashes.get(path);
if (cache !== undefined) {
@ -2093,11 +2032,7 @@ class FileSystemInfo {
}
break;
case 1:
unsharedFileTimestamps = this._fileTimestampsOptimization.optimize(
capturedFiles,
startTime,
children
);
this._fileTimestampsOptimization.optimize(snapshot, capturedFiles);
for (const path of capturedFiles) {
const cache = this._fileTimestamps.get(path);
if (cache !== undefined) {
@ -2131,11 +2066,7 @@ class FileSystemInfo {
);
switch (mode) {
case 3:
unsharedContextTshs = this._contextTshsOptimization.optimize(
capturedDirectories,
undefined,
children
);
this._contextTshsOptimization.optimize(snapshot, capturedDirectories);
for (const path of capturedDirectories) {
const cache = this._contextTshs.get(path);
let resolved;
@ -2168,10 +2099,9 @@ class FileSystemInfo {
}
break;
case 2:
unsharedContextHashes = this._contextHashesOptimization.optimize(
capturedDirectories,
undefined,
children
this._contextHashesOptimization.optimize(
snapshot,
capturedDirectories
);
for (const path of capturedDirectories) {
const cache = this._contextHashes.get(path);
@ -2205,12 +2135,10 @@ class FileSystemInfo {
}
break;
case 1:
unsharedContextTimestamps =
this._contextTimestampsOptimization.optimize(
capturedDirectories,
startTime,
children
);
this._contextTimestampsOptimization.optimize(
snapshot,
capturedDirectories
);
for (const path of capturedDirectories) {
const cache = this._contextTimestamps.get(path);
let resolved;
@ -2246,11 +2174,7 @@ class FileSystemInfo {
}
if (missing) {
const capturedMissing = captureNonManaged(missing, managedMissing);
unsharedMissingExistence = this._missingExistenceOptimization.optimize(
capturedMissing,
startTime,
children
);
this._missingExistenceOptimization.optimize(snapshot, capturedMissing);
for (const path of capturedMissing) {
const cache = this._fileTimestamps.get(path);
if (cache !== undefined) {
@ -2275,11 +2199,7 @@ class FileSystemInfo {
}
}
}
unsharedManagedItemInfo = this._managedItemInfoOptimization.optimize(
managedItems,
undefined,
children
);
this._managedItemInfoOptimization.optimize(snapshot, managedItems);
for (const path of managedItems) {
const cache = this._managedItems.get(path);
if (cache !== undefined) {

View File

@ -0,0 +1,34 @@
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Ivan Kopeykin @vankop
*/
"use strict";
const WebpackError = require("./WebpackError");
const makeSerializable = require("./util/makeSerializable");
/** @typedef {import("./Dependency").DependencyLocation} DependencyLocation */
class NodeStuffInWebError extends WebpackError {
/**
* @param {DependencyLocation} loc loc
* @param {string} expression expression
* @param {string} description description
*/
constructor(loc, expression, description) {
super(
`${JSON.stringify(
expression
)} has been used, it will be undefined in next major version.
${description}`
);
this.name = "NodeStuffInWebError";
this.loc = loc;
}
}
makeSerializable(NodeStuffInWebError, "webpack/lib/NodeStuffInWebError");
module.exports = NodeStuffInWebError;

View File

@ -5,6 +5,7 @@
"use strict";
const NodeStuffInWebError = require("./NodeStuffInWebError");
const RuntimeGlobals = require("./RuntimeGlobals");
const CachedConstDependency = require("./dependencies/CachedConstDependency");
const ConstDependency = require("./dependencies/ConstDependency");
@ -44,7 +45,8 @@ class NodeStuffPlugin {
localOptions = { ...localOptions, ...parserOptions.node };
}
if (localOptions.global) {
if (localOptions.global !== false) {
const withWarning = localOptions.global === "warn";
parser.hooks.expression
.for("global")
.tap("NodeStuffPlugin", expr => {
@ -55,10 +57,21 @@ class NodeStuffPlugin {
);
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
// TODO webpack 6 remove
if (withWarning) {
parser.state.module.addWarning(
new NodeStuffInWebError(
dep.loc,
"global",
"The global namespace object is Node.js feature and doesn't present in browser."
)
);
}
});
}
const setModuleConstant = (expressionName, fn) => {
const setModuleConstant = (expressionName, fn, warning) => {
parser.hooks.expression
.for(expressionName)
.tap("NodeStuffPlugin", expr => {
@ -69,22 +82,41 @@ class NodeStuffPlugin {
);
dep.loc = expr.loc;
parser.state.module.addPresentationalDependency(dep);
// TODO webpack 6 remove
if (warning) {
parser.state.module.addWarning(
new NodeStuffInWebError(dep.loc, expressionName, warning)
);
}
return true;
});
};
const setConstant = (expressionName, value) =>
setModuleConstant(expressionName, () => value);
const setConstant = (expressionName, value, warning) =>
setModuleConstant(expressionName, () => value, warning);
const context = compiler.context;
if (localOptions.__filename) {
if (localOptions.__filename === "mock") {
setConstant("__filename", "/index.js");
} else if (localOptions.__filename === true) {
setModuleConstant("__filename", module =>
relative(compiler.inputFileSystem, context, module.resource)
);
switch (localOptions.__filename) {
case "mock":
setConstant("__filename", "/index.js");
break;
case "warn-mock":
setConstant(
"__filename",
"/index.js",
"The __filename is Node.js feature and doesn't present in browser."
);
break;
case true:
setModuleConstant("__filename", module =>
relative(compiler.inputFileSystem, context, module.resource)
);
break;
}
parser.hooks.evaluateIdentifier
.for("__filename")
.tap("NodeStuffPlugin", expr => {
@ -94,13 +126,24 @@ class NodeStuffPlugin {
});
}
if (localOptions.__dirname) {
if (localOptions.__dirname === "mock") {
setConstant("__dirname", "/");
} else if (localOptions.__dirname === true) {
setModuleConstant("__dirname", module =>
relative(compiler.inputFileSystem, context, module.context)
);
switch (localOptions.__dirname) {
case "mock":
setConstant("__dirname", "/");
break;
case "warn-mock":
setConstant(
"__dirname",
"/",
"The __dirname is Node.js feature and doesn't present in browser."
);
break;
case true:
setModuleConstant("__dirname", module =>
relative(compiler.inputFileSystem, context, module.context)
);
break;
}
parser.hooks.evaluateIdentifier
.for("__dirname")
.tap("NodeStuffPlugin", expr => {

View File

@ -118,6 +118,14 @@ class Pack {
this.requests.push(undefined);
this.requestsTimeout = undefined;
}, MAX_TIME_IN_FRESH_PACK);
if (this.requestsTimeout.unref) this.requestsTimeout.unref();
}
}
stopCapturingRequests() {
if (this.requestsTimeout !== undefined) {
clearTimeout(this.requestsTimeout);
this.requestsTimeout = undefined;
}
}
@ -1232,6 +1240,7 @@ class PackFileCacheStrategy {
const reportProgress = ProgressPlugin.getReporter(this.compiler);
return (this.storePromise = packPromise
.then(pack => {
pack.stopCapturingRequests();
if (!pack.invalid) return;
this.packPromise = undefined;
this.logger.log(`Storing pack...`);

View File

@ -211,7 +211,10 @@ const applyWebpackOptionsDefaults = options => {
: "var";
});
applyNodeDefaults(options.node, { targetProperties });
applyNodeDefaults(options.node, {
futureDefaults: options.experiments.futureDefaults,
targetProperties
});
F(options, "performance", () =>
production &&
@ -262,6 +265,7 @@ const applyExperimentsDefaults = (experiments, { production, development }) => {
D(experiments, "layers", false);
D(experiments, "lazyCompilation", false);
D(experiments, "buildHttp", false);
D(experiments, "futureDefaults", false);
if (typeof experiments.buildHttp === "object") {
D(experiments.buildHttp, "frozen", production);
@ -943,21 +947,26 @@ const applyLoaderDefaults = (loader, { targetProperties }) => {
* @param {WebpackNode} node options
* @param {Object} options options
* @param {TargetProperties | false} options.targetProperties target properties
* @param {boolean} options.futureDefaults is future defaults enabled
* @returns {void}
*/
const applyNodeDefaults = (node, { targetProperties }) => {
const applyNodeDefaults = (node, { futureDefaults, targetProperties }) => {
if (node === false) return;
F(node, "global", () => {
if (targetProperties && targetProperties.global) return false;
return true;
// TODO webpack 6 should always default to false
return futureDefaults ? "warn" : true;
});
F(node, "__filename", () => {
if (targetProperties && targetProperties.node) return "eval-only";
return "mock";
// TODO webpack 6 should always default to false
return futureDefaults ? "warn-mock" : "mock";
});
F(node, "__dirname", () => {
if (targetProperties && targetProperties.node) return "eval-only";
return "mock";
// TODO webpack 6 should always default to false
return futureDefaults ? "warn-mock" : "mock";
});
};

View File

@ -41,6 +41,7 @@ const builtins = [
"repl",
"stream",
"stream/promises",
"stream/web",
"string_decoder",
"sys",
"timers",

View File

@ -189,6 +189,7 @@ module.exports = {
UnsupportedFeatureWarning: () => require("../UnsupportedFeatureWarning"),
"util/LazySet": () => require("../util/LazySet"),
UnhandledSchemeError: () => require("../UnhandledSchemeError"),
NodeStuffInWebError: () => require("../NodeStuffInWebError"),
WebpackError: () => require("../WebpackError"),
"util/registerExternalSerializer": () => {

View File

@ -1,6 +1,6 @@
{
"name": "webpack",
"version": "5.52.1",
"version": "5.53.0",
"author": "Tobias Koppers @sokra",
"description": "Packs CommonJs/AMD modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to preprocess files, i.e. json, jsx, es7, css, less, ... and your custom stuff.",
"license": "MIT",
@ -39,7 +39,7 @@
"@babel/core": "^7.11.1",
"@babel/preset-react": "^7.10.4",
"@types/es-module-lexer": "^0.4.1",
"@types/jest": "^26.0.24",
"@types/jest": "^27.0.1",
"@types/node": "^15.0.1",
"babel-loader": "^8.1.0",
"benchmark": "^2.1.4",

File diff suppressed because one or more lines are too long

View File

@ -699,6 +699,10 @@
}
]
},
"futureDefaults": {
"description": "Apply defaults of next major version.",
"type": "boolean"
},
"layers": {
"description": "Enable module and chunk layers.",
"type": "boolean"
@ -1940,15 +1944,15 @@
"properties": {
"__dirname": {
"description": "Include a polyfill for the '__dirname' variable.",
"enum": [false, true, "mock", "eval-only"]
"enum": [false, true, "warn-mock", "mock", "eval-only"]
},
"__filename": {
"description": "Include a polyfill for the '__filename' variable.",
"enum": [false, true, "mock", "eval-only"]
"enum": [false, true, "warn-mock", "mock", "eval-only"]
},
"global": {
"description": "Include a polyfill for the 'global' variable.",
"type": "boolean"
"enum": [false, true, "warn"]
}
}
},

View File

@ -3,4 +3,4 @@
* DO NOT MODIFY BY HAND.
* Run `yarn special-lint-fix` to update
*/
"use strict";function e(t,{instancePath:o="",parentData:s,parentDataProperty:r,rootData:n=t}={}){let c=null,a=0;const p=a;let l=!1;const i=a;if(a===i)if(t&&"object"==typeof t&&!Array.isArray(t)){const e=a;for(const e in t)if("contextRegExp"!==e&&"resourceRegExp"!==e){const t={params:{additionalProperty:e}};null===c?c=[t]:c.push(t),a++;break}if(e===a){if(void 0!==t.contextRegExp){const e=a;if(!(t.contextRegExp instanceof RegExp)){const e={params:{}};null===c?c=[e]:c.push(e),a++}var u=e===a}else u=!0;if(u)if(void 0!==t.resourceRegExp){const e=a;if(!(t.resourceRegExp instanceof RegExp)){const e={params:{}};null===c?c=[e]:c.push(e),a++}u=e===a}else u=!0}}else{const e={params:{type:"object"}};null===c?c=[e]:c.push(e),a++}var f=i===a;if(l=l||f,!l){const e=a;if(a===e)if(t&&"object"==typeof t&&!Array.isArray(t)){const e=a;for(const e in t)if("checkResource"!==e){const t={params:{additionalProperty:e}};null===c?c=[t]:c.push(t),a++;break}if(e===a&&void 0!==t.checkResource&&!(t.checkResource instanceof Function)){const e={params:{}};null===c?c=[e]:c.push(e),a++}}else{const e={params:{type:"object"}};null===c?c=[e]:c.push(e),a++}f=e===a,l=l||f}if(!l){const t={params:{}};return null===c?c=[t]:c.push(t),a++,e.errors=c,!1}return a=p,null!==c&&(p?c.length=p:c=null),e.errors=c,0===a}module.exports=e,module.exports.default=e;
"use strict";function e(s,{instancePath:o="",parentData:r,parentDataProperty:t,rootData:n=s}={}){let c=null,a=0;const p=a;let l=!1;const i=a;if(a===i)if(s&&"object"==typeof s&&!Array.isArray(s)){let e;if(void 0===s.resourceRegExp&&(e="resourceRegExp")){const s={params:{missingProperty:e}};null===c?c=[s]:c.push(s),a++}else{const e=a;for(const e in s)if("contextRegExp"!==e&&"resourceRegExp"!==e){const s={params:{additionalProperty:e}};null===c?c=[s]:c.push(s),a++;break}if(e===a){if(void 0!==s.contextRegExp){const e=a;if(!(s.contextRegExp instanceof RegExp)){const e={params:{}};null===c?c=[e]:c.push(e),a++}var u=e===a}else u=!0;if(u)if(void 0!==s.resourceRegExp){const e=a;if(!(s.resourceRegExp instanceof RegExp)){const e={params:{}};null===c?c=[e]:c.push(e),a++}u=e===a}else u=!0}}}else{const e={params:{type:"object"}};null===c?c=[e]:c.push(e),a++}var f=i===a;if(l=l||f,!l){const e=a;if(a===e)if(s&&"object"==typeof s&&!Array.isArray(s)){let e;if(void 0===s.checkResource&&(e="checkResource")){const s={params:{missingProperty:e}};null===c?c=[s]:c.push(s),a++}else{const e=a;for(const e in s)if("checkResource"!==e){const s={params:{additionalProperty:e}};null===c?c=[s]:c.push(s),a++;break}if(e===a&&void 0!==s.checkResource&&!(s.checkResource instanceof Function)){const e={params:{}};null===c?c=[e]:c.push(e),a++}}}else{const e={params:{type:"object"}};null===c?c=[e]:c.push(e),a++}f=e===a,l=l||f}if(!l){const s={params:{}};return null===c?c=[s]:c.push(s),a++,e.errors=c,!1}return a=p,null!==c&&(p?c.length=p:c=null),e.errors=c,0===a}module.exports=e,module.exports.default=e;

View File

@ -15,7 +15,8 @@
"instanceof": "RegExp",
"tsType": "RegExp"
}
}
},
"required": ["resourceRegExp"]
},
{
"type": "object",
@ -26,7 +27,8 @@
"instanceof": "Function",
"tsType": "((resource: string, context: string) => boolean)"
}
}
},
"required": ["checkResource"]
}
]
}

View File

@ -94,6 +94,7 @@ Object {
"asset": false,
"asyncWebAssembly": false,
"buildHttp": false,
"futureDefaults": false,
"layers": false,
"lazyCompilation": false,
"outputModule": false,
@ -1875,4 +1876,29 @@ Object {
+ "async-node",
`)
);
test(
"experiments.futureDefaults",
{
experiments: {
futureDefaults: true
}
},
e =>
e.toMatchInlineSnapshot(`
- Expected
+ Received
@@ ... @@
- "futureDefaults": false,
+ "futureDefaults": true,
@@ ... @@
- "__dirname": "mock",
- "__filename": "mock",
- "global": true,
+ "__dirname": "warn-mock",
+ "__filename": "warn-mock",
+ "global": "warn",
`)
);
});

View File

@ -524,6 +524,19 @@ Object {
"multiple": false,
"simpleType": "boolean",
},
"experiments-future-defaults": Object {
"configs": Array [
Object {
"description": "Apply defaults of next major version.",
"multiple": false,
"path": "experiments.futureDefaults",
"type": "boolean",
},
],
"description": "Apply defaults of next major version.",
"multiple": false,
"simpleType": "boolean",
},
"experiments-layers": Object {
"configs": Array [
Object {
@ -1408,6 +1421,7 @@ Object {
"values": Array [
false,
true,
"warn-mock",
"mock",
"eval-only",
],
@ -1427,6 +1441,7 @@ Object {
"values": Array [
false,
true,
"warn-mock",
"mock",
"eval-only",
],
@ -1442,12 +1457,17 @@ Object {
"description": "Include a polyfill for the 'global' variable.",
"multiple": false,
"path": "module.parser.javascript/auto.node.global",
"type": "boolean",
"type": "enum",
"values": Array [
false,
true,
"warn",
],
},
],
"description": "Include a polyfill for the 'global' variable.",
"multiple": false,
"simpleType": "boolean",
"simpleType": "string",
},
"module-parser-javascript-auto-require-context": Object {
"configs": Array [
@ -1895,6 +1915,7 @@ Object {
"values": Array [
false,
true,
"warn-mock",
"mock",
"eval-only",
],
@ -1914,6 +1935,7 @@ Object {
"values": Array [
false,
true,
"warn-mock",
"mock",
"eval-only",
],
@ -1929,12 +1951,17 @@ Object {
"description": "Include a polyfill for the 'global' variable.",
"multiple": false,
"path": "module.parser.javascript/dynamic.node.global",
"type": "boolean",
"type": "enum",
"values": Array [
false,
true,
"warn",
],
},
],
"description": "Include a polyfill for the 'global' variable.",
"multiple": false,
"simpleType": "boolean",
"simpleType": "string",
},
"module-parser-javascript-dynamic-require-context": Object {
"configs": Array [
@ -2343,6 +2370,7 @@ Object {
"values": Array [
false,
true,
"warn-mock",
"mock",
"eval-only",
],
@ -2362,6 +2390,7 @@ Object {
"values": Array [
false,
true,
"warn-mock",
"mock",
"eval-only",
],
@ -2377,12 +2406,17 @@ Object {
"description": "Include a polyfill for the 'global' variable.",
"multiple": false,
"path": "module.parser.javascript/esm.node.global",
"type": "boolean",
"type": "enum",
"values": Array [
false,
true,
"warn",
],
},
],
"description": "Include a polyfill for the 'global' variable.",
"multiple": false,
"simpleType": "boolean",
"simpleType": "string",
},
"module-parser-javascript-esm-require-context": Object {
"configs": Array [
@ -2736,6 +2770,7 @@ Object {
"values": Array [
false,
true,
"warn-mock",
"mock",
"eval-only",
],
@ -2755,6 +2790,7 @@ Object {
"values": Array [
false,
true,
"warn-mock",
"mock",
"eval-only",
],
@ -2770,12 +2806,17 @@ Object {
"description": "Include a polyfill for the 'global' variable.",
"multiple": false,
"path": "module.parser.javascript.node.global",
"type": "boolean",
"type": "enum",
"values": Array [
false,
true,
"warn",
],
},
],
"description": "Include a polyfill for the 'global' variable.",
"multiple": false,
"simpleType": "boolean",
"simpleType": "string",
},
"module-parser-javascript-require-context": Object {
"configs": Array [
@ -3822,6 +3863,7 @@ Object {
"values": Array [
false,
true,
"warn-mock",
"mock",
"eval-only",
],
@ -3841,6 +3883,7 @@ Object {
"values": Array [
false,
true,
"warn-mock",
"mock",
"eval-only",
],
@ -3856,12 +3899,17 @@ Object {
"description": "Include a polyfill for the 'global' variable.",
"multiple": false,
"path": "node.global",
"type": "boolean",
"type": "enum",
"values": Array [
false,
true,
"warn",
],
},
],
"description": "Include a polyfill for the 'global' variable.",
"multiple": false,
"simpleType": "boolean",
"simpleType": "string",
},
"optimization-check-wasm-types": Object {
"configs": Array [

View File

@ -0,0 +1,13 @@
import "./no-warn"
it("global", () => {
expect(typeof global).toBe("object");
});
it("__filename", () => {
expect(typeof __filename).toBe("string");
});
it("__dirname", () => {
expect(typeof __dirname).toBe("string");
});

View File

@ -0,0 +1,3 @@
// top level global as identifier should not warn
let global = 1;
global = 2;

View File

@ -0,0 +1,10 @@
module.exports = [
[/"global" has been used, it will be undefined in next major version/],
[/"__filename" has been used, it will be undefined in next major version/],
[/"__dirname" has been used, it will be undefined in next major version/],
[/"global" has been used, it will be undefined in next major version/],
[/"__filename" has been used, it will be undefined in next major version/],
[/"__dirname" has been used, it will be undefined in next major version/],
];

View File

@ -0,0 +1,27 @@
/** @type {import("../../../../").Configuration[]} */
module.exports = [
{
target: "web",
optimization: false,
experiments: {
futureDefaults: true
}
},
{
target: "web",
optimization: false,
node: {
__filename: "mock",
__dirname: "mock",
global: "warn"
}
},
{
target: "web",
node: {
__filename: "warn-mock",
__dirname: "warn-mock",
global: true
}
}
];

15
types.d.ts vendored
View File

@ -3282,6 +3282,11 @@ declare interface Experiments {
*/
buildHttp?: boolean | HttpUriOptions;
/**
* Apply defaults of next major version.
*/
futureDefaults?: boolean;
/**
* Enable module and chunk layers.
*/
@ -4438,13 +4443,13 @@ type IgnorePluginOptions =
/**
* A RegExp to test the request against.
*/
resourceRegExp?: RegExp;
resourceRegExp: RegExp;
}
| {
/**
* A filter function for resource and context.
*/
checkResource?: (resource: string, context: string) => boolean;
checkResource: (resource: string, context: string) => boolean;
};
declare interface ImportModuleOptions {
/**
@ -7156,17 +7161,17 @@ declare interface NodeOptions {
/**
* Include a polyfill for the '__dirname' variable.
*/
__dirname?: boolean | "mock" | "eval-only";
__dirname?: boolean | "warn-mock" | "mock" | "eval-only";
/**
* Include a polyfill for the '__filename' variable.
*/
__filename?: boolean | "mock" | "eval-only";
__filename?: boolean | "warn-mock" | "mock" | "eval-only";
/**
* Include a polyfill for the 'global' variable.
*/
global?: boolean;
global?: boolean | "warn";
}
declare class NodeSourcePlugin {
constructor();

View File

@ -759,17 +759,6 @@
source-map "^0.6.1"
write-file-atomic "^3.0.0"
"@jest/types@^26.6.2":
version "26.6.2"
resolved "https://registry.yarnpkg.com/@jest/types/-/types-26.6.2.tgz#bef5a532030e1d88a2f5a6d933f84e97226ed48e"
integrity sha512-fC6QCp7Sc5sX6g8Tvbmj4XUTbyrik0akgRy03yjXbQaBWWNWGE7SGtJk98m0N8nzegD/7SggrUlivxo5ax4KWQ==
dependencies:
"@types/istanbul-lib-coverage" "^2.0.0"
"@types/istanbul-reports" "^3.0.0"
"@types/node" "*"
"@types/yargs" "^15.0.0"
chalk "^4.0.0"
"@jest/types@^27.1.0":
version "27.1.0"
resolved "https://registry.yarnpkg.com/@jest/types/-/types-27.1.0.tgz#674a40325eab23c857ebc0689e7e191a3c5b10cc"
@ -935,13 +924,13 @@
dependencies:
"@types/istanbul-lib-report" "*"
"@types/jest@^26.0.24":
version "26.0.24"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-26.0.24.tgz#943d11976b16739185913a1936e0de0c4a7d595a"
integrity sha512-E/X5Vib8BWqZNRlDxj9vYXhsDwPYbPINqKF9BsnSoon4RQ0D9moEuLD8txgyypFLH7J4+Lho9Nr/c8H0Fi+17w==
"@types/jest@^27.0.1":
version "27.0.1"
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-27.0.1.tgz#fafcc997da0135865311bb1215ba16dba6bdf4ca"
integrity sha512-HTLpVXHrY69556ozYkcq47TtQJXpcWAWfkoqz+ZGz2JnmZhzlRjprCIyFnetSy8gpDWwTTGBcRVv1J1I1vBrHw==
dependencies:
jest-diff "^26.0.0"
pretty-format "^26.0.0"
jest-diff "^27.0.0"
pretty-format "^27.0.0"
"@types/json-schema@*", "@types/json-schema@^7.0.3", "@types/json-schema@^7.0.4", "@types/json-schema@^7.0.5", "@types/json-schema@^7.0.8":
version "7.0.8"
@ -988,13 +977,6 @@
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.1.tgz#3b9ce2489919d9e4fea439b76916abc34b2df129"
integrity sha512-7tFImggNeNBVMsn0vLrpn1H1uPrUBdnARPTpZoitY37ZrdJREzf7I16tMrlK3hen349gr1NYh8CmZQa7CTG6Aw==
"@types/yargs@^15.0.0":
version "15.0.14"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-15.0.14.tgz#26d821ddb89e70492160b66d10a0eb6df8f6fb06"
integrity sha512-yEJzHoxf6SyQGhBhIYGXQDSCkJjB6HohDShto7m8vaKg9Yp0Yn8+71J9eakh2bnPg6BfsH9PRMhiRTZnd4eXGQ==
dependencies:
"@types/yargs-parser" "*"
"@types/yargs@^16.0.0":
version "16.0.4"
resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-16.0.4.tgz#26aad98dd2c2a38e421086ea9ad42b9e51642977"
@ -2185,11 +2167,6 @@ detect-newline@^3.0.0:
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
integrity sha512-TLz+x/vEXm/Y7P7wn1EJFNLxYpUD4TgMosxY6fAVJUnJMbupHBOncxyWUG9OpTaH9EBD7uFI5LfEgmMOc54DsA==
diff-sequences@^26.6.2:
version "26.6.2"
resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-26.6.2.tgz#48ba99157de1923412eed41db6b6d4aa9ca7c0b1"
integrity sha512-Mv/TDa3nZ9sbc5soK+OoA74BsS3mL37yixCvUAQkiuA4Wz6YtwP/K47n2rv2ovzHZvoiQeA5FTQOschKkEwB0Q==
diff-sequences@^27.0.6:
version "27.0.6"
resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-27.0.6.tgz#3305cb2e55a033924054695cc66019fd7f8e5723"
@ -3515,17 +3492,7 @@ jest-config@^27.1.0:
micromatch "^4.0.4"
pretty-format "^27.1.0"
jest-diff@^26.0.0:
version "26.6.2"
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-26.6.2.tgz#1aa7468b52c3a68d7d5c5fdcdfcd5e49bd164394"
integrity sha512-6m+9Z3Gv9wN0WFVasqjCL/06+EFCMTqDEUl/b87HYK2rAPTyfz4ZIuSlPhY51PIQRWx5TaxeF1qmXKe9gfN3sA==
dependencies:
chalk "^4.0.0"
diff-sequences "^26.6.2"
jest-get-type "^26.3.0"
pretty-format "^26.6.2"
jest-diff@^27.0.2, jest-diff@^27.1.0:
jest-diff@^27.0.0, jest-diff@^27.0.2, jest-diff@^27.1.0:
version "27.1.0"
resolved "https://registry.yarnpkg.com/jest-diff/-/jest-diff-27.1.0.tgz#c7033f25add95e2218f3c7f4c3d7b634ab6b3cd2"
integrity sha512-rjfopEYl58g/SZTsQFmspBODvMSytL16I+cirnScWTLkQVXYVZfxm78DFfdIIXc05RCYuGjxJqrdyG4PIFzcJg==
@ -3578,11 +3545,6 @@ jest-environment-node@^27.1.0:
jest-mock "^27.1.0"
jest-util "^27.1.0"
jest-get-type@^26.3.0:
version "26.3.0"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-26.3.0.tgz#e97dc3c3f53c2b406ca7afaed4493b1d099199e0"
integrity sha512-TpfaviN1R2pQWkIihlfEanwOXK0zcxrKEE4MlU6Tn7keoXdN6/3gK/xl0yEh8DOunn5pOVGKf8hB4R9gVh04ig==
jest-get-type@^27.0.6:
version "27.0.6"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-27.0.6.tgz#0eb5c7f755854279ce9b68a9f1a4122f69047cfe"
@ -4854,17 +4816,7 @@ prettier@^2.0.5, prettier@^2.2.0:
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.4.0.tgz#85bdfe0f70c3e777cf13a4ffff39713ca6f64cba"
integrity sha512-DsEPLY1dE5HF3BxCRBmD4uYZ+5DCbvatnolqTqcxEgKVZnL2kUfyu7b8pPQ5+hTBkdhU9SLUmK0/pHb07RE4WQ==
pretty-format@^26.0.0, pretty-format@^26.6.2:
version "26.6.2"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-26.6.2.tgz#e35c2705f14cb7fe2fe94fa078345b444120fc93"
integrity sha512-7AeGuCYNGmycyQbCqd/3PWH4eOoX/OiCa0uphp57NVTeAGdJGaAliecxwBDHYQCIvrW7aDBZCYeNTP/WX69mkg==
dependencies:
"@jest/types" "^26.6.2"
ansi-regex "^5.0.0"
ansi-styles "^4.0.0"
react-is "^17.0.1"
pretty-format@^27.0.2, pretty-format@^27.1.0:
pretty-format@^27.0.0, pretty-format@^27.0.2, pretty-format@^27.1.0:
version "27.1.0"
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-27.1.0.tgz#022f3fdb19121e0a2612f3cff8d724431461b9ca"
integrity sha512-4aGaud3w3rxAO6OXmK3fwBFQ0bctIOG3/if+jYEFGNGIs0EvuidQm3bZ9mlP2/t9epLNC/12czabfy7TZNSwVA==
@ -5409,9 +5361,9 @@ signal-exit@^3.0.2, signal-exit@^3.0.3:
integrity sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA==
simple-git@^2.17.0:
version "2.45.0"
resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-2.45.0.tgz#4d53b146fc23496099ebfc7af5b0d605d0e3e504"
integrity sha512-wu/Ujs9IXn0HuyYm4HyRvne+EKsjJSWKEMkB3wQa3gNHSMHt7y3oeNX9zRQ3UBPk7bRRMLLHAdIZCZfHT9ehPg==
version "2.45.1"
resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-2.45.1.tgz#27d26ae59f734ffd7e1dea16a1ee3b309d68f5ef"
integrity sha512-NmEoThiLTJxl26WNtZxtJTue18ReTcSrf3so5vJG/O8KY9uMxH+yAhXV/DElBJyOYZrrBbVsH8JOFxgENdc9Xg==
dependencies:
"@kwsites/file-exists" "^1.1.1"
"@kwsites/promise-deferred" "^1.1.1"