Skip to content

Commit

Permalink
refactor: Extract utils
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Jun 10, 2024
1 parent c671041 commit ef546cd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
40 changes: 11 additions & 29 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import crypto from 'crypto';
import path from 'path';
import { OutputBundle, OutputChunk } from 'rollup';
import { OutputBundle } from 'rollup';

const HASH_LENGTH = 7;
import type { ExcludeFilepathConfig } from "./types";
import { getByteSize, getChunkId } from "./utils";

// https://github.com/relative-ci/bundle-stats/blob/master/packages/plugin-webpack-filter/src/index.ts
export type WebpackStatsFilteredAsset = {
Expand Down Expand Up @@ -42,38 +42,20 @@ export interface WebpackStatsFiltered {
modules?: Array<WebpackStatsFilteredRootModule>;
}

const getByteSize = (content: string | Buffer): number => {
if (typeof content === 'string') {
return Buffer.from(content).length;
}

return content?.length || 0;
};


const getHash = (text: string): string => {
const digest = crypto.createHash('sha256');
return digest.update(Buffer.from(text)).digest('hex').substr(0, HASH_LENGTH);
};

const getChunkId = (chunk: OutputChunk): string => {
let value = chunk.name;

// Use entry module relative path
if (chunk.moduleIds?.length > 0) {
const absoluteModulePath = chunk.moduleIds[chunk.moduleIds.length - 1];
value = path.relative(process.cwd(), absoluteModulePath);
}

return getHash([chunk, value].join('-'));
}

export type BundleTransformOptions = {
/**
* Extract module original size or rendered size
* default: false
*/
moduleOriginalSize?: boolean;
/**
* Exclude asset
*/
excludeAssets?: ExcludeFilepathConfig | Array<ExcludeFilepathConfig>;
/**
* Exclude module
*/
excludeModules?: ExcludeFilepathConfig | Array<ExcludeFilepathConfig>;
};

export const bundleToWebpackStats = (
Expand Down
37 changes: 37 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
import path from 'path';
import crypto from 'crypto';
import type { OutputChunk } from 'rollup';

import type { ExcludeFilepathConfig } from './types';

const HASH_LENGTH = 7;

/**
* Get content byte size
*/
export function getByteSize(content: string | Buffer): number {
if (typeof content === 'string') {
return Buffer.from(content).length;
}

return content?.length || 0;
}

/**
* Generate a 7 chars hash from a filepath
*/
export function getHash(filepath: string): string {
const digest = crypto.createHash('sha256');
return digest.update(Buffer.from(filepath)).digest('hex').substr(0, HASH_LENGTH);
}

export function getChunkId(chunk: OutputChunk): string {
let value = chunk.name;

// Use entry module relative path
if (chunk.moduleIds?.length > 0) {
const absoluteModulePath = chunk.moduleIds[chunk.moduleIds.length - 1];
value = path.relative(process.cwd(), absoluteModulePath);
}

return getHash([chunk, value].join('-'));
}

/**
* Check if filepath should be excluded based on a config
*/
Expand Down

0 comments on commit ef546cd

Please sign in to comment.