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

fix #91 #182

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
fix #91 hooks was not found
  • Loading branch information
SyMind committed Nov 27, 2021
commit 5512dd3c48b1ca182d0bf67ac70738dcd3a8c09e
45 changes: 28 additions & 17 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,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);

Expand All @@ -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;
Expand All @@ -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;
};
Expand All @@ -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;
Expand All @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions __tests__/setups/v4-html-webpack-plugin/package-lock.json

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

1 change: 1 addition & 0 deletions __tests__/setups/v4-html-webpack-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
2 changes: 2 additions & 0 deletions __tests__/setups/v4-html-webpack-plugin/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const PreloadWebpackPlugin = require("preload-webpack-plugin");

module.exports = {
entry: {
Expand All @@ -11,6 +12,7 @@ module.exports = {
plugins: [
new webpack.DefinePlugin({ FOO: "'BAR'" }),
new HtmlWebpackPlugin(),
new PreloadWebpackPlugin(),
],
module: {
rules: [
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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