Skip to content

Commit 568b752

Browse files
committed
feat: support export result to json and support not showing logs
1 parent 3d1983c commit 568b752

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,15 @@ Whether to run unused export detection or not.
138138
#### options.log (default: `"all"`)
139139
140140
`"all"`: show all messages.
141+
141142
`"unused"`: only show messages when there are either unused files or unused export.
142143
144+
`"none"`: won't show unused files or unused export messages in the terminal, it can keep terminal clean when set `exportJSON` to `true`
145+
146+
#### options.exportJSON (default: `false`)
147+
148+
When set to `true`, it will export the unused files and unused export to a JSON file called `deadcode.json` at the root of the project.
149+
143150
[npm]: https://img.shields.io/npm/v/webpack-deadcode-plugin.svg
144151
[npm-url]: https://npmjs.com/package/webpack-deadcode-plugin
145152
[node]: https://img.shields.io/node/v/webpack-deadcode-plugin.svg

samples/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const webpack = require("webpack");
2-
const DeadCodePlugin = require("webpack-deadcode-plugin");
2+
const DeadCodePlugin = require("../src/index");
33
const HtmlWebpackPlugin = require("html-webpack-plugin");
44

55
module.exports = {

samples/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -921,9 +921,9 @@ camelcase@^5.0.0:
921921
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
922922

923923
caniuse-lite@^1.0.30001254:
924-
version "1.0.30001257"
925-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001257.tgz#150aaf649a48bee531104cfeda57f92ce587f6e5"
926-
integrity sha512-JN49KplOgHSXpIsVSF+LUyhD8PUp6xPpAXeRrrcBh4KBeP7W864jHn6RvzJgDlrReyeVjMFJL3PLpPvKIxlIHA==
924+
version "1.0.30001356"
925+
resolved "https://registry.npmmirror.com/caniuse-lite/-/caniuse-lite-1.0.30001356.tgz"
926+
integrity sha512-/30854bktMLhxtjieIxsrJBfs2gTM1pel6MXKF3K+RdIVJZcsn2A2QdhsuR4/p9+R204fZw0zCBBhktX8xWuyQ==
927927

928928
chalk@^2.0.0:
929929
version "2.4.2"
@@ -4057,7 +4057,7 @@ webpack-cli@4.8.0:
40574057
webpack-merge "^5.7.3"
40584058

40594059
webpack-deadcode-plugin@../:
4060-
version "0.1.15"
4060+
version "0.1.16"
40614061
dependencies:
40624062
chalk "^3.0.0"
40634063
fast-glob "^3.1.1"

src/detect.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require("path");
22
const chalk = require("chalk");
3+
const fs = require("fs");
34
const fg = require("fast-glob");
45

56
function detectDeadCode(compilation, options) {
@@ -14,26 +15,56 @@ function detectDeadCode(compilation, options) {
1415
if (options.detectUnusedFiles) {
1516
unusedFiles = includedFiles.filter(file => !compiledFiles[file]);
1617

17-
if (Object.keys(unusedFiles).length > 0 || options.log === "all") {
18+
if (Object.keys(unusedFiles).length > 0 || options.log !== "none") {
1819
logUnusedFiles(unusedFiles);
1920
}
2021
}
2122

2223
if (options.detectUnusedExport) {
2324
unusedExportMap = getUsedExportMap(convertFilesToDict(includedFiles), compilation, isWebpack5);
2425

25-
if (Object.keys(unusedExportMap).length > 0 || options.log == "all") {
26+
if (Object.keys(unusedExportMap).length > 0 || options.log !== "none") {
2627
logUnusedExportMap(unusedExportMap);
2728
}
2829
}
2930

31+
if (options.exportJSON) {
32+
let exportPath = "deadcode.json";
33+
try {
34+
fs.stat(exportPath, err => {
35+
if (err == null) {
36+
fs.unlinkSync(exportPath);
37+
return exportResultToJSON(exportPath, unusedFiles, unusedExportMap);
38+
}
39+
if (err.code === "ENOENT") {
40+
return exportResultToJSON(exportPath, unusedFiles, unusedExportMap);
41+
}
42+
});
43+
} catch (error) {
44+
console.error("export result to json error: ", error);
45+
}
46+
}
47+
3048
if (unusedFiles.length > 0 || unusedExportMap.length > 0) {
3149
if (options.failOnHint) {
3250
process.exit(2);
3351
}
3452
}
3553
}
3654

55+
function exportResultToJSON(exportPath, unusedFiles, unusedExports) {
56+
const data = {
57+
unusedFiles,
58+
unusedExports,
59+
};
60+
fs.writeFile(exportPath, JSON.stringify(data, null, 2), err => {
61+
if (err) {
62+
throw err;
63+
}
64+
console.log(exportPath + " is generated.");
65+
});
66+
}
67+
3768
function getPattern({ context, patterns, exclude }) {
3869
return patterns
3970
.map(pattern => path.resolve(context, pattern))

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class WebpackDeadcodePlugin {
1616
detectUnusedFiles: true,
1717
detectUnusedExport: true,
1818
log: "all",
19+
exportJSON: false,
1920
},
2021
this.options,
2122
);

0 commit comments

Comments
 (0)