shellscape / koa-webpack

Development and Hot Reload Middleware for Koa2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Returning a promise when calling koa-webpack makes it awkward to work with.

2Pacalypse- opened this issue · comments

  • Node Version: 9.6.1
  • NPM Version: 5.7.1
  • koa Version: 2.5.1
  • koa-wepback Version: 5.0.2

Expected Behavior

To use the koa-webpack middleware in the same way any other middleware is used.

Actual Behavior

Calling the new version of koa-webpack doesn't actually return a middleware, but it returns a promise instead, which needs to be awaited. It makes things a little bit awkward since now all the server logic which must come after this middleware needs to be put inside a .then of the promise, which can be quite a bit of code. Compounding this problem is the fact that the middleware initialization is usually done in a top-level scope where await can't be used (without wrapping it in a IIFE).

Please don't remove portions of the issue template.

Promise is a standard pattern and it's entirely common for NPM modules to return them, and has been for some time. It shouldn't be awkward for any seasoned Node developer to use Promise at this point. Interestingly enough, Koa v2 is entirely based off of exactly that. IIFE are also perfectly acceptable until Node.js implements the top-level-await support that recently dropped in V8.

You're still completely free to use an older version of koa-webpack, or if you're not using the dynamic port capabilities of the latest webpack-hot-client, you can fork the project, assert a port to the hotClient config, and remove the need to wait thus removing the need for the Promise.

Unfortunately the public API of this module won't be reverted to previous versions.