Repository: next.js
Issue: Code with a worker fails to build under the edge runtime, even if it's dead code.
Labels: bug,Runtime

### Link to the code that reproduces this issue

https://github.com/uonr/nextjs-edge-worker-issue-repoducetion/blob/master/src/app/FromWorker.tsx

### To Reproduce

1. Build it

### Current vs. Expected behavior

Expected build success, but:

```
> repoduction-worker-issue@0.1.0 build
> next build

  ▲ Next.js 14.2.3

   Creating an optimized production build ...
 ✓ Compiled successfully
 ✓ Linting and checking validity of types    
   Collecting page data  ./Users/user/Repositories/repoduction-worker-issue/.next/server/edge-runtime-webpack.js:1
(()=>{"use strict";var e={},r={};function t(o){var n=r[o];if(void 0!==n)return n.exports;var i=r[o]={exports:{}},l=!0;try{e[o](i,i.exports,t),l=!1}finally{l&&delete r[o]}return i.exports}t.m=e,(()=>{var e=[];t.O=(r,o,n,i)=>{if(o){i=i||0;for(var l=e.length;l>0&&e[l-1][2]>i;l--)e[l]=e[l-1];e[l]=[o,n,i];return}for(var a=1/0,l=0;l<e.length;l++){for(var[o,n,i]=e[l],u=!0,f=0;f<o.length;f++)a>=i&&Object.keys(t.O).every(e=>t.O[e](o[f]))?o.splice(f--,1):(u=!1,i<a&&(a=i));if(u){e.splice(l--,1);var s=n();void 0!==s&&(r=s)}}return r}})(),t.n=e=>{var r=e&&e.__esModule?()=>e.default:()=>e;return t.d(r,{a:r}),r},t.d=(e,r)=>{for(var o in r)t.o(r,o)&&!t.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:r[o]})},t.e=()=>Promise.resolve(),t.u=e=>""+e+".js",t.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||Function("return this")()}catch(e){if("object"==typeof window)return window}}(),t.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),t.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.p="/_next/",(()=>{t.b=document.baseURI||self.location.href;var e={993:0};t.O.j=r=>0===e[r];var r=(r,o)=>{var n,i,[l,a,u]=o,f=0;if(l.some(r=>0!==e[r])){for(n in a)t.o(a,n)&&(t.m[n]=a[n]);if(u)var s=u(t)}for(r&&r(o);f<l.length;f++)i=l[f],t.o(e,i)&&e[i]&&e[i][0](),e[i]=0;return t.O(s)},o=self.webpackChunk_N_E=self.webpackChunk_N_E||[];o.forEach(r.bind(null,0)),o.push=r.bind(null,o.push.bind(o))})()})();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

ReferenceError: document is not defined
    at /Users/user
/Repositories/repoduction-worker-issue/.next/server/edge-runtime-webpack.js:1:1156
    at /Users/user/Repositories/repoduction-worker-issue/.next/server/edge-runtime-webpack.js:1:1530
    at /Users/user/Repositories/repoduction-worker-issue/.next/server/edge-runtime-webpack.js:1:1534
    at Script.runInContext (node:vm:148:12)
    at runInContext (node:vm:300:6)
    at evaluateInContext (/Users/user/Repositories/repoduction-worker-issue/node_modules/next/dist/server/web/sandbox/context.js:403:38)
    at getRuntimeContext (/Users/user/Repositories/repoduction-worker-issue/node_modules/next/dist/server/web/sandbox/sandbox.js:71:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/user/Repositories/repoduction-worker-issue/node_modules/next/dist/build/utils.js:1089:29
    at async Span.traceAsyncFn (/Users/user/Repositories/repoduction-worker-issue/node_modules/next/dist/trace/trace.js:154:20)

> Build error occurred
Error: Failed to collect page data for /
    at /Users/user/Repositories/repoduction-worker-issue/node_modules/next/dist/build/utils.js:1268:15
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  type: 'Error'
}
   Collecting page data
```

### Provide environment information

```bash
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000
  Available memory (MB): 32768
  Available CPU cores: 10
Binaries:
  Node: 20.12.2
  npm: 10.5.0
  Yarn: N/A
  pnpm: 8.15.5
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: 14.2.3
  react: 18.3.0
  react-dom: 18.3.0
  typescript: 5.4.5
Next.js Config:
  output: N/A
```


### Which area(s) are affected? (Select all that apply)

Runtime

### Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local)

### Additional context

It looks like a code generation issue, the project that found this issue used babel instead of swc, but had the exact same problem