-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[edge] favor browser exports for edge compiler (#38319)
this is a regression from the previous implementation where Next.js compiled Middleware using the client compiler. We used to favor the browser exports over the `module` and `main`, which allowed packages like `debug` to work without any changes on Edge Functions. This is no longer the case, and this commit fixes that. Side note: I believe that in the future we will also have a different key to symbolize edge deployments. Maybe it will be `winter` to refer to WinterCG, but only time will tell! Another side note: we need to add support for import maps for advanced use cases. ## Bug - [ ] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md`
- Loading branch information
Showing
2 changed files
with
55 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
test/e2e/edge-compiler-module-exports-preference/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { createNext } from 'e2e-utils' | ||
import { NextInstance } from 'test/lib/next-modes/base' | ||
import { fetchViaHTTP } from 'next-test-utils' | ||
|
||
describe('Edge compiler module exports preference', () => { | ||
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'; | ||
import lib from 'my-lib'; | ||
export default (req) => { | ||
return NextResponse.next({ | ||
headers: { | ||
'x-imported': lib | ||
} | ||
}) | ||
} | ||
`, | ||
'node_modules/my-lib/package.json': JSON.stringify({ | ||
name: 'my-lib', | ||
version: '1.0.0', | ||
main: 'index.js', | ||
browser: 'browser.js', | ||
}), | ||
'node_modules/my-lib/index.js': `module.exports = "Node.js"`, | ||
'node_modules/my-lib/browser.js': `module.exports = "Browser"`, | ||
}, | ||
dependencies: {}, | ||
}) | ||
}) | ||
afterAll(() => next.destroy()) | ||
|
||
it('favors the browser export', async () => { | ||
const response = await fetchViaHTTP(next.url, '/') | ||
expect(Object.fromEntries(response.headers)).toMatchObject({ | ||
'x-imported': 'Browser', | ||
}) | ||
}) | ||
}) |