Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
fix(build-stats): esbuild and webpack stats under build-stats dir (#595)
Browse files Browse the repository at this point in the history
* fix(build-stats): esbuild and webpack stats under build-stats dir

* chore(one-app-bundler/build-stats): micro refactor build stats path
  • Loading branch information
JAdshead authored Jan 5, 2024
1 parent 8c3f402 commit 5b9701b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 8 deletions.
10 changes: 8 additions & 2 deletions packages/one-app-bundler/bin/postProcessOneAppBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ module.exports = async function postProcessBuild() {
};
const { hash } = await hashElement(tmpPath, options);
const buildVersion = `${version}-${hash.slice(0, 8)}`;
const modernBrowserChunkAssets = require(path.resolve(pkgPath, '../.webpack-stats.browser.json')).assetsByChunkName;
const legacyBrowserChunkAssets = require(path.resolve(pkgPath, '../.webpack-stats.legacyBrowser.json')).assetsByChunkName;

const buildStatsPath = path.resolve(pkgPath, '../.build-stats');

if (!fs.existsSync(buildStatsPath)) {
fs.mkdirSync(buildStatsPath);
}
const modernBrowserChunkAssets = require(path.resolve(buildStatsPath, '.webpack-stats.browser.json')).assetsByChunkName;
const legacyBrowserChunkAssets = require(path.resolve(buildStatsPath, '.webpack-stats.legacyBrowser.json')).assetsByChunkName;

fs.renameSync(tmpPath, path.resolve(tmpPath, `../${buildVersion}`));
const metaFilePath = path.resolve(pkgPath, '../.build-meta.json');
Expand Down
7 changes: 6 additions & 1 deletion packages/one-app-bundler/bin/webpackCallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ module.exports = function getWebpackCallback(label, isModuleBuild) {
});
}

fs.writeFileSync(path.join(process.cwd(), `.webpack-stats.${label}.json`), JSON.stringify(jsonStats));
const buildStatsPath = path.join(process.cwd(), '.build-stats');
if (!fs.existsSync(buildStatsPath)) {
fs.mkdirSync(buildStatsPath);
}

fs.writeFileSync(path.join(buildStatsPath, `.webpack-stats.${label}.json`), JSON.stringify(jsonStats));

if (isModuleBuild) {
generateIntegrityManifest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ jest.spyOn(console, 'log');
jest.mock('node:fs', () => ({
promises: {
writeFile: jest.fn(),
mkdir: jest.fn(),
},
existsSync: jest.fn(() => true),
}));

describe('Esbuild plugin timeBuild', () => {
Expand Down Expand Up @@ -89,8 +91,35 @@ describe('Esbuild plugin timeBuild', () => {
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith('mockBundleName bundle built in 3ms');
expect(fs.promises.writeFile).toHaveBeenCalledTimes(1);
expect(fs.promises.writeFile).toHaveBeenCalledWith('.esbuild-stats.mockBundleName.json', '{"file.js":{},"durationMs":3}');
expect(fs.promises.writeFile).toHaveBeenCalledWith('./.build-stats/.esbuild-stats.mockBundleName.json', '{"file.js":{},"durationMs":3}');
});

it('creates .build-stats directory if it does not exist', async () => {
fs.existsSync.mockReturnValue(false);

const plugin = timeBuild({ bundleName: 'mockBundleName', watch: false });
const hooks = runSetupAndGetLifeHooks(plugin);
const onStart = hooks.onStart[0];
const onEnd = hooks.onEnd[0];

process.hrtime.bigint.mockImplementation(() => BigInt(1000000)); // 1ms in nanoseconds
onStart(); // call onStart to load in the start time
jest.clearAllMocks();

process.hrtime.bigint.mockImplementation(() => BigInt(4000000)); // 4ms in nanoseconds

await onEnd({
metafile: {
outputs: {
'file.js': {},
},
},
});

expect(fs.promises.mkdir).toHaveBeenCalledTimes(1);
expect(fs.promises.mkdir).toHaveBeenCalledWith('.build-stats');
});

it('should sanatize bundlename replacing `/` with `-`', async () => {
const plugin = timeBuild({ bundleName: '@scoped/mockBundleName', watch: false });
const hooks = runSetupAndGetLifeHooks(plugin);
Expand All @@ -111,7 +140,7 @@ describe('Esbuild plugin timeBuild', () => {
},
});

expect(fs.promises.writeFile).toHaveBeenCalledWith('[email protected]', '{"file.js":{},"durationMs":3}');
expect(fs.promises.writeFile).toHaveBeenCalledWith('./.build-stats/.[email protected]', '{"file.js":{},"durationMs":3}');
});
it('should not log in watch mode', async () => {
const plugin = timeBuild({ bundleName: 'mockBundleName', watch: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('analyze-bundles', () => {
jest.clearAllMocks();
});

it('reports the analysis comparting webpack vs esbuild', async () => {
it('reports the analysis comparing webpack vs esbuild', async () => {
fs.promises.readFile.mockImplementation((absolutePath) => {
const fileName = absolutePath.split(path.sep).reverse()[0];

Expand Down
5 changes: 4 additions & 1 deletion packages/one-app-dev-bundler/esbuild/plugins/time-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ const timeBuild = ({ bundleName, watch }) => ({
if (!watch) { // watch has its own logging
console.log(`${bundleName} bundle built in ${result.durationMs}ms`);
const sanitizedName = bundleName.replace('/', '-');
await fs.promises.writeFile(`.esbuild-stats.${sanitizedName}.json`, JSON.stringify({
if (!fs.existsSync('.build-stats')) {
await fs.promises.mkdir('.build-stats');
}
await fs.promises.writeFile(`./.build-stats/.esbuild-stats.${sanitizedName}.json`, JSON.stringify({
...result.metafile.outputs,
durationMs: result.durationMs,
}));
Expand Down
2 changes: 1 addition & 1 deletion packages/one-app-dev-bundler/utils/analyze-bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const BUNDLERS = ['webpack', 'esbuild'];

const readStatsFile = async (fileName) => {
try {
const content = await fs.promises.readFile(path.resolve(process.cwd(), fileName), 'utf-8');
const content = await fs.promises.readFile(path.resolve(process.cwd(), '.build-stats', fileName), 'utf-8');

return content;
} catch (error) {
Expand Down

0 comments on commit 5b9701b

Please sign in to comment.