add deprecation codes

This commit is contained in:
Sergey Melyukov 2019-11-14 17:01:25 +03:00
parent 85145cebf3
commit a214be41a0
11 changed files with 69 additions and 32 deletions

View File

@ -1142,7 +1142,8 @@ class ChunkGraph {
);
return chunkGraph;
},
deprecateMessage + ": Use new ChunkGraph API"
deprecateMessage + ": Use new ChunkGraph API",
"DEP_WEBPACK_CHUNK_GRAPH_GET_CHUNK_GRAPH_FOR_MODULE"
);
deprecateGetChunkGraphForModuleMap.set(deprecateMessage, newFn);
return newFn(module);
@ -1181,7 +1182,8 @@ class ChunkGraph {
);
return chunkGraph;
},
deprecateMessage + ": Use new ChunkGraph API"
deprecateMessage + ": Use new ChunkGraph API",
"DEP_WEBPACK_CHUNK_GRAPH_GET_CHUNK_GRAPH_FOR_CHUNK"
);
deprecateGetChunkGraphForChunkMap.set(deprecateMessage, newFn);
return newFn(chunk);

View File

@ -550,12 +550,14 @@ class ChunkGroup {
ChunkGroup.prototype.getModuleIndex = util.deprecate(
ChunkGroup.prototype.getModulePreOrderIndex,
"ChunkGroup.getModuleIndex was renamed to getModulePreOrderIndex"
"ChunkGroup.getModuleIndex was renamed to getModulePreOrderIndex",
"DEP_WEBPACK_CHUNK_GROUP_GET_MODULE_INDEX"
);
ChunkGroup.prototype.getModuleIndex2 = util.deprecate(
ChunkGroup.prototype.getModulePostOrderIndex,
"ChunkGroup.getModuleIndex2 was renamed to getModulePostOrderIndex"
"ChunkGroup.getModuleIndex2 was renamed to getModulePostOrderIndex",
"DEP_WEBPACK_CHUNK_GROUP_GET_MODULE_INDEX_2"
);
module.exports = ChunkGroup;

View File

@ -184,9 +184,13 @@ const { arrayToSetDeprecation } = require("./util/deprecation");
*/
// TODO webpack 6: remove
const deprecatedNormalModuleLoaderHook = util.deprecate(compilation => {
return require("./NormalModule").getCompilationHooks(compilation).loader;
}, "Compilation.hooks.normalModuleLoader was moved to NormalModule.getCompilationHooks(compilation).loader");
const deprecatedNormalModuleLoaderHook = util.deprecate(
compilation => {
return require("./NormalModule").getCompilationHooks(compilation).loader;
},
"Compilation.hooks.normalModuleLoader was moved to NormalModule.getCompilationHooks(compilation).loader",
"DEP_WEBPACK_COMPILATION_NORMAL_MODULE_LOADER_HOOK"
);
const byId = compareSelect(
/**
@ -2173,8 +2177,7 @@ class Compilation {
emitAsset(file, source, assetInfo = {}) {
if (this.assets[file]) {
if (!isSourceEqual(this.assets[file], source)) {
// TODO webpack 5: make this an error instead
this.warnings.push(
this.errors.push(
new WebpackError(
`Conflict: Multiple assets emit different content to the same filename ${file}`
)

View File

@ -5,6 +5,7 @@
"use strict";
const util = require("util");
const ExternalModule = require("./ExternalModule");
const UNSPECIFIED_EXTERNAL_TYPE_REGEXP = /^[a-z0-9]+ /;
@ -104,9 +105,16 @@ class ExternalModuleFactoryPlugin {
}
};
if (externals.length === 3) {
// TODO webpack 5 insert deprecation message here
// TODO webpack 6 remove this
externals.call(null, context, dependency.request, cb);
util
.deprecate(
(context, request, cb) => {
externals.call(null, context, dependency.request, cb);
},
"The externals-function should be defined like ({context, request}, cb) => { ... }",
"DEP_WEBPACK_EXTERNALS_FUNCTION_PARAMETERS"
)
.call(null, context, dependency.request, cb);
} else {
externals(
{

View File

@ -1267,7 +1267,8 @@ class ModuleGraph {
);
return moduleGraph;
},
deprecateMessage + ": Use new ModuleGraph API"
deprecateMessage + ": Use new ModuleGraph API",
"DEP_WEBPACK_MODULE_GRAPH_GET_MODULE_GRAPH_FOR_MODULE"
);
deprecateMap.set(deprecateMessage, newFn);
return newFn(module);

View File

@ -67,10 +67,10 @@ const replacer = (value, allowEmpty) => {
const deprecationCache = new Map();
const deprecatedFunction = (() => () => {})();
const deprecated = (fn, message) => {
const deprecated = (fn, message, code) => {
let d = deprecationCache.get(message);
if (d === undefined) {
d = util.deprecate(deprecatedFunction, message);
d = util.deprecate(deprecatedFunction, message, code);
deprecationCache.set(message, d);
}
return (...args) => {
@ -130,7 +130,11 @@ const replacePathVariables = (path, data, assetInfo) => {
// Legacy
replacements.set(
"filebase",
deprecated(replacer(base), "[filebase] is now [base]")
deprecated(
replacer(base),
"[filebase] is now [base]",
"DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_FILENAME"
)
);
}
}
@ -158,7 +162,8 @@ const replacePathVariables = (path, data, assetInfo) => {
"hash",
deprecated(
hashReplacer,
"[hash] is now [fullhash] (also consider using [chunkhash] or [contenthash], see documentation for details)"
"[hash] is now [fullhash] (also consider using [chunkhash] or [contenthash], see documentation for details)",
"DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_HASH"
)
);
}
@ -237,11 +242,19 @@ const replacePathVariables = (path, data, assetInfo) => {
// Legacy
replacements.set(
"moduleid",
deprecated(idReplacer, "[moduleid] is now [id]")
deprecated(
idReplacer,
"[moduleid] is now [id]",
"DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_MODULE_ID"
)
);
replacements.set(
"modulehash",
deprecated(hashReplacer, "[modulehash] is now [hash]")
deprecated(
hashReplacer,
"[modulehash] is now [hash]",
"DEP_WEBPACK_TEMPLATE_PATH_PLUGIN_REPLACE_PATH_VARIABLES_MODULE_HASH"
)
);
}

View File

@ -78,7 +78,8 @@ exportPlugins(module.exports, {
RuntimeModule: () => require("./RuntimeModule"),
SingleEntryPlugin: util.deprecate(
() => require("./EntryPlugin"),
"SingleEntryPlugin was renamed to EntryPlugin"
"SingleEntryPlugin was renamed to EntryPlugin",
"DEP_WEBPACK_SINGLE_ENTRY_PLUGIN"
),
SetVarTemplatePlugin: () => require("./SetVarTemplatePlugin"),
SourceMapDevToolPlugin: () => require("./SourceMapDevToolPlugin"),
@ -112,7 +113,8 @@ exportPlugins((module.exports.optimize = {}), {
AggressiveMergingPlugin: () => require("./optimize/AggressiveMergingPlugin"),
AggressiveSplittingPlugin: util.deprecate(
() => require("./optimize/AggressiveSplittingPlugin"),
"AggressiveSplittingPlugin is deprecated in favor of SplitChunksPlugin"
"AggressiveSplittingPlugin is deprecated in favor of SplitChunksPlugin",
"DEP_WEBPACK_AGGRESSIVE_SPLITTING_PLUGIN"
),
LimitChunkCountPlugin: () => require("./optimize/LimitChunkCountPlugin"),
MinChunkSizePlugin: () => require("./optimize/MinChunkSizePlugin"),

View File

@ -25,7 +25,8 @@ const InitFragment = require("../InitFragment");
const deprecatedGetInitFragments = util.deprecate(
(template, dependency, templateContext) =>
template.getInitFragments(dependency, templateContext),
"DependencyTemplate.getInitFragment is deprecated (use apply(dep, source, { initFragments }) instead)"
"DependencyTemplate.getInitFragment is deprecated (use apply(dep, source, { initFragments }) instead)",
"DEP_WEBPACK_JAVASCRIPT_GENERATOR_GET_INIT_FRAGMENTS"
);
const TYPES = new Set(["javascript"]);

View File

@ -12,12 +12,17 @@ const deprecationCache = new Map();
/**
* @param {string} message deprecation message
* @param {string} code deprecation code
* @returns {Function} function to trigger deprecation
*/
const createDeprecation = message => {
const createDeprecation = (message, code) => {
const cached = deprecationCache.get(message);
if (cached !== undefined) return cached;
const fn = util.deprecate(() => {}, message);
const fn = util.deprecate(
() => {},
message,
"DEP_WEBPACK_DEPRECATION_" + code
);
deprecationCache.set(message, fn);
return fn;
};
@ -61,7 +66,8 @@ exports.arrayToSetDeprecation = (set, name) => {
for (const method of COPY_METHODS) {
if (set[method]) continue;
const d = createDeprecation(
`${name} was changed from Array to Set (using Array method '${method}' is deprecated)`
`${name} was changed from Array to Set (using Array method '${method}' is deprecated)`,
"ARRAY_TO_SET"
);
/**
* @deprecated
@ -75,13 +81,16 @@ exports.arrayToSetDeprecation = (set, name) => {
};
}
const dPush = createDeprecation(
`${name} was changed from Array to Set (using Array method 'push' is deprecated)`
`${name} was changed from Array to Set (using Array method 'push' is deprecated)`,
"ARRAY_TO_SET_PUSH"
);
const dLength = createDeprecation(
`${name} was changed from Array to Set (using Array property 'length' is deprecated)`
`${name} was changed from Array to Set (using Array property 'length' is deprecated)`,
"ARRAY_TO_SET_LENGTH"
);
const dIndexer = createDeprecation(
`${name} was changed from Array to Set (indexing Array is deprecated)`
`${name} was changed from Array to Set (indexing Array is deprecated)`,
"ARRAY_TO_SET_INDEXER"
);
/**
* @deprecated

View File

@ -414,11 +414,7 @@ class WebAssemblyGenerator extends Generator {
* @returns {Source} generated code
*/
generate(module, { moduleGraph }) {
const source = module.originalSource().source();
// TODO remove this casts when webpack-sources is fixed
// source() should have return type (string | Buffer)
const sourceAsAny = /** @type {TODO} */ (source);
const bin = /** @type {Buffer} */ (sourceAsAny);
const bin = module.originalSource().source();
const initFuncId = t.identifier("");