🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →

time-analytics-webpack-plugin

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

time-analytics-webpack-plugin - npm Package Compare versions

Comparing version

to
0.1.12

@@ -48,2 +48,8 @@ import chalk, { Chalk } from 'chalk';

eventType: LoaderEventType;
/**
* Whether this loader is async loader or sync loader.
*
* async loader means this loader calls `this.async()`
*/
isAsync: boolean;
}

@@ -118,2 +124,6 @@

dangerTimeLimit: number;
/**
* TODO: should we remove this option? Feels like we should not collect the info at all. Do this by give loader options.
*/
ignoredLoaders: string[];
}

@@ -239,3 +249,3 @@

&& dataB[1].eventType === PluginEventType.end
, 'each tap should start once and end once');
, 'each tap execution should be collected info for start and end, once and only once.');
const tapTime = dataB[1].time - dataB[0].time;

@@ -260,2 +270,6 @@ currentPluginTotalTime += tapTime;

Object.entries(nameGrouppedLoader).forEach(([loaderName, dataA]) => {
if (option.ignoredLoaders.includes(loaderName)) {
messages.push(`Loader ${chalk.bold(loaderName)} is ignored.`);
return;
}
let currentLoaderTotalTime = 0;

@@ -267,3 +281,3 @@ const idGroupedPlugin = groupBy(prop('callId'), dataA);

&& dataB[1].eventType === LoaderEventType.end
, 'each tap should start once and end once');
, `each laoder execution should be collected info for start and end, once and only once. But for ${loaderName}, there is an error, why?`);
const tapTime = dataB[1].time - dataB[0].time;

@@ -270,0 +284,0 @@ currentLoaderTotalTime += tapTime;

@@ -7,5 +7,5 @@ import * as path from 'path';

/**
* Only `compilation` will have this unqiue key
* Only `compilation`/`compiler` will have this unqiue key
*/
// A symbol might be better, but it's fine and string is good to debug.
export const COMPILATION_WEAK_MAP_ID_KEY = '__webpack_compilation_weak_map_Id';
export const WEBPACK_WEAK_MAP_ID_KEY = '__webpack_weak_map_Id';

@@ -150,3 +150,3 @@ "use strict";

&& dataB[0].eventType === PluginEventType.start
&& dataB[1].eventType === PluginEventType.end, 'each tap should start once and end once');
&& dataB[1].eventType === PluginEventType.end, 'each tap execution should be collected info for start and end, once and only once.');
const tapTime = dataB[1].time - dataB[0].time;

@@ -168,2 +168,6 @@ currentPluginTotalTime += tapTime;

Object.entries(nameGrouppedLoader).forEach(([loaderName, dataA]) => {
if (option.ignoredLoaders.includes(loaderName)) {
messages.push(`Loader ${chalk_1.default.bold(loaderName)} is ignored.`);
return;
}
let currentLoaderTotalTime = 0;

@@ -174,3 +178,3 @@ const idGroupedPlugin = (0, ramda_1.groupBy)((0, ramda_1.prop)('callId'), dataA);

&& dataB[0].eventType === LoaderEventType.start
&& dataB[1].eventType === LoaderEventType.end, 'each tap should start once and end once');
&& dataB[1].eventType === LoaderEventType.end, `each laoder execution should be collected info for start and end, once and only once. But for ${loaderName}, there is an error, why?`);
const tapTime = dataB[1].time - dataB[0].time;

@@ -177,0 +181,0 @@ currentLoaderTotalTime += tapTime;

@@ -26,3 +26,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.COMPILATION_WEAK_MAP_ID_KEY = exports.PACKAGE_LOADER_PATH = exports.PACKAGE_NAME = void 0;
exports.WEBPACK_WEAK_MAP_ID_KEY = exports.PACKAGE_LOADER_PATH = exports.PACKAGE_NAME = void 0;
const path = __importStar(require("path"));

