Skip to content

Commit

Permalink
Optimize webpack plugin (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidparkagoda authored Feb 24, 2025
1 parent d9716ee commit cd97658
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const createMockCompiler = () => {
tap: vi.fn(),
},
done: {
tap: vi.fn(),
tapPromise: vi.fn(),
},
compilation: {
tap: vi.fn(),
Expand Down Expand Up @@ -63,7 +63,7 @@ describe('WebpackBuildStatsPlugin', () => {
'WebpackBuildStatsPlugin',
expect.any(Function),
);
expect(mockedCompiler.hooks.done.tap).toHaveBeenCalledWith(
expect(mockedCompiler.hooks.done.tapPromise).toHaveBeenCalledWith(
'WebpackBuildStatsPlugin',
expect.any(Function),
);
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('WebpackBuildStatsPlugin', () => {

// Act
// Retrieve the callback the plugin registered with the "done" hook
const doneHookCallback = mockedCompiler.hooks.done.tap.mock.calls[0][1];
const doneHookCallback = mockedCompiler.hooks.done.tapPromise.mock.calls[0][1];
// Simulate a completed build
await doneHookCallback(mockedStats);

Expand Down Expand Up @@ -141,7 +141,7 @@ describe('WebpackBuildStatsPlugin', () => {
mockedSendBuildData.mockResolvedValue(undefined);

// Act
const doneHookCallback = mockedCompiler.hooks.done.tap.mock.calls[0][1];
const doneHookCallback = mockedCompiler.hooks.done.tapPromise.mock.calls[0][1];
await doneHookCallback(mockedStats);

// Assert
Expand Down
10 changes: 8 additions & 2 deletions packages/webpack-plugin/src/lib/webpack-build-stats-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,17 @@ export class WebpackBuildStatsPlugin {
/**
* 3) done => record "compileDone" and then send build stats with devFeedback
*/
compiler.hooks.done.tap('WebpackBuildStatsPlugin', async (stats: Stats) => {
compiler.hooks.done.tapPromise('WebpackBuildStatsPlugin', async (stats: Stats) => {
this.recordEvent({ type: 'compileDone' });

// Original build-stats logic
const jsonStats: StatsCompilation = stats.toJson();
const jsonStats: StatsCompilation = stats.toJson({
preset: 'none',
timings: true,
hash: true,
version: true,
modules: true,
});

const buildStats: WebpackBuildData = {
...getCommonMetadata(jsonStats.time ?? -1, this.customIdentifier),
Expand Down

0 comments on commit cd97658

Please sign in to comment.