Repository: next.js
Issue: No way to opt out of experimental optimizePackageImports, which breaks Cypress component tests
Labels: SWC,Webpack,Pages Router,Module Resolution

### Link to the code that reproduces this issue

https://github.com/redbmk/next-optimize-package-imports-bug

### To Reproduce

1. Create an app that uses the App router
    - `bun create next-app cypress-mui-bug`
2. Install Cypress and MUI
    - `bun add -d cypress && bun add @mui/material @emotion/react @emotion/styled`
3. Open cypress to initialize required files
    - `bunx cypress open`
4. Close cypress and create component with a test
    ```tsx
    // src/hello.tsx
    import { Paper } from '@mui/material'
    export const HelloWorld = () => <Paper>Hello world</Paper>
    
    // src/hello.cy.tsx
    import { HelloWorld } from './hello'
    it("should mount", () => cy.mount(<HelloWorld />))
    ```



### Current vs. Expected behavior

Current behavior is you will see an error and the test will fail:

```
ERROR in ./node_modules/@mui/material/Paper/index.js
Module build failed (from ./node_modules/next/dist/build/webpack/loaders/next-swc-loader.js):
Error: 
  x Using `export * from '...'` in a page is disallowed. Please use `export { default } from '...'` instead.
  | Read more: https://nextjs.org/docs/messages/export-all-in-page
   ,-[/path/to/repo/node_modules/@mui/material/Paper/index.js:2:1]
 2 | 
 3 | export { default } from './Paper';
 4 | export { default as paperClasses } from './paperClasses';
 5 | export * from './paperClasses';
   : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   `----

client (webpack 5.90.0) compiled with 1 error in 412 ms
```

Expected behavior is that the test will pass. Ideally with no extra configuration, but perhaps with something like:

```javascript
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    optimizePackageImports: process.env.CYPRESS === "true" ? false : undefined,
  }
};

export default nextConfig;
```

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 21.5.0
  npm: 10.2.4
  Yarn: 1.22.21
  pnpm: 8.13.1
  bun: 1.1.17
Relevant Packages:
  next: 14.2.4 // Latest available version is detected (14.2.4).
  eslint-config-next: 14.2.4
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.3
  @emotion/react: 11.11.4
  @emotion/styled: 11.11.5
  @mui/material: 5.16.0
  cypress: 13.13.0
Next.js Config:
  output: N/A
```


### Which area(s) are affected? (Select all that apply)

Developer Experience, Documentation, Module Resolution, Pages Router, SWC, Webpack

### Which stage(s) are affected? (Select all that apply)

Other (Deployed)

### Additional context

I've tried a few different things to fix or workaround this.

One thing I found was that if I create a `pages` folder, the errors go away. However, since I'm using the app router, that also introduces all kinds of other side effects, such as `useSearchParams` now showing up as possibly being `null` in typescript. It's also unclear to me if that means that `optimizePackageImports` is essentially disabled even when using `next build`.

I tried commenting out [this line](https://github.com/vercel/next.js/blob/6d92ff8e1764e025721f66a270d5c7304a657cdb/packages/next/src/server/config.ts#L847) and that works, but ideally I'm not having to patch anything in `node_modules`. As far as I can tell, there's no way to remove anything from `optimizePackageImports` - there's a default set and you can only add to it, but can't remove anything.

Considering this is marked as "experimental" I would expect to have to opt _in_ to it, but at the very least there should be a way to opt out.

I also found that this same setup works fine in NextJS `14.1.4`, but breaks in `14.2.0` (the next stable release). That's a pretty large release so might take a bit to track down exactly when it broke. The diff between `v14.1.4..v14.2.0` on `config.ts` is pretty minimal and seems unrelated to this. It could be a change in `swc` or some changes related to the Pages router (It seems like Cypress probably creates a "page" when running component tests)