@@ -32,6 +32,6 @@ exports.PACKAGE_NAME = 'time-analytics-webpack-plugin';

/**
* Only `compilation` will have this unqiue key
* Only `compilation`/`compiler` will have this unqiue key
*/
// A symbol might be better, but it's fine and string is good to debug.
exports.COMPILATION_WEAK_MAP_ID_KEY = '__webpack_compilation_weak_map_Id';
exports.WEBPACK_WEAK_MAP_ID_KEY = '__webpack_weak_map_Id';
//# sourceMappingURL=const.js.map

@@ -116,2 +116,3 @@ "use strict";

time: (0, utils_1.now)(),
isAsync: !isSync,
});

@@ -133,2 +134,3 @@ // const asyncResult = arguments[1];

time: (0, utils_1.now)(),
isAsync: !isSync,
});

@@ -154,2 +156,3 @@ const ret = originLoader.apply(almostThis, arguments);

time: (0, utils_1.now)(),
isAsync: !isSync,
});

@@ -156,0 +159,0 @@ return ret;

@@ -72,6 +72,14 @@ "use strict";

const originHooks = target[property];
(0, utils_1.assert)(Object.isFrozen(originHooks), 'webpack frozens all `hooks` by defualt');
// Webpack 4 not freeze the hooks, but Webpack 5 freeze
(0, utils_1.assert)(originHooks.constructor.name === 'Object', '`Hooks` should just be plain object');
const unfrozenHooks = { ...originHooks };
return that._proxyForHooks(unfrozenHooks, [hooksProvider.constructor.name, property]);
let hookObject;
if (Object.isFrozen(originHooks)) {
hookObject = { ...originHooks };
}
else {
// TODO: remove this support
utils_1.ConsoleHelper.warn('You are using Webpack 4 and Time Analyzer Plugin together. However, this plugin is designed for Webpack 5.');
hookObject = originHooks;
}
return that._proxyForHooks(hookObject, [hooksProvider.constructor.name, property]);
}

@@ -78,0 +86,0 @@ return target[property];

@@ -14,6 +14,6 @@ "use strict";

// Hack the WeakMap, so that we could generate a unique ID for each to generate a unique
const compilationId = key[const_1.COMPILATION_WEAK_MAP_ID_KEY];
const compilationId = key[const_1.WEBPACK_WEAK_MAP_ID_KEY];
const isWebpackCompilation = !!compilationId;
if (isWebpackCompilation) {
(0, utils_1.assert)(compilationId instanceof WeakMapIdObject_1.WebpackCompilationWeakMapId);
(0, utils_1.assert)(compilationId instanceof WeakMapIdObject_1.WebpackWeakMapId);
finalKey = compilationId;

@@ -20,0 +20,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WebpackCompilationWeakMapId = void 0;
exports.WebpackWeakMapId = void 0;
const crypto_1 = require("crypto");
/**
* The object which will be used as the key of WeakMap for compilation.
* The object which will be used as the key of WeakMap for compilation/compiler.
*
* Need this, because WeakMap only accepts key as object.
*/
class WebpackCompilationWeakMapId {
class WebpackWeakMapId {
constructor() {

@@ -15,3 +15,3 @@ this.id = (0, crypto_1.randomUUID)();

}
exports.WebpackCompilationWeakMapId = WebpackCompilationWeakMapId;
exports.WebpackWeakMapId = WebpackWeakMapId;
//# sourceMappingURL=WeakMapIdObject.js.map

@@ -17,2 +17,11 @@ "use strict";

apply(compiler) {
// Maybe `environment` or `afterEnvironment` hook? Or wrap the parametere directly?
compiler.hooks.initialize.tap({
name: TimeAnalyticsPlugin.name,
// Make sure to be called fistly
stage: -100,
}, () => {
(0, utils_1.assert)(!compiler[const_1.WEBPACK_WEAK_MAP_ID_KEY], 'add unique id to compilation only once!');
compiler[const_1.WEBPACK_WEAK_MAP_ID_KEY] = new WeakMapIdObject_1.WebpackWeakMapId();
});
compiler.hooks.thisCompilation.tap({

@@ -23,4 +32,4 @@ name: TimeAnalyticsPlugin.name,

}, (compilation) => {
(0, utils_1.assert)(!compilation[const_1.COMPILATION_WEAK_MAP_ID_KEY], 'add unique id to compilation only once!');
compilation[const_1.COMPILATION_WEAK_MAP_ID_KEY] = new WeakMapIdObject_1.WebpackCompilationWeakMapId();
(0, utils_1.assert)(!compilation[const_1.WEBPACK_WEAK_MAP_ID_KEY], 'add unique id to compilation only once!');
compilation[const_1.WEBPACK_WEAK_MAP_ID_KEY] = new WeakMapIdObject_1.WebpackWeakMapId();
});

@@ -45,2 +54,3 @@ compiler.hooks.compile.tap(TimeAnalyticsPlugin.name, () => {

warnTimeLimit: this.option?.warnTimeLimit ?? 3000,
ignoredLoaders: this.option?.loader?.exclude ?? [],
});

@@ -59,2 +69,26 @@ });

}
get isLoaderEnabled() {
switch (typeof this.option?.enable) {
case 'boolean':
return this.option.enable;
case 'object':
return this.option.enable.loader;
case 'undefined':
return true;
default:
(0, utils_1.fail)('TS has a strange error here. We could not use assertNever, use fail instead.');
}
}
get isPluginEnabled() {
switch (typeof this.option?.enable) {
case 'boolean':
return this.option.enable;
case 'object':
return this.option.enable.plugin;
case 'undefined':
return true;
default:
(0, utils_1.fail)('TS has a strange error here. We could not use assertNever, use fail instead.');
}
}
}

