@@ -2,6 +2,7 @@ const path = require("path");
22const chalk = require ( "chalk" ) ;
33const fs = require ( "fs" ) ;
44const fg = require ( "fast-glob" ) ;
5+ const getDirName = path . dirname ;
56
67function 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