Skip to content

Commit

Permalink
Final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
karolyi committed Jul 30, 2024
1 parent 0ca588e commit 98dc662
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
66 changes: 40 additions & 26 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/** @typedef {import("webpack").Compiler} Compiler4 */
/** @typedef {Compiler4['hooks']['assetEmitted']} assetEmitted4 */
/** @typedef {import("webpack").Stats} Stats4 */
/** @typedef {import("webpack5").Stats} Stats5 */
/** @typedef {import("webpack").compilation.Compilation} Compilation4 */
/** @typedef {import("webpack").ChunkData} ChunkData4 */
/** @typedef {import("webpack5").Compiler} Compiler5 */
Expand All @@ -16,7 +17,6 @@
/** @typedef {import("../typings").Options} Options */
/** @typedef {Contents['assets']} ContentsAssets */
/** @typedef {ContentsAssets[keyof ContentsAssets]} ContentsAssetsValue */
/** @typedef {Compilation4 | Compilation5} Compilation4or5 */

const path = require('path')
const fs = require('fs')
Expand All @@ -30,7 +30,7 @@ const toPairs = require('lodash.topairs')
const stripAnsi = require('./utils/stripAnsi')

/**
* @param {Compilation4or5} compilation
* @param {Compilation4 | Compilation5} compilation
* @param {string} name
* @returns {string}
*/
Expand All @@ -56,9 +56,6 @@ function mergeObjects(obj1, obj2) {
// regenerating the object from the original key-value pairs.
return fromPairs(sortedPairs)
}
/**
* @property {Compilation4or5} _compilation
*/

class BundleTrackerPlugin {
/**
Expand All @@ -79,26 +76,40 @@ class BundleTrackerPlugin {
this._iter_output = { status: 'compile', assets: {}, chunks: {} }
}

/**
* @param {Compiler4 | Compiler5} compiler
* @returns {string}
*/
_getPublicPath(compiler) {
const publicPath = compiler.options.output?.publicPath
if (publicPath === undefined)
return ''
if (typeof publicPath === 'string')
return publicPath
// It is a function but no idea what to pass it
// See https://webpack.js.org/configuration/output/#outputpublicpath
return ''
}

/**
* Setup module options from compiler data
* @param {Compiler4} compiler
* @returns ComputedOpts
* @param {Compiler4 | Compiler5} compiler
* @returns {ComputedOpts}
*/
_getComputedOptions(compiler) {
const opts = this.options
const outputPath = compiler.options.output?.path || process.cwd()
const config = {
path: opts.path || get(compiler.options, 'output.path', process.cwd()),
path: opts.path || outputPath,
filename: opts.filename || 'webpack-stats.json',
publicPath:
opts.publicPath || get(compiler.options, 'output.publicPath', ''),
publicPath: opts.publicPath || this._getPublicPath(compiler),
logTime: opts.logTime || false,
relativePath: opts.relativePath || false,
indent: opts.indent || 2,
integrity: opts.integrity || false,
// https://www.w3.org/TR/SRI/#cryptographic-hash-functions
integrityHashes: opts.integrityHashes || ['sha256', 'sha384', 'sha512'],
outputChunkDir:
path.resolve(get(compiler.options, 'output.path', process.cwd())),
outputChunkDir: path.resolve(outputPath)
}
if (config.filename.includes('/'))
throw Error(
Expand All @@ -121,6 +132,7 @@ class BundleTrackerPlugin {
* the output from the latest compilation results, back to the
* `this.contents` variable.
* @param {ComputedOpts} computedOpts
* @returns {void}
*/
_writeOutput(computedOpts) {
assign(this.contents, this._iter_output, {
Expand Down Expand Up @@ -159,10 +171,9 @@ class BundleTrackerPlugin {

/**
* Handle compile hook
* @param {Compiler4} compiler
* @param {ComputedOpts} computedOpts
*/
_handleCompile(compiler, computedOpts) {
_handleCompile(computedOpts) {
this._iter_output = { status: 'compile', assets: {}, chunks: {} }
this._writeOutput(computedOpts)
}
Expand All @@ -172,9 +183,10 @@ class BundleTrackerPlugin {
* @param {ComputedOpts} computedOpts
* @param {string} compiledFile
* @param {AssetEmittedInfo5 | string} detailsOrContent
* @returns {void}
*/
_handleAssetEmitted(computedOpts, compiledFile, detailsOrContent) {
/** @type {Compilation4or5} */
/** @type {Compilation4 | Compilation5} */
let compilation
let content
let targetPath
Expand Down Expand Up @@ -222,24 +234,26 @@ class BundleTrackerPlugin {

/**
* Handle compile hook
* @param {Compiler4} compiler
* @param {ComputedOpts} computedOpts
* @param {Stats4} stats
* @param {Stats4 | Stats5} stats
* @returns {void}
*/
_handleDone(compiler, computedOpts, stats) {
_handleDone(computedOpts, stats) {
if (stats.hasErrors()) {
const findError = (
/** @type {Compilation4or5} */ compilation
/** @type {Compilation4 | Compilation5} */ compilation
) => {
if (compilation.errors.length > 0) {
return compilation.errors[0]
}
return compilation.children.find(child => findError(child))
}
const error = findError(stats.compilation)
const errorStr =
typeof error === 'string' ? error : error?.details || 'unknown-error'
this._iter_output = {
status: 'error',
error: get(error, 'name', 'unknown-error'),
error: errorStr,
message: stripAnsi(error['message']),
assets: {}, chunks: {}
}
Expand All @@ -262,23 +276,23 @@ class BundleTrackerPlugin {

/**
* Set the compilation object (for the webpack4 case)
* @param {Compilation4or5} compilation
* @param {Compilation4 | Compilation5} compilation
* @returns {void}
*/
_handleThisCompilation(compilation) {
this._compilation = compilation
}

/**
* Method called by webpack to apply plugin hook
* @param {Compiler4} compiler
* @param {Compiler4 | Compiler5} compiler
* @returns {void}
*/
apply(compiler) {
const computedOpts = this._getComputedOptions(compiler)
// this._setParamsFromCompiler(compiler)

// In order of hook calls:
compiler.hooks.compile
.tap(this.name, this._handleCompile.bind(this, compiler, computedOpts))
.tap(this.name, this._handleCompile.bind(this, computedOpts))
compiler.hooks.thisCompilation
.tap(this.name, this._handleThisCompilation.bind(this))
// Webpack 4 or 5, who knows at this point
Expand All @@ -287,7 +301,7 @@ class BundleTrackerPlugin {
assetEmitted
.tap(this.name, this._handleAssetEmitted.bind(this, computedOpts))
compiler.hooks.done
.tap(this.name, this._handleDone.bind(this, compiler, computedOpts))
.tap(this.name, this._handleDone.bind(this, computedOpts))
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-bundle-tracker",
"version": "3.1.0",
"version": "3.2.0",
"description": "Spits out some stats about webpack compilation process to a file",
"keywords": [
"bundle",
Expand Down

0 comments on commit 98dc662

Please # to comment.