preactjs / next-plugin-preact

Next.js plugin for preact X
396 stars 9 forks source link

Latest version of `next@10.2.2` broke the build using `next-plugin-preact` #31

Closed nickrttn closed 3 years ago

nickrttn commented 3 years ago

Hi!

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch next-plugin-preact@3.0.4 for the project I'm working on.

Since the last version of next@10.2.2 when using webpack@5, during a build, I've been seeing the following error:

> Build error occurred
TypeError: Cannot read property 'framework' of undefined
    at Object.webpack (/Users/nick/dev/els-jhp/node_modules/next-plugin-preact/index.js:46:27)

I believe it has been caused by this change: https://github.com/vercel/next.js/pull/25251.

When I log splitChunks in next-plugin-preact, I'm seeing two different instances of splitChunks, one of which doesn't contain cacheGroups. A simple check fixed the issue and passed the build for me. I'm not sure if this is the right place to fix the issue or it needs to be raised over at Next.js.

A reduced test case of the bug would be to npx create-next-app --example using-preact using-preact-app, install the latest version of Next.js and enable webpack@5 by adding future: { webpack5: true } in next.config.js, then running yarn/npm build.

Here is the diff that solved my problem:

diff --git a/node_modules/next-plugin-preact/index.js b/node_modules/next-plugin-preact/index.js
index e1ba250..30044ef 100644
--- a/node_modules/next-plugin-preact/index.js
+++ b/node_modules/next-plugin-preact/index.js
@@ -40,7 +40,7 @@ module.exports = function withPreact(nextConfig = {}) {
         // Move Preact into the framework chunk instead of duplicating in routes:
         const splitChunks =
           config.optimization && config.optimization.splitChunks;
-        if (splitChunks) {
+        if (splitChunks && 'cacheGroups' in splitChunks) {
           const cacheGroups = splitChunks.cacheGroups;
           const test = /[\\/]node_modules[\\/](preact|preact-render-to-string|preact-context-provider)[\\/]/;
           if (cacheGroups.framework) {

This issue body was partially generated by patch-package.

JoviDeCroock commented 3 years ago

Fixed by #32 & published as 3.0.6

nickrttn commented 3 years ago

Thanks!