diff --git a/src/utils/timers.ts b/src/utils/timers.ts index d097f15131f..e8554eb3307 100644 --- a/src/utils/timers.ts +++ b/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'; @@ -77,8 +77,6 @@ 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 = {}; - for (const hook of TIMED_PLUGIN_HOOKS) { if (hook in plugin) { let timerLabel = `plugin ${index}`; @@ -86,9 +84,12 @@ function getPluginWithTimers(plugin: any, index: number): Plugin { 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); @@ -101,10 +102,7 @@ function getPluginWithTimers(plugin: any, index: number): Plugin { }; } } - return { - ...plugin, - ...timedPlugin - }; + return plugin; } export function initialiseTimers(inputOptions: InputOptions): void { diff --git a/test/function/samples/adds-plugin-context-to-plugins/_config.js b/test/function/samples/adds-plugin-context-to-plugins/_config.js new file mode 100644 index 00000000000..df0c2752171 --- /dev/null +++ b/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'); + } + } + ] + } +}; diff --git a/test/function/samples/adds-plugin-context-to-plugins/foo.js b/test/function/samples/adds-plugin-context-to-plugins/foo.js new file mode 100644 index 00000000000..d02ba545bd3 --- /dev/null +++ b/test/function/samples/adds-plugin-context-to-plugins/foo.js @@ -0,0 +1 @@ +export default 'foo'; diff --git a/test/function/samples/adds-plugin-context-to-plugins/main.js b/test/function/samples/adds-plugin-context-to-plugins/main.js new file mode 100644 index 00000000000..92c66c7eb7c --- /dev/null +++ b/test/function/samples/adds-plugin-context-to-plugins/main.js @@ -0,0 +1 @@ +import('./foo');