diff --git a/WrappedPlugin/index.js b/WrappedPlugin/index.js index 64875f1..0f1c42c 100644 --- a/WrappedPlugin/index.js +++ b/WrappedPlugin/index.js @@ -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); @@ -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; }); @@ -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, @@ -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, @@ -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, @@ -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, @@ -115,13 +115,13 @@ 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 proxy = new Proxy(hooks[method], { + const genProxy = (method) => { + const proxy = new Proxy(method, { get: (target, property) => { const raw = Reflect.get(target, property); @@ -148,11 +148,21 @@ const wrapHooks = (orig, pluginName, smp, type) => { return proxy; }; - const wrapped = Object.keys(hooks).reduce((acc, method) => { - acc[method] = genProxy(method); + const wrappedMethods = Object.keys(hooks).reduce((acc, method) => { + acc[method] = genProxy(hooks[method]); return acc; }, {}); + const wrapped = new Proxy(hooks, { + get: (target, property) => { + return wrappedMethods[property]; + }, + set: (target, property, value) => { + wrappedMethods[property] = genProxy(value); + return true; + }, + }); + wrappedHooks.push({ orig: hooks, wrapped, pluginName }); return wrapped; @@ -170,7 +180,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; }; @@ -179,15 +190,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; @@ -196,7 +207,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(); diff --git a/__tests__/setups/v4-html-webpack-plugin/package-lock.json b/__tests__/setups/v4-html-webpack-plugin/package-lock.json index d1fbc4e..5582c78 100644 --- a/__tests__/setups/v4-html-webpack-plugin/package-lock.json +++ b/__tests__/setups/v4-html-webpack-plugin/package-lock.json @@ -8089,6 +8089,12 @@ "uniqs": "^2.0.0" } }, + "preload-webpack-plugin": { + "version": "3.0.0-beta.4", + "resolved": "https://registry.npmjs.org/preload-webpack-plugin/-/preload-webpack-plugin-3.0.0-beta.4.tgz", + "integrity": "sha512-6hhh0AswCbp/U4EPVN4fbK2wiDkXhmgjjgEYEmXa21UYwjYzCIgh3ZRMXM21ZPLfbQGpdFuSL3zFslU+edjpwg==", + "dev": true + }, "prelude-ls": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.1.2.tgz", diff --git a/__tests__/setups/v4-html-webpack-plugin/package.json b/__tests__/setups/v4-html-webpack-plugin/package.json index 9e77462..a7285fd 100644 --- a/__tests__/setups/v4-html-webpack-plugin/package.json +++ b/__tests__/setups/v4-html-webpack-plugin/package.json @@ -19,6 +19,7 @@ "css-loader": "^0.28.11", "html-webpack-plugin": "^4.5.1", "jest": "^26.6.3", + "preload-webpack-plugin": "3.0.0-beta.4", "style-loader": "^0.20.3", "webpack": "^4.46.0" } diff --git a/__tests__/setups/v4-html-webpack-plugin/webpack.config.js b/__tests__/setups/v4-html-webpack-plugin/webpack.config.js index 7e3a064..b114fca 100644 --- a/__tests__/setups/v4-html-webpack-plugin/webpack.config.js +++ b/__tests__/setups/v4-html-webpack-plugin/webpack.config.js @@ -1,5 +1,6 @@ const webpack = require("webpack"); const HtmlWebpackPlugin = require("html-webpack-plugin"); +const PreloadWebpackPlugin = require("preload-webpack-plugin"); module.exports = { entry: { @@ -11,6 +12,7 @@ module.exports = { plugins: [ new webpack.DefinePlugin({ FOO: "'BAR'" }), new HtmlWebpackPlugin(), + new PreloadWebpackPlugin(), ], module: { rules: [ diff --git a/package-lock.json b/package-lock.json index 9e81d3c..897f7a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "speed-measure-webpack-plugin", - "version": "1.4.0", + "version": "1.5.0", "lockfileVersion": 1, "requires": true, "dependencies": {