From b1dec87710dfac44ca4136d218e47072882f1df0 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Wed, 19 Jan 2022 22:29:46 -0500 Subject: [PATCH 1/2] fix: timed plugin context --- src/utils/timers.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) 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 { From efc5ca6768380efad659ebcd7af4f2fccba339be Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Thu, 20 Jan 2022 20:37:39 -0500 Subject: [PATCH 2/2] add test --- .../adds-plugin-context-to-plugins/_config.js | 27 +++++++++++++++++++ .../adds-plugin-context-to-plugins/foo.js | 1 + .../adds-plugin-context-to-plugins/main.js | 1 + 3 files changed, 29 insertions(+) create mode 100644 test/function/samples/adds-plugin-context-to-plugins/_config.js create mode 100644 test/function/samples/adds-plugin-context-to-plugins/foo.js create mode 100644 test/function/samples/adds-plugin-context-to-plugins/main.js 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');