From 98dc6626625ddc004b468c333a5795e6718704ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20K=C3=A1rolyi?= Date: Tue, 30 Jul 2024 22:51:37 +0200 Subject: [PATCH] Final touches --- lib/index.js | 66 +++++++++++++++++++++++++++++++--------------------- package.json | 2 +- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/lib/index.js b/lib/index.js index 87e3174..ea10bca 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 */ @@ -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') @@ -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} */ @@ -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 { /** @@ -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( @@ -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, { @@ -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) } @@ -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 @@ -222,14 +234,14 @@ 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] @@ -237,9 +249,11 @@ class BundleTrackerPlugin { 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: {} } @@ -262,7 +276,8 @@ class BundleTrackerPlugin { /** * Set the compilation object (for the webpack4 case) - * @param {Compilation4or5} compilation + * @param {Compilation4 | Compilation5} compilation + * @returns {void} */ _handleThisCompilation(compilation) { this._compilation = compilation @@ -270,15 +285,14 @@ class BundleTrackerPlugin { /** * 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 @@ -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)) } } diff --git a/package.json b/package.json index 8624b78..043fba1 100644 --- a/package.json +++ b/package.json @@ -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",