next.js: `swcMinify: true` does not produce an usable build with v12.1.5

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

 $ npx --no-install next info

    Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64
    Binaries:
      Node: 16.13.1
      npm: 8.1.2
      Yarn: 1.22.18
      pnpm: N/A
    Relevant packages:
      next: 12.1.5
      react: 17.0.2
      react-dom: 17.0.2

What browser are you using? (if relevant)

Not relevant

How are you deploying your application? (if relevant)

next start after a next build

Describe the Bug

Locally (running the application locally, in my IDE), everything looks good. Our tests are green as well. Only the PROD artefact is somehow rubbish.

When accessing the homepage, this is what we get in the console:

console

When clicking on one of the links displayed, this is what I get:

error_decoder

Expected Behavior

No error messages in the console, application is running properly and swcMinify is set to true, like it used to be for v12.1.0 (application was NOT working with the releases from v12.1.1 to v12.1.4).

The expected behavior could be achieved if I set swcMinify to false in the configuration file.

To Reproduce

Kindly follow instructions given in the README.md, Production-like local deployment.

More info on the problem could be found here and there.

Please, let me know if you have questions, or whether I should deeply investigate in a given direction, or if I can do anything here to help.

Thanks in advance for your time and investigation.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 9
  • Comments: 42 (19 by maintainers)

Commits related to this issue

Most upvoted comments

@VeronikaSergiyenko I think it will be fixed by https://github.com/vercel/next.js/pull/36535

React now uses google closure compiler as their minifier, and it triggered a bug of swc minifier. So I disabled the problematic pass for now and I’m working on a full fix for alias analyzer

@seanparmelee image

It will be fixed by https://github.com/vercel/next.js/pull/36351

(I used my own script to replace next-swc binary)

@kdy1 I can reproduce this error using a dependency as example:

next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
  swcMinify: true,
};

module.exports = nextConfig;
package.json
{
  "name": "swc-test",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@codemirror/text": "^0.19.6",
    "next": "12.1.6",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  },
  "devDependencies": {
    "@types/node": "18.0.0",
    "@types/react": "18.0.14",
    "@types/react-dom": "18.0.5",
    "eslint": "8.18.0",
    "eslint-config-next": "12.1.6",
    "typescript": "4.7.4"
  }
}

index.tsx
import type { NextPage } from "next";
import { Text } from "@codemirror/text";

const Home: NextPage = () => {
  const length = Text.of(["Hello World"]).length;

  return <p>{length}</p>;
};

export default Home;

The variable value should always return 11, but if you set the swcMinify to true it returns -1. As far I could understand, this option is generating a different output of the following piece of code.

// Wrong
swcMinify: true,

// @codemirror/text
constructor(a, b=function(a) {
  for (let b of a)
    b.length;
  return -1
} 

// Correct
swcMinify: false,

// @codemirror/text
constructor(a, b=function(a) {
  let e = -1
  for (let b of a)
    e += b.length + 1;
  return e
}

Hope it helps


Edit: it has been fixed on Canary

We have been running into this issue in one of our apps as well (even on the latest canary). In our case, the following error is thrown:

TypeError: j is not a function
    at r.exports (util.js:1:46666)
    at Object.112 (util.js:1:46666)
    at __nccwpck_require__ (util.js:1:46666)
    at Object.749 (util.js:1:46666)
    at __nccwpck_require__ (util.js:1:46666)
    at Object.715 (util.js:1:46666)
    at __nccwpck_require__ (util.js:1:46666)
    at Object.3 (util.js:1:46666)
    at __nccwpck_require__ (util.js:1:46666)
    at Object.650 (util.js:1:46666)

After some process of elimination, the culprit turned out to be some code that was using

import { isString } from 'util';

And removing that code makes the error goes away.

I pushed up a simple reproduction here in hopes it helps get to the root cause of the issue (this form of it anyway): https://github.com/seanparmelee/nextjs-36127-reproduction