Unverified Commit 672736c4 authored by Gal Schlezinger's avatar Gal Schlezinger Committed by GitHub

[edge] Make runtime addressable in compile time (#38288)

this commit allows to use EdgeRuntime as a dead code eliminator identifier:

```ts
if (typeof EdgeRuntime !== "undefined") {
  console.log("will be stripped away");
} else {
  console.log("will be kept in the bundle");
}
```

which means we're replacing `EdgeRuntime` with a literal

## Related

- Fixes #30739

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent e6bad25b
...@@ -1480,6 +1480,11 @@ export default async function getBaseWebpackConfig( ...@@ -1480,6 +1480,11 @@ export default async function getBaseWebpackConfig(
[`process.env.${key}`]: JSON.stringify(config.env[key]), [`process.env.${key}`]: JSON.stringify(config.env[key]),
} }
}, {}), }, {}),
...(compilerType !== 'edge-server'
? {}
: {
EdgeRuntime: JSON.stringify('edge-runtime'),
}),
// TODO: enforce `NODE_ENV` on `process.env`, and add a test: // TODO: enforce `NODE_ENV` on `process.env`, and add a test:
'process.env.NODE_ENV': JSON.stringify( 'process.env.NODE_ENV': JSON.stringify(
dev ? 'development' : 'production' dev ? 'development' : 'production'
......
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'
import fs from 'fs-extra'
import path from 'path'
describe('Edge Runtime is addressable', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
'middleware.js': `
import { NextResponse } from 'next/server'
if (typeof EdgeRuntime === 'undefined') {
console.log("EdgeRuntime is undefined");
} else {
console.log("EdgeRuntime is defined");
}
export default (req) => {
return NextResponse.next({
headers: {
'x-runtime-version': EdgeRuntime
}
})
}
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())
test('EdgeRuntime evaluates to a string', async () => {
const resp = await fetchViaHTTP(next.url, '/')
expect(await resp.text()).toContain('hello world')
expect(Object.fromEntries(resp.headers)).toMatchObject({
'x-runtime-version': 'edge-runtime',
})
})
test('removes the undefined branch with dead code elimination', async () => {
const compiledMiddlewareFile = await fs.readFile(
path.join(next.testDir, '.next/server/middleware.js'),
'utf8'
)
expect(compiledMiddlewareFile).toContain('EdgeRuntime is defined')
expect(compiledMiddlewareFile).not.toContain('EdgeRuntime is undefined')
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment