Skip to content

Commit

Permalink
Merge pull request #17048 from webpack/define-plugin-logging
Browse files Browse the repository at this point in the history
feat: added logging to define plugin
  • Loading branch information
alexander-akait committed Apr 25, 2023
2 parents cc431cf + 7a60896 commit ec87953
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 44 deletions.
125 changes: 81 additions & 44 deletions lib/DefinePlugin.js
Expand Up @@ -26,6 +26,7 @@ const createHash = require("./util/createHash");
/** @typedef {import("./NormalModule")} NormalModule */
/** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
/** @typedef {import("./javascript/JavascriptParser")} JavascriptParser */
/** @typedef {import("./logging/Logger").Logger} Logger */

/** @typedef {null|undefined|RegExp|Function|string|number|boolean|bigint|undefined} CodeValuePrimitive */
/** @typedef {RecursiveArrayOrRecord<CodeValuePrimitive|RuntimeValue>} CodeValue */
Expand Down Expand Up @@ -117,6 +118,7 @@ class RuntimeValue {
* @param {Map<string, string | Set<string>>} valueCacheVersions valueCacheVersions
* @param {string} key the defined key
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {Logger} logger the logger object
* @param {boolean|undefined|null=} asiSafe asi safe (undefined: unknown, null: unneeded)
* @param {Set<string>|undefined=} objKeys used keys
* @returns {string} code converted to string that evaluates
Expand All @@ -127,6 +129,7 @@ const stringifyObj = (
valueCacheVersions,
key,
runtimeTemplate,
logger,
asiSafe,
objKeys
) => {
Expand All @@ -135,7 +138,15 @@ const stringifyObj = (
if (arr) {
code = `[${obj
.map(code =>
toCode(code, parser, valueCacheVersions, key, runtimeTemplate, null)
toCode(
code,
parser,
valueCacheVersions,
key,
runtimeTemplate,
logger,
null
)
)
.join(",")}]`;
} else {
Expand All @@ -150,7 +161,15 @@ const stringifyObj = (
return (
JSON.stringify(key) +
":" +
toCode(code, parser, valueCacheVersions, key, runtimeTemplate, null)
toCode(
code,
parser,
valueCacheVersions,
key,
runtimeTemplate,
logger,
null
)
);
})
.join(",")}}`;
Expand All @@ -175,6 +194,7 @@ const stringifyObj = (
* @param {Map<string, string | Set<string>>} valueCacheVersions valueCacheVersions
* @param {string} key the defined key
* @param {RuntimeTemplate} runtimeTemplate the runtime template
* @param {Logger} logger the logger object
* @param {boolean|undefined|null=} asiSafe asi safe (undefined: unknown, null: unneeded)
* @param {Set<string>|undefined=} objKeys used keys
* @returns {string} code converted to string that evaluates
Expand All @@ -185,51 +205,62 @@ const toCode = (
valueCacheVersions,
key,
runtimeTemplate,
logger,
asiSafe,
objKeys
) => {
if (code === null) {
return "null";
}
if (code === undefined) {
return "undefined";
}
if (Object.is(code, -0)) {
return "-0";
}
if (code instanceof RuntimeValue) {
return toCode(
code.exec(parser, valueCacheVersions, key),
parser,
valueCacheVersions,
key,
runtimeTemplate,
asiSafe
);
}
if (code instanceof RegExp && code.toString) {
return code.toString();
}
if (typeof code === "function" && code.toString) {
return "(" + code.toString() + ")";
}
if (typeof code === "object") {
return stringifyObj(
code,
parser,
valueCacheVersions,
key,
runtimeTemplate,
asiSafe,
objKeys
);
}
if (typeof code === "bigint") {
return runtimeTemplate.supportsBigIntLiteral()
? `${code}n`
: `BigInt("${code}")`;
}
return code + "";
const transformToCode = () => {
if (code === null) {
return "null";
}
if (code === undefined) {
return "undefined";
}
if (Object.is(code, -0)) {
return "-0";
}
if (code instanceof RuntimeValue) {
return toCode(
code.exec(parser, valueCacheVersions, key),
parser,
valueCacheVersions,
key,
runtimeTemplate,
logger,
asiSafe
);
}
if (code instanceof RegExp && code.toString) {
return code.toString();
}
if (typeof code === "function" && code.toString) {
return "(" + code.toString() + ")";
}
if (typeof code === "object") {
return stringifyObj(
code,
parser,
valueCacheVersions,
key,
runtimeTemplate,
logger,
asiSafe,
objKeys
);
}
if (typeof code === "bigint") {
return runtimeTemplate.supportsBigIntLiteral()
? `${code}n`
: `BigInt("${code}")`;
}
return code + "";
};

const strCode = transformToCode();

logger.log(`Replaced "${key}" with "${strCode}"`);

return strCode;
};

const toCacheVersion = code => {
Expand Down Expand Up @@ -300,6 +331,7 @@ class DefinePlugin {
compiler.hooks.compilation.tap(
PLUGIN_NAME,
(compilation, { normalModuleFactory }) => {
const logger = compilation.getLogger("webpack.DefinePlugin");
compilation.dependencyTemplates.set(
ConstDependency,
new ConstDependency.Template()
Expand Down Expand Up @@ -421,6 +453,7 @@ class DefinePlugin {
compilation.valueCacheVersions,
key,
runtimeTemplate,
logger,
null
)
);
Expand All @@ -436,6 +469,7 @@ class DefinePlugin {
compilation.valueCacheVersions,
originalKey,
runtimeTemplate,
logger,
!parser.isAsiPosition(expr.range[0]),
parser.destructuringAssignmentPropertiesFor(expr)
);
Expand Down Expand Up @@ -470,6 +504,7 @@ class DefinePlugin {
compilation.valueCacheVersions,
originalKey,
runtimeTemplate,
logger,
null
);
const typeofCode = isTypeof
Expand All @@ -488,6 +523,7 @@ class DefinePlugin {
compilation.valueCacheVersions,
originalKey,
runtimeTemplate,
logger,
null
);
const typeofCode = isTypeof
Expand Down Expand Up @@ -534,6 +570,7 @@ class DefinePlugin {
compilation.valueCacheVersions,
key,
runtimeTemplate,
logger,
!parser.isAsiPosition(expr.range[0]),
parser.destructuringAssignmentPropertiesFor(expr)
);
Expand Down
8 changes: 8 additions & 0 deletions test/__snapshots__/StatsTestCases.basictest.js.snap
Expand Up @@ -860,6 +860,14 @@ webpack x.x.x compiled successfully in X ms
asset both.js 1.4 KiB [emitted] (name: main)
./index.js 24 bytes [built] [code generated]
webpack x.x.x compiled successfully in X ms
asset 123.js 1.4 KiB [emitted] (name: main)
./index.js 24 bytes [built] [code generated]
DEBUG LOG from webpack.DefinePlugin
Replaced \\"VALUE\\" with \\"123\\"
webpack x.x.x compiled successfully in X ms"
`;

Expand Down
21 changes: 21 additions & 0 deletions test/statsCases/define-plugin/webpack.config.js
Expand Up @@ -56,5 +56,26 @@ module.exports = [
)
})
]
},

{
mode: "production",
entry: "./index",
output: {
filename: "123.js"
},
infrastructureLogging: {
debug: /DefinePlugin/,
level: "none"
},
stats: {
loggingDebug: /DefinePlugin/,
logging: "none"
},
plugins: [
new webpack.DefinePlugin({
VALUE: "123"
})
]
}
];

0 comments on commit ec87953

Please sign in to comment.