Skip to content
Open
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
5 changes: 5 additions & 0 deletions .idea/.gitignore

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

6 changes: 6 additions & 0 deletions .idea/dictionaries

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

5 changes: 5 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

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

9 changes: 9 additions & 0 deletions .idea/markdown.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

12 changes: 12 additions & 0 deletions .idea/speed-measure-webpack-plugin.iml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ yarn add -D speed-measure-webpack-plugin

## Requirements

SMP requires at least **Node v6**. But otherwise, accepts **all webpack** versions (1, 2, 3, and 4).
SMP requires at least **Node v6**. But otherwise, accepts **all webpack** versions (1, 2, 3, 4 and 5).

## Usage

Expand All @@ -53,13 +53,21 @@ const SpeedMeasurePlugin = require("speed-measure-webpack-plugin");

const smp = new SpeedMeasurePlugin();

const webpackConfig = smp.wrap({
const webpackConfig = smp.wrap(
{
plugins: [new MyPlugin(), new MyOtherPlugin()],
});
},
{
pluginsToExclude: ['MiniCSSExtractPlugin', 'AngularWebpackPlugin']
}
);
```

and you're done! SMP will now be printing timing output to the console by default.

Note: pluginsToExclude parameter is optional. It allows you to use SMP with plugins that are not compatible with it.
For example, MiniCSSExtractPlugin and AngularWebpackPlugin.

Check out the [examples folder](/examples) for some more examples.

## Options
Expand Down
32 changes: 17 additions & 15 deletions WrappedPlugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ const genWrappedFunc = ({
// to complete compilation. If this gets invoked and not the subsequent
// call, then our data will be inaccurate, sadly
addEndEvent();
const normalArgMap = a => wrap(a, pluginName, smp);
const normalArgMap = (a) => wrap(a, pluginName, smp);
let ret;
if (endType === "wrapDone")
ret = func.apply(
context,
args.map(a => wrap(a, pluginName, smp, addEndEvent))
args.map((a) => wrap(a, pluginName, smp, addEndEvent))
);
else if (endType === "async") {
const argsButLast = args.slice(0, args.length - 1);
Expand All @@ -45,7 +45,7 @@ const genWrappedFunc = ({
})
);
} else if (endType === "promise")
ret = func.apply(context, args.map(normalArgMap)).then(promiseArg => {
ret = func.apply(context, args.map(normalArgMap)).then((promiseArg) => {
addEndEvent();
return promiseArg;
});
Expand All @@ -56,7 +56,7 @@ const genWrappedFunc = ({
};

const genPluginMethod = (orig, pluginName, smp, type) =>
function(method, func) {
function (method, func) {
const timeEventName = pluginName + "/" + type + "/" + method;
const wrappedFunc = genWrappedFunc({
func,
Expand All @@ -70,7 +70,7 @@ const genPluginMethod = (orig, pluginName, smp, type) =>
};

const wrapTap = (tap, pluginName, smp, type, method) =>
function(id, func) {
function (id, func) {
const timeEventName = pluginName + "/" + type + "/" + method;
const wrappedFunc = genWrappedFunc({
func,
Expand All @@ -83,7 +83,7 @@ const wrapTap = (tap, pluginName, smp, type, method) =>
};

const wrapTapAsync = (tapAsync, pluginName, smp, type, method) =>
function(id, func) {
function (id, func) {
const timeEventName = pluginName + "/" + type + "/" + method;
const wrappedFunc = genWrappedFunc({
func,
Expand All @@ -97,7 +97,7 @@ const wrapTapAsync = (tapAsync, pluginName, smp, type, method) =>
};

const wrapTapPromise = (tapPromise, pluginName, smp, type, method) =>
function(id, func) {
function (id, func) {
const timeEventName = pluginName + "/" + type + "/" + method;
const wrappedFunc = genWrappedFunc({
func,
Expand All @@ -115,12 +115,12 @@ const wrapHooks = (orig, pluginName, smp, type) => {
const hooks = orig.hooks;
if (!hooks) return hooks;
const prevWrapped = wrappedHooks.find(
w =>
(w) =>
w.pluginName === pluginName && (w.orig === hooks || w.wrapped === hooks)
);
if (prevWrapped) return prevWrapped.wrapped;

const genProxy = method => {
const genProxy = (method) => {
const proxy = new Proxy(hooks[method], {
get: (target, property) => {
const raw = Reflect.get(target, property);
Expand Down Expand Up @@ -149,7 +149,8 @@ const wrapHooks = (orig, pluginName, smp, type) => {
};

const wrapped = Object.keys(hooks).reduce((acc, method) => {
acc[method] = genProxy(method);
if (hooks[method])
acc[method] = genProxy(method);
return acc;
}, {});

Expand All @@ -170,7 +171,8 @@ const construcNamesToWrap = [
const wrappedObjs = [];
const findWrappedObj = (orig, pluginName) => {
const prevWrapped = wrappedObjs.find(
w => w.pluginName === pluginName && (w.orig === orig || w.wrapped === orig)
(w) =>
w.pluginName === pluginName && (w.orig === orig || w.wrapped === orig)
);
if (prevWrapped) return prevWrapped.wrapped;
};
Expand All @@ -179,15 +181,15 @@ const wrap = (orig, pluginName, smp, addEndEvent) => {
const prevWrapped = findWrappedObj(orig, pluginName);
if (prevWrapped) return prevWrapped;

const getOrigConstrucName = target =>
const getOrigConstrucName = (target) =>
target && target.constructor && target.constructor.name;
const getShouldWrap = target => {
const getShouldWrap = (target) => {
const origConstrucName = getOrigConstrucName(target);
return construcNamesToWrap.includes(origConstrucName);
};
const shouldWrap = getShouldWrap(orig);
const shouldSoftWrap = Object.keys(orig)
.map(k => orig[k])
.map((k) => orig[k])
.some(getShouldWrap);

let wrappedReturn;
Expand All @@ -196,7 +198,7 @@ const wrap = (orig, pluginName, smp, addEndEvent) => {
const vanillaFunc = orig.name === "next";
wrappedReturn =
vanillaFunc && addEndEvent
? function() {
? function () {
// do this before calling the callback, since the callback can start
// the next plugin step
addEndEvent();
Expand Down
30 changes: 28 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const path = require("path");
const fs = require("fs");
const chalk = require("chalk");
const Compilation = require("webpack/lib/Compilation");
const { WrappedPlugin, clear } = require("./WrappedPlugin");
const {
getModuleName,
Expand Down Expand Up @@ -35,13 +36,22 @@ module.exports = class SpeedMeasurePlugin {
);
}

wrap(config) {
wrap(config, { pluginsToExclude }) {
pluginsToExclude = pluginsToExclude ?? [];
if (this.options.disable) return config;
if (Array.isArray(config)) return config.map(this.wrap);
if (typeof config === "function")
return (...args) => this.wrap(config(...args));

pluginsToExclude = pluginsToExclude.map((pluginName) =>
pluginName.toLowerCase()
);

config.plugins = (config.plugins || []).map((plugin) => {
if (pluginsToExclude.includes(plugin.constructor.name.toLowerCase())) {
return plugin;
}

const pluginName =
Object.keys(this.options.pluginNames || {}).find(
(pluginName) => plugin === this.options.pluginNames[pluginName]
Expand All @@ -68,6 +78,7 @@ module.exports = class SpeedMeasurePlugin {
this.smpPluginAdded = true;
}

// config.plugins = config.plugins.filter((plugin)=>plugin);
return config;
}

Expand Down Expand Up @@ -253,9 +264,24 @@ module.exports = class SpeedMeasurePlugin {
});

tap(compiler, "compilation", (compilation) => {
tap(compilation, "normal-module-loader", (loaderContext) => {
const normalModuleLoader = require("webpack/lib/NormalModule").getCompilationHooks(
new Compilation(compilation)
).loader;
normalModuleLoader.tap("new-normal-module-loader", (loaderContext) => {
loaderContext[NS] = this.provideLoaderTiming;
});
// console.info(normalModuleHooks);
// normalModuleHooks.beforeLoaders.tap("normal-module-loader", (data, callback) => {
// const loaderHook = normalModuleHooks.loader;
// loaderHook.tap("normal-module-loader", (loaderContext, module) => {
// loaderContext[NS] = this.provideLoaderTiming;
// });
// callback(null, data);
// });

// tap(compilation, "normal-module-loader", (loaderContext) => {
// loaderContext[NS] = this.provideLoaderTiming;
// });

tap(compilation, "build-module", (module) => {
const name = getModuleName(module);
Expand Down
Loading