Skip to content

Commit

Permalink
fix: console logging for component instance proxies (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
skirtles-code committed Jun 14, 2023
1 parent eb41c3a commit bb0e143
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/output/srcdoc.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,18 @@
['clear', 'log', 'info', 'dir', 'warn', 'error', 'table'].forEach((level) => {
const original = console[level];
console[level] = (...args) => {
const msg = String(args[0])
if (
msg.includes('You are running a development build of Vue') ||
msg.includes('You are running the esm-bundler build of Vue')
) {
return
const msg = args[0]
if (typeof msg === 'string') {
if (
msg.includes('You are running a development build of Vue') ||
msg.includes('You are running the esm-bundler build of Vue')
) {
return
}
}

original(...args);

const stringifiedArgs = stringify(args);
if (
previous.level === level &&
Expand All @@ -158,13 +163,9 @@
try {
parent.postMessage({ action: 'console', level, args }, '*');
} catch (err) {
parent.postMessage({ action: 'console', level, args: args.map(a => {
return a instanceof Error ? a.message : String(a)
}) }, '*');
parent.postMessage({ action: 'console', level, args: args.map(toString) }, '*');
}
}

original(...args);
}
});

Expand Down Expand Up @@ -246,9 +247,26 @@
original_trace(...args);
};

function toString(value) {
if (value instanceof Error) {
return value.message;
}
for (const fn of [String, v => Object.prototype.toString.call(v), v => typeof v]) {
try {
return fn(value);
} catch (err) {}
}
}

function isComponentProxy(value) {
return value && typeof value === 'object' && value.__v_skip === true && typeof value.$nextTick === 'function' && value.$ && value._;
}

function stringify(args) {
try {
return JSON.stringify(args);
return JSON.stringify(args, (key, value) => {
return isComponentProxy(value) ? '{component proxy}' : value;
});
} catch (error) {
return null;
}
Expand Down

0 comments on commit bb0e143

Please sign in to comment.