Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize plugin #18

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/WebpackBuildStatsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@ export class WebpackBuildStatsPlugin {
}

apply(compiler: Compiler) {
compiler.hooks.done.tap('AgodaBuildStatsPlugin', async (stats: Stats) => {
const jsonStats: StatsCompilation = stats.toJson();

compiler.hooks.done.tapPromise('AgodaBuildStatsPlugin', async (stats: Stats) => {
let nbrOfCachedModules = 0, nbrOfRebuiltModules = 0;
// https://github.com/webpack/webpack/blob/f4092a60598a73447687fa6e6375bb4786bfcbe3/lib/stats/DefaultStatsFactoryPlugin.js#L1142
const compilation = stats.compilation;
for (const module of compilation.modules) {
if (!compilation.builtModules.has(module) && !compilation.codeGeneratedModules.has(module)) {
nbrOfCachedModules += 1;
} else {
nbrOfRebuiltModules += 1;
}
}
const buildStats: WebpackBuildData = {
...getCommonMetadata(jsonStats.time ?? -1, this.customIdentifier),
...getCommonMetadata(stats.endTime - stats.startTime ?? -1, this.customIdentifier),
type: 'webpack',
compilationHash: jsonStats.hash ?? null,
webpackVersion: jsonStats.version ?? null,
nbrOfCachedModules: jsonStats.modules?.filter((m) => m.cached).length ?? 0,
nbrOfRebuiltModules: jsonStats.modules?.filter((m) => m.built).length ?? 0,
compilationHash: stats.hash ?? null,
webpackVersion: compiler.webpack.version ?? null,
nbrOfCachedModules,
nbrOfRebuiltModules,
};

sendBuildData(buildStats);
await sendBuildData(buildStats);
});
}
}
25 changes: 21 additions & 4 deletions tests/WebpackBuildStatsPlugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ const mockedGetCommonMetadata = getCommonMetadata as jest.MockedFunction<
>;
const mockedSendBuildData = sendBuildData as jest.MockedFunction<typeof sendBuildData>;

const compilation = {
modules: new Set([1, 2, 3]),
builtModules: new Set([1]),
codeGeneratedModules: new Set([2]),
};
const mockedCompiler = {
webpack: {
version: '5.51.1',
},
compilation,
hooks: {
done: {
tap: jest.fn(),
tapPromise: jest.fn(),
},
},
};
Expand All @@ -27,7 +36,7 @@ describe('WebpackBuildStatsPlugin', () => {
webpackVersion: '5.51.1',
compilationHash: 'blahblahblacksheep',
nbrOfCachedModules: 1,
nbrOfRebuiltModules: 1,
nbrOfRebuiltModules: 2,
} as WebpackBuildData;

beforeEach(() => {
Expand All @@ -41,6 +50,10 @@ describe('WebpackBuildStatsPlugin', () => {

// mock stats
const mockedStats = {
compilation,
startTime: 1000,
endTime: 1123,
hash: 'blahblahblacksheep',
toJson: jest.fn().mockReturnValue({
time: 123,
hash: 'blahblahblacksheep',
Expand All @@ -55,7 +68,7 @@ describe('WebpackBuildStatsPlugin', () => {
const plugin = new WebpackBuildStatsPlugin('my custom identifier');
plugin.apply(mockedCompiler as unknown as Compiler);

const callback = mockedCompiler.hooks.done.tap.mock.calls[0][1];
const callback = mockedCompiler.hooks.done.tapPromise.mock.calls[0][1];
await callback(mockedStats as unknown as import('webpack').Stats);

expect(mockedGetCommonMetadata).toBeCalledWith(123, 'my custom identifier');
Expand All @@ -65,6 +78,10 @@ describe('WebpackBuildStatsPlugin', () => {
it('should use process.env.npm_lifecycle_event as default custom identifier', async () => {
// mock stats
const mockedStats = {
compilation,
startTime: 1000,
endTime: 1123,
hash: 'blahblahblacksheep',
toJson: jest.fn().mockReturnValue({
time: 123,
hash: 'blahblahblacksheep',
Expand All @@ -86,7 +103,7 @@ describe('WebpackBuildStatsPlugin', () => {
const plugin = new WebpackBuildStatsPlugin();
plugin.apply(mockedCompiler as unknown as Compiler);

const callback = mockedCompiler.hooks.done.tap.mock.calls[0][1];
const callback = mockedCompiler.hooks.done.tapPromise.mock.calls[0][1];
await callback(mockedStats as unknown as import('webpack').Stats);

expect(mockedGetCommonMetadata).toBeCalledWith(123, 'default_value');
Expand Down