8000 Merge pull request #14653 from webpack/feature/no-back-compat · webpack/webpack@122db57 · GitHub
[go: up one dir, main page]

Skip to content

Commit 122db57

Browse files
authored
Merge pull request #14653 from webpack/feature/no-back-compat
allow to disable some deprecations
2 parents 95b101f + 43f6109 commit 122db57

16 files changed

+923
-856
lines changed

declarations/WebpackOptions.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2738,14 +2738,14 @@ export interface EntryStaticNormalized {
27382738
* Enables/Disables experiments (experimental features with relax SemVer compatibility).
27392739
*/
27402740
export interface ExperimentsCommon {
2741-
/**
2742-
* Allow module type 'asset' to generate assets.
2743-
*/
2744-
asset?: boolean;
27452741
/**
27462742
* Support WebAssembly as asynchronous EcmaScript Module.
27472743
*/
27482744
asyncWebAssembly?: boolean;
2745+
/**
2746+
* Enable backward-compat layer with deprecation warnings for many webpack 4 APIs.
2747+
*/
2748+
backCompat?: boolean;
27492749
/**
27502750
* Enable additional in memory caching of modules that are unchanged and reference only unchanged modules.
27512751
*/
@@ -2755,7 +2755,7 @@ export interface ExperimentsCommon {
27552755
*/
27562756
futureDefaults?: boolean;
27572757
/**
2758-
* Enable module and chunk layers.
2758+
* Enable module layers.
27592759
*/
27602760
layers?: boolean;
27612761
/**

lib/Chunk.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ let debugId = 1000;
6363
class Chunk {
6464
/**
6565
* @param {string=} name of chunk being created, is optional (for subclasses)
66+
* @param {boolean} backCompat enable backward-compatibility
6667
*/
67-
constructor(name) {
68+
constructor(name, backCompat = true) {
6869
/** @type {number | string | null} */
6970
this.id = null;
7071
/** @type {(number|string)[] | null} */
@@ -84,7 +85,7 @@ class Chunk {
8485
/** @type {RuntimeSpec} */
8586
this.runtime = undefined;
8687
/** @type {Set<string>} */
87-
this.files = new ChunkFilesSet();
88+
this.files = backCompat ? new ChunkFilesSet() : new Set();
8889
/** @type {Set<string>} */
8990
this.auxiliaryFiles = new Set();
9091
/** @type {boolean} */

lib/Compilation.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ class Compilation {
429429
* @param {CompilationParams} params the compilation parameters
430430
*/
431431
constructor(compiler, params) {
432+
this._backCompat = compiler._backCompat;
433+
432434
const getNormalModuleLoader = () => deprecatedNormalModuleLoaderHook(this);
433435
/** @typedef {{ additionalAssets?: true | Function }} ProcessAssetsAdditionalOptions */
434436
/** @type {AsyncSeriesHook<[CompilationAssets], ProcessAssetsAdditionalOptions>} */
@@ -559,6 +561,7 @@ class Compilation {
559561
* @returns {FakeHook<Pick<AsyncSeriesHook<T>, "tap" | "tapAsync" | "tapPromise" | "name">>} fake hook which redirects
560562
*/
561563
const createProcessAssetsHook = (name, stage, getArgs, code) => {
564+
if (!this._backCompat && code) return undefined;
562565
const errorMessage =
563566
reason => `Can't automatically convert plugin using Compilation.hooks.${name} to Compilation.hooks.processAssets because ${reason}.
564567
BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a single Compilation.hooks.processAssets hook.`;
@@ -983,7 +986,6 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
983986
this.asyncEntrypoints = [];
984987
/** @type {Set<Chunk>} */
985988
this.chunks = new Set();
986-
arrayToSetDeprecation(this.chunks, "Compilation.chunks");
987989
/** @type {ChunkGroup[]} */
988990
this.chunkGroups = [];
989991
/** @type {Map<string, ChunkGroup>} */
@@ -992,7 +994,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
992994
this.namedChunks = new Map();
993995
/** @type {Set<Module>} */
994996
this.modules = new Set();
995-
arrayToSetDeprecation(this.modules, "Compilation.modules");
997+
if (this._backCompat) {
998+
arrayToSetDeprecation(this.chunks, "Compilation.chunks");
999+
arrayToSetDeprecation(this.modules, "Compilation.modules");
1000+
}
9961001
/** @private @type {Map<string, Module>} */
9971002
this._modules = new Map();
9981003
this.records = null;
@@ -1286,7 +1291,8 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
12861291
}
12871292
this._modules.set(identifier, module);
12881293
this.modules.add(module);
1289-
ModuleGraph.setModuleGraphForModule(module, this.moduleGraph);
1294+
if (this._backCompat)
1295+
ModuleGraph.setModuleGraphForModule(module, this.moduleGraph);
12901296
if (currentProfile !== undefined) {
12911297
currentProfile.markIntegrationEnd();
12921298
}
@@ -1701,7 +1707,8 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
17011707

17021708
this._modules.set(module.identifier(), module);
17031709
this.modules.add(module);
1704-
ModuleGraph.setModuleGraphForModule(module, this.moduleGraph);
1710+
if (this._backCompat)
1711+
ModuleGraph.setModuleGraphForModule(module, this.moduleGraph);
17051712

17061713
this._handleModuleBuildAndDependencies(
17071714
originModule,
@@ -2777,8 +2784,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
27772784
);
27782785
this.chunkGraph = chunkGraph;
27792786

2780-
for (const module of this.modules) {
2781-
ChunkGraph.setChunkGraphForModule(module, chunkGraph);
2787+
if (this._backCompat) {
2788+
for (const module of this.modules) {
2789+
ChunkGraph.setChunkGraphForModule(module, chunkGraph);
2790+
}
27822791
}
27832792

27842793
this.hooks.seal.call();
@@ -3045,14 +3054,16 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
30453054
}
30463055
this.hooks.afterProcessAssets.call(this.assets);
30473056
this.logger.timeEnd("process assets");
3048-
this.assets = soonFrozenObjectDeprecation(
3049-
this.assets,
3050-
"Compilation.assets",
3051-
"DEP_WEBPACK_COMPILATION_ASSETS",
3052-
`BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
3057+
this.assets = this._backCompat
3058+
? soonFrozenObjectDeprecation(
3059+
this.assets,
3060+
"Compilation.assets",
3061+
"DEP_WEBPACK_COMPILATION_ASSETS",
3062+
`BREAKING CHANGE: No more changes should happen to Compilation.assets after sealing the Compilation.
30533063
Do changes to assets earlier, e. g. in Compilation.hooks.processAssets.
30543064
Make sure to select an appropriate stage from Compilation.PROCESS_ASSETS_STAGE_*.`
3055-
);
3065+
)
3066+
: Object.freeze(this.assets);
30563067

30573068
this.summarizeDependencies();
30583069
if (shouldRecord) {
@@ -3453,7 +3464,8 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
34533464
*/
34543465
addRuntimeModule(chunk, module, chunkGraph = this.chunkGraph) {
34553466
// Deprecated ModuleGraph association
3456-
ModuleGraph.setModuleGraphForModule(module, this.moduleGraph);
3467+
if (this._backCompat)
3468+
ModuleGraph.setModuleGraphForModule(module, this.moduleGraph);
34573469

34583470
// add it to the list
34593471
this.modules.add(module);
@@ -3589,9 +3601,10 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
35893601
return chunk;
35903602
}
35913603
}
3592-
const chunk = new Chunk(name);
3604+
const chunk = new Chunk(name, this._backCompat);
35933605
this.chunks.add(chunk);
3594-
ChunkGraph.setChunkGraphForChunk(chunk, this.chunkGraph);
3606+
if (this._backCompat)
3607+
ChunkGraph.setChunkGraphForChunk(chunk, this.chunkGraph);
35953608
if (name) {
35963609
this.namedChunks.set(name, chunk);
35973610
}
@@ -4715,7 +4728,7 @@ This prevents using hashes of each other and should be avoided.`);
47154728
this.outputOptions;
47164729
const runtimeTemplate = this.runtimeTemplate;
47174730

4718-
const chunk = new Chunk("build time chunk");
4731+
const chunk = new Chunk("build time chunk", this._backCompat);
47194732
chunk.id = chunk.name;
47204733
chunk.ids = [chunk.id];
47214734
chunk.runtime = runtime;

lib/Compiler.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ const includesHash = (filename, hashes) => {
119119
class Compiler {
120120
/**
121121
* @param {string} context the compilation path
122+
* @param {WebpackOptions} options options
122123
*/
123-
constructor(context) {
124+
constructor(context, options = /** @type {WebpackOptions} */ ({})) {
124125
this.hooks = Object.freeze({
125126
/** @type {SyncHook<[]>} */
126127
initialize: new SyncHook([]),
@@ -240,8 +241,7 @@ class Compiler {
240241

241242
this.infrastructureLogger = undefined;
242243

243-
/** @type {WebpackOptions} */
244-
this.options = /** @type {WebpackOptions} */ ({});
244+
this.options = options;
245245

246246
this.context = context;
247247

@@ -263,6 +263,8 @@ class Compiler {
263263
/** @type {boolean} */
264264
this.watchMode = false;
265265

266+
this._backCompat = this.options.experiments.backCompat !== false;
267+
266268
/** @type {Compilation} */
267269
this._lastCompilation = undefined;
268270
/** @type {NormalModuleFactory} */
@@ -963,7 +965,13 @@ ${other}`);
963965
outputOptions,
964966
plugins
965967
) {
966-
const childCompiler = new Compiler(this.context);
968+
const childCompiler = new Compiler(this.context, {
969+
...this.options,
970+
output: {
971+
...this.options.output,
972+
...outputOptions
973+
}
974+
});
967975
childCompiler.name = compilerName;
968976
childCompiler.outputPath = this.outputPath;
969977
childCompiler.inputFileSystem = this.inputFileSystem;
@@ -976,6 +984,7 @@ ${other}`);
976984
childCompiler.fsStartTime = this.fsStartTime;
977985
childCompiler.cache = this.cache;
978986
childCompiler.compilerPath = `${this.compilerPath}${compilerName}|${compilerIndex}|`;
987+
childCompiler._backCompat = this._backCompat;
979988

980989
const relativeCompilerName = makePathsRelative(
981990
this.context,
@@ -991,13 +1000,6 @@ ${other}`);
9911000
this.records[relativeCompilerName].push((childCompiler.records = {}));
9921001
}
9931002

994-
childCompiler.options = {
995-
...this.options,
996-
output: {
997-
...this.options.output,
998-
...outputOptions
999-
}
1000-
};
10011003
childCompiler.parentCompilation = compilation;
10021004
childCompiler.root = this.root;
10031005
if (Array.isArray(plugins)) {

lib/HotModuleReplacementPlugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class HotModuleReplacementPlugin {
8383
* @returns {void}
8484
*/
8585
apply(compiler) {
86+
const { _backCompat: backCompat } = compiler;
8687
if (compiler.options.output.strictModuleErrorHandling === undefined)
8788
compiler.options.output.strictModuleErrorHandling = true;
8889
const runtimeRequirements = [RuntimeGlobals.module];
@@ -597,7 +598,8 @@ class HotModuleReplacementPlugin {
597598
(newRuntimeModules && newRuntimeModules.length > 0)
598599
) {
599600
const hotUpdateChunk = new HotUpdateChunk();
600-
ChunkGraph.setChunkGraphForChunk(hotUpdateChunk, chunkGraph);
601+
if (backCompat)
602+
ChunkGraph.setChunkGraphForChunk(hotUpdateChunk, chunkGraph);
601603
hotUpdateChunk.id = chunkId;
602604
hotUpdateChunk.runtime = newRuntime;
603605
if (currentChunk) {

lib/config/defaults.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,15 @@ const applyWebpackOptionsDefaults = options => {
265265
* @returns {void}
266266
*/
267267
const applyExperimentsDefaults = (experiments, { production, development }) => {
268-
D(experiments, "topLevelAwait", false);
268+
D(experiments, "futureDefaults", false);
269+
D(experiments, "backCompat", !experiments.futureDefaults);
270+
D(experiments, "topLevelAwait", experiments.futureDefaults);
269271
D(experiments, "syncWebAssembly", false);
270-
D(experiments, "asyncWebAssembly", false);
272+
D(experiments, "asyncWebAssembly", experiments.futureDefaults);
271273
D(experiments, "outputModule", false);
272-
D(experiments, "asset", false);
273274
D(experiments, "layers", false);
274275
D(experiments, "lazyCompilation", undefined);
275276
D(experiments, "buildHttp", undefined);
276-
D(experiments, "futureDefaults", false);
277277
D(experiments, "cacheUnaffected", experiments.futureDefaults);
278278

279279
if (typeof experiments.buildHttp === "object") {

lib/optimize/ModuleConcatenationPlugin.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class ModuleConcatenationPlugin {
5656
* @returns {void}
5757
*/
5858
apply(compiler) {
59+
const { _backCompat: backCompat } = compiler;
5960
compiler.hooks.compilation.tap("ModuleConcatenationPlugin", compilation => {
6061
const moduleGraph = compilation.moduleGraph;
6162
const bailoutReasonMap = new Map();
@@ -389,8 +390,10 @@ class ModuleConcatenationPlugin {
389390
};
390391

391392
const integrate = () => {
392-
ChunkGraph.setChunkGraphForModule(newModule, chunkGraph);
393-
ModuleGraph.setModuleGraphForModule(newModule, moduleGraph);
393+
if (backCompat) {
394+
ChunkGraph.setChunkGraphForModule(newModule, chunkGraph);
395+
ModuleGraph.setModuleGraphForModule(newModule, moduleGraph);
396+
}
394397

395398
for (const warning of concatConfiguration.getWarningsSorted()) {
396399
moduleGraph

lib/util/deprecation.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,16 @@ exports.arrayToSetDeprecation = (set, name) => {
165165
};
166166

167167
exports.createArrayToSetDeprecationSet = name => {
168-
class SetDeprecatedArray extends Set {}
169-
exports.arrayToSetDeprecation(SetDeprecatedArray.prototype, name);
168+
let initialized = false;
169+
class SetDeprecatedArray extends Set {
170+
constructor(items) {
171+
super(items);
172+
if (!initialized) {
173+
initialized = true;
174+
exports.arrayToSetDeprecation(SetDeprecatedArray.prototype, name);
175+
}
176+
}
177+
}
170178
return SetDeprecatedArray;
171179
};
172180

lib/webpack.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ const createMultiCompiler = (childOptions, options) => {
6161
const createCompiler = rawOptions => {
6262
const options = getNormalizedWebpackOptions(rawOptions);
6363
applyWebpackOptionsBaseDefaults(options);
64-
const compiler = new Compiler(options.context);
65-
compiler.options = options;
64+
const compiler = new Compiler(options.context, options);
6665
new NodeEnvironmentPlugin({
6766
infrastructureLogging: options.infrastructureLogging
6867
}).apply(compiler);

schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)