@@ -64,3 +98,3 @@ exports.TimeAnalyticsPlugin = TimeAnalyticsPlugin;

const newConfig = { ...config };
if (newConfig.plugins) {
if (this.isPluginEnabled && newConfig.plugins) {
newConfig.plugins = newConfig.plugins.map((plugin) => {

@@ -75,3 +109,3 @@ const pluginName = plugin.constructor.name;

}
if (newConfig.optimization?.minimizer) {
if (this.isPluginEnabled && newConfig.optimization?.minimizer) {
newConfig.optimization.minimizer = newConfig.optimization.minimizer

@@ -86,3 +120,3 @@ .map((minimizer) => {

}
if (newConfig.module) {
if (this.isLoaderEnabled && newConfig.module) {
newConfig.module = injectModule(newConfig.module);

@@ -89,0 +123,0 @@ }

@@ -125,2 +125,3 @@ /* eslint-disable prefer-rest-params */

time: now(),
isAsync: !isSync,
});

@@ -143,2 +144,3 @@ // const asyncResult = arguments[1];

time: now(),
isAsync: !isSync,
});

@@ -166,2 +168,3 @@

time: now(),
isAsync: !isSync,
});

@@ -168,0 +171,0 @@ return ret;

{
"name": "time-analytics-webpack-plugin",
"version": "0.1.11",
"version": "0.1.12",
"description": "analytize the time of each part of webpack",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

@@ -93,6 +93,13 @@ /* eslint-disable @typescript-eslint/no-this-alias */ // use function, so that we could put logic firstly

const originHooks = target[property];
assert(Object.isFrozen(originHooks), 'webpack frozens all `hooks` by defualt');
// Webpack 4 not freeze the hooks, but Webpack 5 freeze
assert(originHooks.constructor.name === 'Object', '`Hooks` should just be plain object');
const unfrozenHooks = { ...originHooks };
return that._proxyForHooks(unfrozenHooks, [hooksProvider.constructor.name, property]);
let hookObject;
if (Object.isFrozen(originHooks)) {
hookObject = { ...originHooks };
} else {
// TODO: remove this support
ConsoleHelper.warn('You are using Webpack 4 and Time Analyzer Plugin together. However, this plugin is designed for Webpack 5.');
hookObject = originHooks;
}
return that._proxyForHooks(hookObject, [hooksProvider.constructor.name, property]);
}

@@ -99,0 +106,0 @@ return target[property];

@@ -1,4 +0,4 @@

import { COMPILATION_WEAK_MAP_ID_KEY } from '../const';
import { WEBPACK_WEAK_MAP_ID_KEY } from '../const';
import { assert } from '../utils';
import { WebpackCompilationWeakMapId } from './WeakMapIdObject';
import { WebpackWeakMapId } from './WeakMapIdObject';

@@ -15,6 +15,6 @@ assert(WeakMap, 'WeakMap must be existed.');

// Hack the WeakMap, so that we could generate a unique ID for each to generate a unique
const compilationId: any = key[COMPILATION_WEAK_MAP_ID_KEY];
const compilationId: any = key[WEBPACK_WEAK_MAP_ID_KEY];
const isWebpackCompilation = !!compilationId;
if (isWebpackCompilation) {
assert(compilationId instanceof WebpackCompilationWeakMapId);
assert(compilationId instanceof WebpackWeakMapId);
finalKey = compilationId;

@@ -21,0 +21,0 @@ }

import { randomUUID } from 'crypto';
/**
* The object which will be used as the key of WeakMap for compilation.
* The object which will be used as the key of WeakMap for compilation/compiler.
*
* Need this, because WeakMap only accepts key as object.
*/
export class WebpackCompilationWeakMapId {
export class WebpackWeakMapId {
private id = randomUUID();
}

@@ -5,6 +5,6 @@ import type { Compiler, Configuration, ModuleOptions, RuleSetRule } from 'webpack';

import { normalizeRules } from './loaderHelper';
import { assert, ConsoleHelper, fail, now } from './utils';
import { assert, assertNever, ConsoleHelper, fail, now } from './utils';
import './sideEffects/hackWeakMap';
import { COMPILATION_WEAK_MAP_ID_KEY } from './const';
import { WebpackCompilationWeakMapId } from './sideEffects/WeakMapIdObject';
import { WEBPACK_WEAK_MAP_ID_KEY } from './const';
import { WebpackWeakMapId } from './sideEffects/WeakMapIdObject';

@@ -24,5 +24,21 @@ export declare class WebpackPlugin {

*
* If true, output all loader and plugin infos.
*
* If object, loader and plugin could be turn off.
*
* Control loader and plugin with fine grained in `loader` and `plugin` options (not this option)
*
* @default true
*/
enable?: boolean;
enable?: boolean | {
/**
* @default true
*/
loader: boolean,
/**
* @default true
*/
plugin: boolean,
};
/**

@@ -88,2 +104,13 @@ * If provided, write the result to a file.

public apply(compiler: Compiler) {
// Maybe `environment` or `afterEnvironment` hook? Or wrap the parametere directly?
compiler.hooks.initialize.tap({
name: TimeAnalyticsPlugin.name,
// Make sure to be called fistly
stage: -100,
}, () => {
assert(!(compiler as any)[WEBPACK_WEAK_MAP_ID_KEY], 'add unique id to compilation only once!');
(compiler as any)[WEBPACK_WEAK_MAP_ID_KEY] = new WebpackWeakMapId();
});
compiler.hooks.thisCompilation.tap({

@@ -94,4 +121,4 @@ name: TimeAnalyticsPlugin.name,

}, (compilation) => {
assert(!(compilation as any)[COMPILATION_WEAK_MAP_ID_KEY], 'add unique id to compilation only once!');
(compilation as any)[COMPILATION_WEAK_MAP_ID_KEY] = new WebpackCompilationWeakMapId();
assert(!(compilation as any)[WEBPACK_WEAK_MAP_ID_KEY], 'add unique id to compilation only once!');
(compilation as any)[WEBPACK_WEAK_MAP_ID_KEY] = new WebpackWeakMapId();
});

@@ -120,2 +147,3 @@

warnTimeLimit: this.option?.warnTimeLimit ?? 3000,
ignoredLoaders: this.option?.loader?.exclude ?? [],
});

@@ -141,2 +169,28 @@ });

}
get isLoaderEnabled(): boolean {
switch (typeof this.option?.enable) {
case 'boolean':
return this.option.enable;
case 'object':
return this.option.enable.loader;
case 'undefined':
return true;
default:
fail('TS has a strange error here. We could not use assertNever, use fail instead.');
}
}
get isPluginEnabled(): boolean {
switch (typeof this.option?.enable) {
case 'boolean':
return this.option.enable;
case 'object':
return this.option.enable.plugin;
case 'undefined':
return true;
default:
fail('TS has a strange error here. We could not use assertNever, use fail instead.');
}
}
}

@@ -146,3 +200,3 @@

const newConfig = { ...config };
if (newConfig.plugins) {
if (this.isPluginEnabled && newConfig.plugins) {
newConfig.plugins = newConfig.plugins.map((plugin) => {

@@ -157,3 +211,3 @@ const pluginName = plugin.constructor.name;

}
if (newConfig.optimization?.minimizer) {
if (this.isPluginEnabled && newConfig.optimization?.minimizer) {
newConfig.optimization.minimizer = newConfig.optimization.minimizer

@@ -168,3 +222,3 @@ .map((minimizer) => {

}
if (newConfig.module) {
if (this.isLoaderEnabled && newConfig.module) {
newConfig.module = injectModule(newConfig.module);

@@ -171,0 +225,0 @@ }

@@ -37,2 +37,8 @@ export declare enum AnalyzeInfoKind {

eventType: LoaderEventType;
/**
* Whether this loader is async loader or sync loader.
*
* async loader means this loader calls `this.async()`
*/
isAsync: boolean;
}

@@ -100,2 +106,6 @@ export declare enum TapType {

dangerTimeLimit: number;
/**
* TODO: should we remove this option? Feels like we should not collect the info at all. Do this by give loader options.
*/
ignoredLoaders: string[];
}

@@ -102,0 +112,0 @@ declare class WebpackTimeAnalyzer {

export declare const PACKAGE_NAME = "time-analytics-webpack-plugin";
export declare const PACKAGE_LOADER_PATH: string;
/**
* Only `compilation` will have this unqiue key
* Only `compilation`/`compiler` will have this unqiue key
*/
export declare const COMPILATION_WEAK_MAP_ID_KEY = "__webpack_compilation_weak_map_Id";
export declare const WEBPACK_WEAK_MAP_ID_KEY = "__webpack_weak_map_Id";
/**
* The object which will be used as the key of WeakMap for compilation.
* The object which will be used as the key of WeakMap for compilation/compiler.
*
* Need this, because WeakMap only accepts key as object.
*/
export declare class WebpackCompilationWeakMapId {
export declare class WebpackWeakMapId {
private id;
}

@@ -14,5 +14,20 @@ import type { Compiler, Configuration } from 'webpack';

*
* If true, output all loader and plugin infos.
*
* If object, loader and plugin could be turn off.
*
* Control loader and plugin with fine grained in `loader` and `plugin` options (not this option)
*
* @default true
*/
enable?: boolean;
enable?: boolean | {
/**
* @default true
*/
loader: boolean;
/**
* @default true
*/
plugin: boolean;
};
/**

@@ -80,2 +95,4 @@ * If provided, write the result to a file.

static wrap(webpackConfigOrFactory: Configuration, options?: TimeAnalyticsPluginOptions): WebpackConfigFactory;
get isLoaderEnabled(): boolean;
get isPluginEnabled(): boolean;
}

@@ -82,0 +99,0 @@ /**

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet