Skip to content

Commit 769a6a7

Browse files
authored
Merge pull request #58 from hawtim/master
feat: support export result to json and support not showing logs
2 parents 3d1983c + 8ecaa9c commit 769a6a7

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,28 @@ 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+
`false`: won't create `deadcode.json` for the unused files and unused export.
149+
150+
`true`: create `deadcode.json` for the unused files and unused export at the root of the project.
151+
152+
You can set `exportJSON` to a specific path, and it will generate `deadcode.json` in that path.
153+
154+
```js
155+
new DeadCodePlugin({
156+
patterns: ["*.(js|css)"],
157+
exclude: ["**/node_modules/**"],
158+
log: "none",
159+
exportJSON: "./analysis"
160+
}),
161+
```
162+
143163
[npm]: https://img.shields.io/npm/v/webpack-deadcode-plugin.svg
144164
[npm-url]: https://npmjs.com/package/webpack-deadcode-plugin
145165
[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: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const path = require("path");
22
const chalk = require("chalk");
3+
const fs = require("fs");
34
const fg = require("fast-glob");
5+
const getDirName = path.dirname;
46

57
function detectDeadCode(compilation, options) {
68
const isWebpack5 = compilation.chunkGraph ? true : false;
@@ -13,27 +15,60 @@ function detectDeadCode(compilation, options) {
1315

1416
if (options.detectUnusedFiles) {
1517
unusedFiles = includedFiles.filter(file => !compiledFiles[file]);
16-
17-
if (Object.keys(unusedFiles).length > 0 || options.log === "all") {
18+
if ((Object.keys(unusedFiles).length > 0 && options.log !== "none") || options.log === "all") {
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") || options.log === "all") {
2627
logUnusedExportMap(unusedExportMap);
2728
}
2829
}
2930

31+
if (options.exportJSON) {
32+
let exportPath = "deadcode.json";
33+
if (typeof options.exportJSON === "string") {
34+
exportPath = options.exportJSON + "/" + exportPath;
35+
}
36+
try {
37+
fs.stat(exportPath, err => {
38+
if (err == null) {
39+
fs.unlinkSync(exportPath);
40+
return exportResultToJSON(exportPath, unusedFiles, unusedExportMap);
41+
}
42+
if (err.code === "ENOENT") {
43+
return exportResultToJSON(exportPath, unusedFiles, unusedExportMap);
44+
}
45+
});
46+
} catch (error) {
47+
console.error("export result to json error: ", error);
48+
}
49+
}
50+
3051
if (unusedFiles.length > 0 || unusedExportMap.length > 0) {
3152
if (options.failOnHint) {
3253
process.exit(2);
3354
}
3455
}
3556
}
3657

58+
function exportResultToJSON(exportPath, unusedFiles, unusedExports) {
59+
const data = {
60+
unusedFiles,
61+
unusedExports,
62+
};
63+
fs.mkdir(getDirName(exportPath), { recursive: true }, err => {
64+
if (err) throw err;
65+
fs.writeFile(exportPath, JSON.stringify(data, null, 2), err => {
66+
if (err) throw err;
67+
console.info(path.resolve(exportPath) + " is generated.");
68+
});
69+
});
70+
}
71+
3772
function getPattern({ context, patterns, exclude }) {
3873
return patterns
3974
.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)