Skip to content

Commit d1079a3

Browse files
authored
fix: timed plugin context (#4357)
* fix: timed plugin context * add test
1 parent a5988dc commit d1079a3

4 files changed

Lines changed: 36 additions & 9 deletions

File tree

src/utils/timers.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InputOptions, Plugin, SerializedTimings } from '../rollup/types';
1+
import type { InputOptions, Plugin, SerializedTimings } from '../rollup/types';
22
import performance from './performance';
33
import process from './process';
44

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

7979
function getPluginWithTimers(plugin: any, index: number): Plugin {
80-
const timedPlugin: Pick<Plugin, typeof TIMED_PLUGIN_HOOKS[number]> = {};
81-
8280
for (const hook of TIMED_PLUGIN_HOOKS) {
8381
if (hook in plugin) {
8482
let timerLabel = `plugin ${index}`;
8583
if (plugin.name) {
8684
timerLabel += ` (${plugin.name})`;
8785
}
8886
timerLabel += ` - ${hook}`;
89-
timedPlugin[hook] = function (...args: unknown[]) {
87+
88+
const func = plugin[hook];
89+
90+
plugin[hook] = function (...args: readonly unknown[]) {
9091
timeStart(timerLabel, 4);
91-
const result = plugin[hook](...args);
92+
const result = func.apply(this, args);
9293
timeEnd(timerLabel, 4);
9394
if (result && typeof result.then === 'function') {
9495
timeStart(`${timerLabel} (async)`, 4);
@@ -101,10 +102,7 @@ function getPluginWithTimers(plugin: any, index: number): Plugin {
101102
};
102103
}
103104
}
104-
return {
105-
...plugin,
106-
...timedPlugin
107-
};
105+
return plugin;
108106
}
109107

110108
export function initialiseTimers(inputOptions: InputOptions): void {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const assert = require('assert');
2+
3+
module.exports = {
4+
description: 'Adds plugin context to plugins with perf=true',
5+
options: {
6+
perf: true,
7+
plugins: [
8+
{
9+
load() {
10+
assert.ok(typeof this.parse === 'function');
11+
},
12+
13+
resolveDynamicImport() {
14+
assert.ok(typeof this.parse === 'function');
15+
},
16+
17+
resolveId() {
18+
assert.ok(typeof this.parse === 'function');
19+
},
20+
21+
transform() {
22+
assert.ok(typeof this.parse === 'function');
23+
}
24+
}
25+
]
26+
}
27+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'foo';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import('./foo');

0 commit comments

Comments
 (0)