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: timed plugin context #4357

Merged
merged 2 commits into from Jan 21, 2022
Merged
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
16 changes: 7 additions & 9 deletions src/utils/timers.ts
@@ -1,4 +1,4 @@
import { InputOptions, Plugin, SerializedTimings } from '../rollup/types';
import type { InputOptions, Plugin, SerializedTimings } from '../rollup/types';
import performance from './performance';
import process from './process';

Expand Down Expand Up @@ -77,18 +77,19 @@ export let timeEnd: (label: string, level?: number) => void = NOOP;
const TIMED_PLUGIN_HOOKS = ['load', 'resolveDynamicImport', 'resolveId', 'transform'] as const;

function getPluginWithTimers(plugin: any, index: number): Plugin {
const timedPlugin: Pick<Plugin, typeof TIMED_PLUGIN_HOOKS[number]> = {};

for (const hook of TIMED_PLUGIN_HOOKS) {
if (hook in plugin) {
let timerLabel = `plugin ${index}`;
if (plugin.name) {
timerLabel += ` (${plugin.name})`;
}
timerLabel += ` - ${hook}`;
timedPlugin[hook] = function (...args: unknown[]) {

const func = plugin[hook];

plugin[hook] = function (...args: readonly unknown[]) {
timeStart(timerLabel, 4);
const result = plugin[hook](...args);
const result = func.apply(this, args);
timeEnd(timerLabel, 4);
if (result && typeof result.then === 'function') {
timeStart(`${timerLabel} (async)`, 4);
Expand All @@ -101,10 +102,7 @@ function getPluginWithTimers(plugin: any, index: number): Plugin {
};
}
}
return {
...plugin,
...timedPlugin
};
return plugin;
}

export function initialiseTimers(inputOptions: InputOptions): void {
Expand Down
27 changes: 27 additions & 0 deletions test/function/samples/adds-plugin-context-to-plugins/_config.js
@@ -0,0 +1,27 @@
const assert = require('assert');

module.exports = {
description: 'Adds plugin context to plugins with perf=true',
options: {
perf: true,
plugins: [
{
load() {
assert.ok(typeof this.parse === 'function');
},

resolveDynamicImport() {
assert.ok(typeof this.parse === 'function');
},

resolveId() {
assert.ok(typeof this.parse === 'function');
},

transform() {
assert.ok(typeof this.parse === 'function');
}
}
]
}
};
@@ -0,0 +1 @@
export default 'foo';
@@ -0,0 +1 @@
import('./foo');