Repository: next.js
Issue: `optimizePackageImports` + `rxjs` fails to build in a turborepo
Labels: bug,SWC,linear: next

### Link to the code that reproduces this issue

https://github.com/decepulis/nextjs-barrel-reproduction

### To Reproduce

I [created a new turborepo](https://turbo.build/repo/docs/getting-started/create-new) and installed `next@canary` in the example web and docs directories. I then installed `rxjs` in the web directory.

`apps/web/app/page.tsx` has the following import: 
```jsx
import { interval, take, lastValueFrom } from "rxjs";
```

### Current vs. Expected behavior

`yarn build` results in the following errors:
```
Attempted import error: 'take' is not exported from '__barrel_optimize__?names=interval,lastValueFrom,take!=!rxjs' (imported as 'take').
Attempted import error: 'lastValueFrom' is not exported from '__barrel_optimize__?names=interval,lastValueFrom,take!=!rxjs' (imported as 'lastValueFrom').
```
<details>
<summary>Here's a full log of the build, filtered by web for clarity</summary>
<pre>
dcepulis@dc-mux nextjs-barrel-reproduction % yarn build --filter=web
yarn run v1.22.17
$ turbo run build --filter=web
• Packages in scope: web
• Running build in 1 packages
• Remote caching disabled
web:build: cache miss, executing 5279913fc08b516e
web:build: $ next build
web:build:    Creating an optimized production build ...
web:build:  ⚠ Compiled with warnings
web:build: 
web:build: ./app/page.tsx
web:build: Attempted import error: 'take' is not exported from '__barrel_optimize__?names=interval,lastValueFrom,take!=!rxjs' (imported as 'take').
web:build: 
web:build: Import trace for requested module:
web:build: ./app/page.tsx
web:build: 
web:build: ./app/page.tsx
web:build: Attempted import error: 'lastValueFrom' is not exported from '__barrel_optimize__?names=interval,lastValueFrom,take!=!rxjs' (imported as 'lastValueFrom').
web:build: 
web:build: Import trace for requested module:
web:build: ./app/page.tsx
web:build: 
web:build:    Linting and checking validity of types ...
web:build:    Collecting page data ...
web:build:    Generating static pages (0/5) ...
   Generating static pages (1/5) 
   Generating static pages (2/5) 
   Generating static pages (3/5) 
web:build: TypeError: (0 , a.take) is not a function
web:build:     at p (/Users/dcepulis/Desktop/nextjs-barrel-reproduction/apps/web/.next/server/app/page.js:1:3477)
web:build:     at _ (/Users/dcepulis/Desktop/nextjs-barrel-reproduction/apps/web/.next/server/app/page.js:1:4276)
web:build:     at /Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:429875
web:build:     at /Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:430007
web:build:     at async ge (/Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:429696)
web:build:     at async /Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:428789
web:build:     at async Promise.all (index 0)
web:build:     at async ge (/Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:428553)
web:build:     at async /Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:434268
web:build: 
web:build: Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
web:build: TypeError: (0 , a.take) is not a function
web:build:     at p (/Users/dcepulis/Desktop/nextjs-barrel-reproduction/apps/web/.next/server/app/page.js:1:3477)
web:build:     at _ (/Users/dcepulis/Desktop/nextjs-barrel-reproduction/apps/web/.next/server/app/page.js:1:4276)
web:build:     at /Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:429875
web:build:     at /Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:430007
web:build:     at async ge (/Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:429696)
web:build:     at async /Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:428789
web:build:     at async Promise.all (index 0)
web:build:     at async ge (/Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:428553)
web:build:     at async /Users/dcepulis/Desktop/nextjs-barrel-reproduction/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:1:434268
 ✓ Generating static pages (5/5) 
web:build: 
web:build: > Export encountered errors on following paths:
web:build:      /page: /
web:build: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
web:build: error Command failed with exit code 1.
web:build: ERROR: command finished with error: command (/Users/dcepulis/Desktop/nextjs-barrel-reproduction/apps/web) yarn run build exited (1)
web#build: command (/Users/dcepulis/Desktop/nextjs-barrel-reproduction/apps/web) yarn run build exited (1)

 Tasks:    0 successful, 1 total
Cached:    0 cached, 1 total
  Time:    5.242s 
Failed:    web#build

 ERROR  run failed: command  exited (1)
error Command failed with exit code 1.
</pre>
</details>

### Verify canary release

- [X] I verified that the issue exists in the latest Next.js canary release

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
Binaries:
  Node: 18.16.0
  npm: 9.5.1
  Yarn: 1.22.17
  pnpm: 7.12.2
Relevant Packages:
  next: 13.5.3-canary.1
  eslint-config-next: 13.5.3-canary.1
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 4.9.5
Next.js Config:
  output: N/A
```


### Which area(s) are affected? (Select all that apply)

App Router, SWC transpilation

### Additional context

- I've seen the issue locally and in prod on Vercel
- I've only seen the issue in my turborepo; rxjs works fine in a single-package project
- I first saw the issue in our app when I installed `next-sanity`, which has `rxjs` as a dependency
- I did not observe this issue with `lodash` and `date-fns`.
- you're the best maintainers thank you for all your hard work

<sub>[NEXT-1684](https://linear.app/vercel/issue/NEXT-1684/optimizepackageimports-rxjs-fails-to-build-in-a-turborepo)</sub>