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

Making this package compatible with Webpack v5 #194

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
33 changes: 18 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,9 @@ const wrapHooks = (orig, pluginName, smp, type) => {
};

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is too hardcoded, now it is arguing with additionalChunkAssets
so I changed it to be like

if (hooks[method])
  acc[method] = genProxy(method);

//removed in webpack v5
acc[method] = genProxy(method);
return acc;
}, {});

Expand All @@ -170,7 +172,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 +182,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 +199,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
29 changes: 27 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,21 @@ module.exports = class SpeedMeasurePlugin {
);
}

wrap(config) {
wrap(config, 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 +77,7 @@ module.exports = class SpeedMeasurePlugin {
this.smpPluginAdded = true;
}

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

Expand Down Expand Up @@ -253,9 +263,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