Skip to content

Commit 8ecaa9c

Browse files
committed
feat: support exportJSON with a specific path
1 parent 7071418 commit 8ecaa9c

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,20 @@ Whether to run unused export detection or not.
145145
146146
#### options.exportJSON (default: `false`)
147147
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.
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+
```
149162
150163
[npm]: https://img.shields.io/npm/v/webpack-deadcode-plugin.svg
151164
[npm-url]: https://npmjs.com/package/webpack-deadcode-plugin

src/detect.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require("path");
22
const chalk = require("chalk");
33
const fs = require("fs");
44
const fg = require("fast-glob");
5+
const getDirName = path.dirname;
56

67
function detectDeadCode(compilation, options) {
78
const isWebpack5 = compilation.chunkGraph ? true : false;
@@ -14,22 +15,24 @@ function detectDeadCode(compilation, options) {
1415

1516
if (options.detectUnusedFiles) {
1617
unusedFiles = includedFiles.filter(file => !compiledFiles[file]);
17-
18-
if (Object.keys(unusedFiles).length > 0 || options.log !== "none") {
18+
if ((Object.keys(unusedFiles).length > 0 && options.log !== "none") || options.log === "all") {
1919
logUnusedFiles(unusedFiles);
2020
}
2121
}
2222

2323
if (options.detectUnusedExport) {
2424
unusedExportMap = getUsedExportMap(convertFilesToDict(includedFiles), compilation, isWebpack5);
2525

26-
if (Object.keys(unusedExportMap).length > 0 || options.log !== "none") {
26+
if ((Object.keys(unusedExportMap).length > 0 && options.log !== "none") || options.log === "all") {
2727
logUnusedExportMap(unusedExportMap);
2828
}
2929
}
3030

3131
if (options.exportJSON) {
3232
let exportPath = "deadcode.json";
33+
if (typeof options.exportJSON === "string") {
34+
exportPath = options.exportJSON + "/" + exportPath;
35+
}
3336
try {
3437
fs.stat(exportPath, err => {
3538
if (err == null) {
@@ -57,11 +60,12 @@ function exportResultToJSON(exportPath, unusedFiles, unusedExports) {
5760
unusedFiles,
5861
unusedExports,
5962
};
60-
fs.writeFile(exportPath, JSON.stringify(data, null, 2), err => {
61-
if (err) {
62-
throw err;
63-
}
64-
console.log(exportPath + " is generated.");
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+
});
6569
});
6670
}
6771

0 commit comments

Comments
 (0)