Skip to content

Commit

Permalink
fix: timed plugin context (#4357)
Browse files Browse the repository at this point in the history
* fix: timed plugin context

* add test
  • Loading branch information
dnalborczyk committed Jan 21, 2022
1 parent a5988dc commit d1079a3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
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');

0 comments on commit d1079a3

Please sign in to comment.