Skip to content

Commit

Permalink
Merge pull request #19 from agoda-com/revert-18-master
Browse files Browse the repository at this point in the history
Revert "Optimize plugin"
  • Loading branch information
Vedmax authored Nov 27, 2024
2 parents dbe9e35 + 90c71dd commit 85c7b2b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 38 deletions.
26 changes: 9 additions & 17 deletions src/WebpackBuildStatsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,19 @@ export class WebpackBuildStatsPlugin {
}

apply(compiler: Compiler) {
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;
}
}
compiler.hooks.done.tap('AgodaBuildStatsPlugin', async (stats: Stats) => {
const jsonStats: StatsCompilation = stats.toJson();

const buildStats: WebpackBuildData = {
...getCommonMetadata(stats.endTime - stats.startTime ?? -1, this.customIdentifier),
...getCommonMetadata(jsonStats.time ?? -1, this.customIdentifier),
type: 'webpack',
compilationHash: stats.hash ?? null,
webpackVersion: compiler.webpack.version ?? null,
nbrOfCachedModules,
nbrOfRebuiltModules,
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,
};

await sendBuildData(buildStats);
sendBuildData(buildStats);
});
}
}
25 changes: 4 additions & 21 deletions tests/WebpackBuildStatsPlugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,10 @@ 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: {
tapPromise: jest.fn(),
tap: jest.fn(),
},
},
};
Expand All @@ -36,7 +27,7 @@ describe('WebpackBuildStatsPlugin', () => {
webpackVersion: '5.51.1',
compilationHash: 'blahblahblacksheep',
nbrOfCachedModules: 1,
nbrOfRebuiltModules: 2,
nbrOfRebuiltModules: 1,
} as WebpackBuildData;

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

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

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

expect(mockedGetCommonMetadata).toBeCalledWith(123, 'my custom identifier');
Expand All @@ -78,10 +65,6 @@ 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 @@ -103,7 +86,7 @@ describe('WebpackBuildStatsPlugin', () => {
const plugin = new WebpackBuildStatsPlugin();
plugin.apply(mockedCompiler as unknown as Compiler);

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

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

0 comments on commit 85c7b2b

Please sign in to comment.