Skip to content

Commit

Permalink
feat: add support for webpack 3
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmaciejewski committed Apr 24, 2020
1 parent 34f97a6 commit caa9206
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 32 deletions.
37 changes: 21 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"start": "tsc -w"
},
"dependencies": {
"dashify": "^2.0.0",
"schema-utils": "^2.6.6"
},
"devDependencies": {
Expand All @@ -50,7 +51,7 @@
"husky": "^4.2.5",
"lint-staged": "^10.1.7",
"prettier": "^2.0.5",
"semantic-release": "^17.0.6",
"semantic-release": "^17.0.7",
"typescript": "^3.8.3",
"webpack": "^4.43.0"
}
Expand Down
27 changes: 12 additions & 15 deletions src/BuildTimeReporterWebpackPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { JSONSchema4 } from 'schema-utils/declarations/validate';
import validateOptions from 'schema-utils';
import { BuildTimeReport } from './BuildTimeReport';
import { BuildTimeReporterWebpackPluginOptions } from './types';

const PLUGIN_NAME = 'BuildTimeReporterWebpackPlugin';
import { getLogger, tapInto, tapPromiseInto } from './utils';

const schema: JSONSchema4 = {
type: 'object',
Expand All @@ -27,20 +26,18 @@ export class BuildTimeReporterWebpackPlugin {

private trackBuild(compilation: webpack.compilation.Compilation, report: BuildTimeReport): void {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
compilation.hooks.buildModule.tap(PLUGIN_NAME, ({ resource }: any) => {
tapInto(compilation, 'buildModule', ({ resource }: any) => {
report.setInitialResource(resource);
report.track('build');
});
compilation.hooks.succeedModule.tap(PLUGIN_NAME, () => {
tapInto(compilation, 'succeedModule', () => {
report.track('build');
});
}

private trackOptimize(compilation: webpack.compilation.Compilation, report: BuildTimeReport): void {
compilation.hooks.optimize.tap(PLUGIN_NAME, () => {
report.track('optimize');
});
[
'optimize',
'afterOptimizeDependencies',
'afterOptimizeModules',
'afterOptimizeChunks',
Expand All @@ -52,35 +49,35 @@ export class BuildTimeReporterWebpackPlugin {
'afterOptimizeAssets',
'afterOptimizeExtractedChunks',
].forEach((hook) => {
compilation.hooks[hook].tap(PLUGIN_NAME, () => {
tapInto(compilation, hook, () => {
report.track('optimize');
});
});
}

private trackEmit(): void {
this.compiler.hooks.emit.tap(PLUGIN_NAME, (compilation) => {
tapInto(this.compiler, 'emit', (compilation) => {
const report = this.reportsByHash.get(compilation.hash as string);
if (report) {
report.track('emit');
}
});
this.compiler.hooks.afterEmit.tap(PLUGIN_NAME, (compilation) => {
tapInto(this.compiler, 'afterEmit', (compilation) => {
const report = this.reportsByHash.get(compilation.hash as string);
if (report) {
report.track('emit');
report.addAssets(compilation.assets);
report.addChunks(compilation.chunks);
report.addModules(compilation.modules);
} else {
this.compiler.getInfrastructureLogger(PLUGIN_NAME).warn('trackEmit: could not find report', compilation.hash);
getLogger(this.compiler).warn('trackEmit: could not find report', compilation.hash);
}
});
}

private sendStatsWhenDone(): void {
this.compiler.hooks.done.tapPromise(PLUGIN_NAME, async (compilation) => {
const logger = this.compiler.getInfrastructureLogger(PLUGIN_NAME);
tapPromiseInto(this.compiler, 'done', async (compilation) => {
const logger = getLogger(this.compiler);
const report = this.reportsByHash.get(compilation.hash as string);
try {
if (report) {
Expand All @@ -97,14 +94,14 @@ export class BuildTimeReporterWebpackPlugin {
}

private setReportHash(compilation: webpack.compilation.Compilation, report: BuildTimeReport): void {
compilation.hooks.afterHash.tap(PLUGIN_NAME, () => {
tapInto(compilation, 'afterHash', () => {
report.setHash(compilation.hash as string);
this.reportsByHash.set(compilation.hash as string, report);
});
}

private createReportAndTrackCompilation(): void {
this.compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
tapInto(this.compiler, 'compilation', (compilation) => {
const report = new BuildTimeReport(this.compiler.context);
report.start();
this.setReportHash(compilation, report);
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PLUGIN_NAME = 'BuildTimeReporterWebpackPlugin';
41 changes: 41 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import webpack from 'webpack';
import dashify from 'dashify';
import { PLUGIN_NAME } from './constants';

export function tapInto(
context: webpack.Compiler | webpack.compilation.Compilation,
hook: string,
cb: (...args: any[]) => void,
): void {
if (context.hooks) {
context.hooks[hook].tap(PLUGIN_NAME, cb);
} else {
(context as webpack.Compiler).plugin(dashify(hook), (...args) => {
const callback = args.pop();
cb(...args, callback);
if (callback && callback instanceof Function) {
callback();
}
});
}
}

export function tapPromiseInto(
compiler: webpack.Compiler,
hook: string,
cb: (compilation: webpack.compilation.Compilation) => Promise<void>,
): void {
if (compiler.hooks) {
compiler.hooks[hook].tapPromise(PLUGIN_NAME, cb);
} else {
compiler.plugin(dashify(hook), (compilation, callback) => {
cb(compilation).then(callback).catch(callback);
});
}
}

export function getLogger(compiler: webpack.Compiler): webpack.Logger {
return compiler.getInfrastructureLogger
? compiler.getInfrastructureLogger(PLUGIN_NAME)
: ((console as unknown) as webpack.Logger);
}

0 comments on commit caa9206

Please sign in to comment.