PepsRyuu / nollup

Rollup compatible development bundler for fast rebuilds and HMR.
MIT License
488 stars 28 forks source link

DevServer API error on CommonJs: TypeError: plugin.execute[hook].apply is not a function #239

Closed afialapis closed 2 years ago

afialapis commented 2 years ago

Hi there, sorry to disturb again!

When using DevServer API, I find this error:

TypeError: plugin.execute[hook].apply is not a function
    at _callAsyncHook (/home/lapis/repos/gh/test-nollup-error-on-node-resolve/node_modules/nollup/lib/impl/PluginLifecycle.js:22:39)
    at callAsyncFirstHook (/home/lapis/repos/gh/test-nollup-error-on-node-resolve/node_modules/nollup/lib/impl/PluginLifecycle.js:54:24)
    at async Object.resolveIdImpl (/home/lapis/repos/gh/test-nollup-error-on-node-resolve/node_modules/nollup/lib/impl/PluginLifecycle.js:204:18)
    at async Object.resolve (/home/lapis/repos/gh/test-nollup-error-on-node-resolve/node_modules/nollup/lib/impl/PluginContext.js:122:28)
    at async Object.resolveId (file:///home/lapis/repos/gh/test-nollup-error-on-node-resolve/node_modules/@rollup/plugin-commonjs/dist/es/index.js:574:10)
    at async PluginErrorHandler.wrapAsync (/home/lapis/repos/gh/test-nollup-error-on-node-resolve/node_modules/nollup/lib/impl/PluginErrorHandler.js:98:22)
    at async _callAsyncHook (/home/lapis/repos/gh/test-nollup-error-on-node-resolve/node_modules/nollup/lib/impl/PluginLifecycle.js:25:21)
    at async callAsyncFirstHook (/home/lapis/repos/gh/test-nollup-error-on-node-resolve/node_modules/nollup/lib/impl/PluginLifecycle.js:54:18)
    at async Object.resolveIdImpl (/home/lapis/repos/gh/test-nollup-error-on-node-resolve/node_modules/nollup/lib/impl/PluginLifecycle.js:204:18)
    at async Array.resolveId (/home/lapis/repos/gh/test-nollup-error-on-node-resolve/node_modules/nollup/lib/impl/PluginLifecycle.js:299:24)

It can be reproduced on this repo, by running the command npm run demo.

As suggested here (as it seemed similar problems) I tried by using liveBindings: true and also rollup-plugin-commonjs-alternate plugin, unsuccessfully.

I can easily patch the problem, around here:

async function _callAsyncHook (plugin, hook, args) {
...
        // let hr = plugin.execute[hook].apply(plugin.context, args);

        let hr = typeof(plugin.execute[hook].handler)=='function'
          ? plugin.execute[hook].handler.apply(plugin.context, args)
          : plugin.execute[hook].apply(plugin.context, args); 
...

Because, in the case of commonjs, resolveId is not a function but an object like this:

{ order: 'post', handler: [AsyncFunction: handler] }

As for other plugins, resolveId hook is not async, so problem could be related, maybe?

Am I calling DevServer as expected or do I miss something? Could problem be avoided somehow?

Anyway, as I can easily patch it and keep going, this is not problematic at all. Just letting you know and hopefully being helpful!

Thanks once again for your time!

PepsRyuu commented 2 years ago

It's a new Rollup feature it seems: https://rollupjs.org/guide/en/#build-hooks Nollup at the moment expects all plugin hooks to be functions, with no support for the object format.

Shouldn't be difficult to add from the looks of it. I should have a bit of time over the weekend to look into this. :)

PepsRyuu commented 2 years ago

Fixed in 0.21.0! https://github.com/PepsRyuu/nollup/pull/240

afialapis commented 2 years ago

Ahh super. Many thanks for your help and time, really appreciated!