Skip to content

Commit

Permalink
refactor: replace to defu
Browse files Browse the repository at this point in the history
  • Loading branch information
wattanx committed Aug 12, 2024
1 parent 89c4288 commit e9d70be
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 83 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 21 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 3 additions & 10 deletions src/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(file: string) {
Expand All @@ -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');
Expand Down
19 changes: 5 additions & 14 deletions src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {};
Expand All @@ -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);
Expand All @@ -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/'));
Expand All @@ -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',
})
);
Expand Down
11 changes: 10 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -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'
*/
Expand Down
43 changes: 9 additions & 34 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand All @@ -9,38 +10,12 @@ export const getOptions = async (
pathPrefix = process.cwd()
): Promise<NuxtBundleAnalysisOptions> => {
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',
});
};
7 changes: 6 additions & 1 deletion src/vite/report.ts
Original file line number Diff line number Diff line change
@@ -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];
}
Expand Down

0 comments on commit e9d70be

Please sign in to comment.