-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): add webpack-logging plugin to Gatsby for debugging (#33214
- Loading branch information
Showing
6 changed files
with
118 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { ProgressPlugin } from "webpack" | ||
import resolveFrom from "resolve-from" | ||
import type reporter from "gatsby-cli/lib/reporter" | ||
import type { Compiler } from "webpack" | ||
|
||
type Reporter = typeof reporter | ||
|
||
export class WebpackLoggingPlugin { | ||
private PLUGIN_NAME = `WebpackLogging` | ||
private rootDir: string | ||
private reporter: Reporter | ||
private isVerbose = false | ||
|
||
constructor(rootDir: string, reporter: Reporter, isVerbose = false) { | ||
this.rootDir = rootDir | ||
this.reporter = reporter | ||
this.isVerbose = isVerbose | ||
} | ||
|
||
apply(compiler: Compiler): void { | ||
compiler.options.infrastructureLogging = { | ||
level: `verbose`, | ||
debug: /FileSystemInfo/, | ||
} | ||
compiler.options.profile = true | ||
|
||
new ProgressPlugin({ | ||
profile: true, | ||
}).apply(compiler) | ||
|
||
// if webpack bundle analyzer is installed lets use it | ||
const webpackBundleAnalyzerPath = resolveFrom.silent( | ||
this.rootDir, | ||
`webpack-bundle-analyzer` | ||
) | ||
if (webpackBundleAnalyzerPath) { | ||
compiler.hooks.beforeRun.tapPromise(this.PLUGIN_NAME, () => | ||
import(webpackBundleAnalyzerPath).then(({ BundleAnalyzerPlugin }) => { | ||
new BundleAnalyzerPlugin({ | ||
analyzerMode: `static`, | ||
openAnalyzer: false, | ||
title: compiler.name, | ||
reportFilename: `report.html`, | ||
}).apply(compiler) | ||
}) | ||
) | ||
} | ||
|
||
compiler.hooks.done.tap(this.PLUGIN_NAME, stats => { | ||
this.reporter.log( | ||
stats.toString({ | ||
colors: true, | ||
logging: this.isVerbose ? `verbose` : `log`, | ||
}) | ||
) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters