diff --git a/package.json b/package.json index 958b8b3..5bc0d56 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ ], "dependencies": { "@discoveryjs/json-ext": "^0.6.1", + "defu": "^6.1.4", "destr": "^2.0.3", "filesize": "^8.0.7", "globby": "^14.0.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e0fc9e3..983dc8e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: '@discoveryjs/json-ext': specifier: ^0.6.1 version: 0.6.1 + defu: + specifier: ^6.1.4 + version: 6.1.4 destr: specifier: ^2.0.3 version: 2.0.3 @@ -1947,9 +1950,6 @@ packages: resolution: {integrity: sha512-Ds09qNh8yw3khSjiJjiUInaGX9xlqZDY7JVryGxdxV7NPeuqQfplOpQ66yJFZut3jLa5zOwkXw1g9EI2uKh4Og==} engines: {node: '>=8'} - defu@6.1.2: - resolution: {integrity: sha512-+uO4+qr7msjNNWKYPHqN/3+Dx3NFkmIzayk2L1MyZQlvgZb/J1A0fo410dpKrN2SnqFjt8n4JL8fDJE0wIgjFQ==} - defu@6.1.4: resolution: {integrity: sha512-mEQCMmwJu317oSz8CwdIOdwf3xMif1ttiM8LTufzc3g6kR+9Pe236twL8j3IYT1F7GfRgGcW6MWxzZjLIkuHIg==} @@ -5072,7 +5072,7 @@ snapshots: '@nuxt/schema': 3.7.1(rollup@3.29.3) c12: 1.4.2 consola: 3.2.3 - defu: 6.1.2 + defu: 6.1.4 globby: 13.2.2 hash-sum: 2.0.0 ignore: 5.2.4 @@ -5096,7 +5096,7 @@ snapshots: '@nuxt/schema': 3.7.4(rollup@3.29.3) c12: 1.4.2 consola: 3.2.3 - defu: 6.1.2 + defu: 6.1.4 globby: 13.2.2 hash-sum: 2.0.0 ignore: 5.2.4 @@ -5118,7 +5118,7 @@ snapshots: '@nuxt/schema@3.7.1(rollup@3.29.3)': dependencies: '@nuxt/ui-templates': 1.3.1 - defu: 6.1.2 + defu: 6.1.4 hookable: 5.5.3 pathe: 1.1.2 pkg-types: 1.1.3 @@ -5135,7 +5135,7 @@ snapshots: dependencies: '@nuxt/ui-templates': 1.3.1 consola: 3.2.3 - defu: 6.1.2 + defu: 6.1.4 hookable: 5.5.3 pathe: 1.1.2 pkg-types: 1.1.3 @@ -5155,7 +5155,7 @@ snapshots: ci-info: 3.8.0 consola: 3.2.3 create-require: 1.1.1 - defu: 6.1.2 + defu: 6.1.4 destr: 2.0.1 dotenv: 16.3.1 fs-extra: 11.1.1 @@ -5186,7 +5186,7 @@ snapshots: clear: 0.1.0 consola: 3.2.3 cssnano: 6.0.1(postcss@8.4.30) - defu: 6.1.2 + defu: 6.1.4 esbuild: 0.19.3 escape-string-regexp: 5.0.0 estree-walker: 3.0.3 @@ -5241,7 +5241,7 @@ snapshots: css-loader: 6.8.1(webpack@5.88.2) css-minimizer-webpack-plugin: 5.0.1(webpack@5.88.2) cssnano: 6.0.1(postcss@8.4.30) - defu: 6.1.2 + defu: 6.1.4 esbuild-loader: 4.0.2(webpack@5.88.2) escape-string-regexp: 5.0.0 estree-walker: 3.0.3 @@ -6029,7 +6029,7 @@ snapshots: c12@1.4.2: dependencies: chokidar: 3.5.3 - defu: 6.1.2 + defu: 6.1.4 dotenv: 16.3.1 giget: 1.1.2 jiti: 1.20.0 @@ -6402,8 +6402,6 @@ snapshots: define-lazy-prop@2.0.0: {} - defu@6.1.2: {} - defu@6.1.4: {} delegates@1.0.0: {} @@ -6821,7 +6819,7 @@ snapshots: giget@1.1.2: dependencies: colorette: 2.0.20 - defu: 6.1.2 + defu: 6.1.4 https-proxy-agent: 5.0.1 mri: 1.2.0 node-fetch-native: 1.4.0 @@ -6896,7 +6894,7 @@ snapshots: h3@1.8.1: dependencies: cookie-es: 1.0.0 - defu: 6.1.2 + defu: 6.1.4 destr: 2.0.1 iron-webcrypto: 0.8.2 radix3: 1.1.0 @@ -7177,7 +7175,7 @@ snapshots: citty: 0.1.4 clipboardy: 3.0.0 consola: 3.2.3 - defu: 6.1.2 + defu: 6.1.4 get-port-please: 3.1.1 h3: 1.8.1 http-shutdown: 1.2.2 @@ -7428,7 +7426,7 @@ snapshots: citty: 0.1.4 consola: 3.2.3 cookie-es: 1.0.0 - defu: 6.1.2 + defu: 6.1.4 destr: 2.0.1 dot-prop: 8.0.2 esbuild: 0.19.3 @@ -7559,7 +7557,7 @@ snapshots: c12: 1.4.2 chokidar: 3.5.3 cookie-es: 1.0.0 - defu: 6.1.2 + defu: 6.1.4 destr: 2.0.1 devalue: 4.3.2 esbuild: 0.19.3 @@ -8245,7 +8243,7 @@ snapshots: rc9@2.1.1: dependencies: - defu: 6.1.2 + defu: 6.1.4 destr: 2.0.1 flat: 5.0.2 @@ -8416,7 +8414,7 @@ snapshots: serve-placeholder@2.0.1: dependencies: - defu: 6.1.2 + defu: 6.1.4 serve-static@1.15.0: dependencies: @@ -8664,7 +8662,7 @@ snapshots: chalk: 5.3.0 citty: 0.1.4 consola: 3.2.3 - defu: 6.1.2 + defu: 6.1.4 esbuild: 0.19.3 globby: 13.2.2 hookable: 5.5.3 @@ -8702,7 +8700,7 @@ snapshots: unenv@1.7.4: dependencies: consola: 3.2.3 - defu: 6.1.2 + defu: 6.1.4 mime: 3.0.0 node-fetch-native: 1.4.0 pathe: 1.1.2 @@ -8789,7 +8787,7 @@ snapshots: '@babel/core': 7.23.0 '@babel/standalone': 7.23.1 '@babel/types': 7.23.0 - defu: 6.1.2 + defu: 6.1.4 jiti: 1.20.0 mri: 1.2.0 scule: 1.0.0 diff --git a/src/compare.ts b/src/compare.ts index 662dff8..e85e8d3 100644 --- a/src/compare.ts +++ b/src/compare.ts @@ -5,11 +5,7 @@ import fs from 'fs'; import fsp from 'node:fs/promises'; import path from 'path'; import { BundleAnalysisType } from './types'; -import { - getBuildOutputDirectory, - getOptions, - getMinimumChangeThreshold, -} from './utils'; +import { getOptions } from './utils'; import { destr } from 'destr'; async function loadJson(file: string) { @@ -24,12 +20,9 @@ function createTableRow(path: string, size: number, diffStr: string) { async function compare() { const options = await getOptions(); - const minimumChangeThreshold = getMinimumChangeThreshold(options); + const { minimumChangeThreshold } = options; - const buildOutputDir = path.join( - process.cwd(), - getBuildOutputDirectory(options) - ); + const buildOutputDir = path.join(process.cwd(), options.buildOutputDirectory); const outdir = path.join(buildOutputDir, 'analyze'); const outfile = path.join(outdir, '__bundle_analysis_comment.txt'); diff --git a/src/report.ts b/src/report.ts index 1163c64..21b6a5f 100644 --- a/src/report.ts +++ b/src/report.ts @@ -5,13 +5,7 @@ import path from 'path'; import zlib from 'zlib'; import { parseChunked } from '@discoveryjs/json-ext'; import { StatsType } from './types'; -import { - getBuildOutputDirectory, - getBuilder, - getClientDir, - getOptions, - getStatsFilePath, -} from './utils'; +import { getOptions } from './utils'; import { getClientStats } from './vite/report'; const memoryCache: { [scriptPath: string]: number } = {}; @@ -30,10 +24,7 @@ function getScriptSize(scriptPath: string) { async function generateAnalysisJson() { const options = await getOptions(); - const buildOutputDir = path.join( - process.cwd(), - getBuildOutputDirectory(options) - ); + const buildOutputDir = path.join(process.cwd(), options.buildOutputDirectory); try { fs.accessSync(buildOutputDir, fs.constants.R_OK); @@ -44,7 +35,7 @@ async function generateAnalysisJson() { process.exit(1); } - if (getBuilder(options) === 'vite') { + if (options.builder === 'vite') { const rawData = JSON.stringify(await getClientStats()); try { fs.mkdirSync(path.join(buildOutputDir, 'analyze/')); @@ -56,10 +47,10 @@ async function generateAnalysisJson() { return; } - const clientDir = getClientDir(options); + const { clientDir } = options; const statsFile: StatsType = await parseChunked( - fs.createReadStream(path.join(process.cwd(), getStatsFilePath(options)), { + fs.createReadStream(path.join(process.cwd(), options.statsFile), { encoding: 'utf-8', }) ); diff --git a/src/types.ts b/src/types.ts index c79420c..d53bcec 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,12 +1,21 @@ export type NuxtBundleAnalysisOptions = { + /** + * @default '.nuxt' + */ buildOutputDirectory: string; clientDir: string; + /** + * @default '.nuxt/stats/client.json' + */ statsFile: string; + /** + * @default 0 + */ minimumChangeThreshold: number; /** * @default 'webpack' */ - builder?: 'webpack' | 'vite'; + builder: 'webpack' | 'vite'; /** * @default '.output' */ diff --git a/src/utils.ts b/src/utils.ts index 8512d99..6057151 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ import path from 'path'; import { NuxtBundleAnalysisOptions } from './types'; import { readPackageJSON } from 'pkg-types'; +import { defu } from 'defu'; /** * Reads options from `package.json` @@ -9,38 +10,12 @@ export const getOptions = async ( pathPrefix = process.cwd() ): Promise => { const json = await readPackageJSON(path.join(pathPrefix, 'package.json')); - return json.nuxtBundleAnalysis; -}; - -/** - * Gets the output build directory, defaults to `.nuxt` - */ -export const getBuildOutputDirectory = (options: NuxtBundleAnalysisOptions) => { - return options.buildOutputDirectory || '.nuxt'; -}; - -/** - * Gets the client directory, defaults to `dist/client` - */ -export const getClientDir = (options: NuxtBundleAnalysisOptions) => { - return options.clientDir || 'dist/client'; -}; - -/** - * Gets the stats file path. - */ -export const getStatsFilePath = (options: NuxtBundleAnalysisOptions) => { - return options.statsFile || '.nuxt/stats/client.json'; -}; - -export const getMinimumChangeThreshold = ( - options: NuxtBundleAnalysisOptions -): number => { - return options.minimumChangeThreshold || 0; -}; - -export const getBuilder = (options: NuxtBundleAnalysisOptions) => { - return ( - options.builder || process.env.NUXT_BUNDLE_ANALYSIS_BUILDER || 'webpack' - ); + return defu(json.nuxtBundleAnalysis, { + minimumChangeThreshold: 0, + buildOutputDirectory: '.nuxt', + clientDir: 'dist/client', + statsFile: '.nuxt/stats/client.json', + builder: process.env.NUXT_BUNDLE_ANALYSIS_BUILDER || 'webpack', + outputDirectory: '.output', + }); }; diff --git a/src/vite/report.ts b/src/vite/report.ts index 7503f37..d56041f 100644 --- a/src/vite/report.ts +++ b/src/vite/report.ts @@ -1,10 +1,15 @@ import fsp from 'node:fs/promises'; import { globby } from 'globby'; import { join } from 'pathe'; +import { getOptions } from '../utils'; export async function getClientStats() { const rootDir = process.cwd(); - const stats = await analyzeSizes('**/*.js', join(rootDir, '.output/public')); + const options = await getOptions(); + const stats = await analyzeSizes( + '**/*.js', + join(rootDir, options.outputDirectory, 'public') + ); return [stats]; }