forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the
node
condition to import resolution (vercel#50007)
This adds the `node` condition, so that importing a node module that uses `exports` with a `node` specified will prioritize that file. Eg, [uuid](https://github.com/uuidjs/uuid/blob/4de23a60/package.json#L25-L31)'s `package.json` will now resolve to the `./dist/esm-node/index.js` in our SSR builds, instead of the default `./dist/esm-browser/index.js` Fixes WEB-1051
- Loading branch information
1 parent
df85ad1
commit 1e86c8b
Showing
31 changed files
with
422 additions
and
4 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
11 changes: 11 additions & 0 deletions
11
...crates/next-dev-tests/tests/integration/next/import/conditions/input/app/app-edge/page.js
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,11 @@ | ||
import edgeThenNode from 'edge-then-node' | ||
import nodeThenEdge from 'node-then-edge' | ||
|
||
export const runtime = 'edge' | ||
|
||
export default function AppEdge() { | ||
return JSON.stringify({ | ||
edgeThenNode, | ||
nodeThenEdge, | ||
}) | ||
} |
11 changes: 11 additions & 0 deletions
11
...ates/next-dev-tests/tests/integration/next/import/conditions/input/app/app-nodejs/page.js
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,11 @@ | ||
import edgeThenNode from 'edge-then-node' | ||
import nodeThenEdge from 'node-then-edge' | ||
|
||
export const runtime = 'nodejs' | ||
|
||
export default function AppNodeJs() { | ||
return JSON.stringify({ | ||
edgeThenNode, | ||
nodeThenEdge, | ||
}) | ||
} |
7 changes: 7 additions & 0 deletions
7
...t-swc/crates/next-dev-tests/tests/integration/next/import/conditions/input/app/layout.tsx
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,7 @@ | ||
export default function RootLayout({ children }: { children: any }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
...-swc/crates/next-dev-tests/tests/integration/next/import/conditions/input/app/loading.tsx
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,3 @@ | ||
export default function Loading() { | ||
return <>Loading</> | ||
} |
9 changes: 9 additions & 0 deletions
9
...ext-swc/crates/next-dev-tests/tests/integration/next/import/conditions/input/app/page.tsx
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,9 @@ | ||
import Test from './test' | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<Test /> | ||
</div> | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
...tes/next-dev-tests/tests/integration/next/import/conditions/input/app/route-edge/route.js
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,11 @@ | ||
import edgeThenNode from 'edge-then-node' | ||
import nodeThenEdge from 'node-then-edge' | ||
|
||
export const runtime = 'edge' | ||
|
||
export function GET() { | ||
return Response.json({ | ||
edgeThenNode, | ||
nodeThenEdge, | ||
}) | ||
} |
11 changes: 11 additions & 0 deletions
11
...s/next-dev-tests/tests/integration/next/import/conditions/input/app/route-nodejs/route.js
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,11 @@ | ||
import edgeThenNode from 'edge-then-node' | ||
import nodeThenEdge from 'node-then-edge' | ||
|
||
export const runtime = 'nodejs' | ||
|
||
export function GET() { | ||
return Response.json({ | ||
edgeThenNode, | ||
nodeThenEdge, | ||
}) | ||
} |
103 changes: 103 additions & 0 deletions
103
...next-swc/crates/next-dev-tests/tests/integration/next/import/conditions/input/app/test.js
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,103 @@ | ||
'use client' | ||
|
||
import { useTestHarness } from '@turbo/pack-test-harness' | ||
|
||
export default function Test() { | ||
useTestHarness(runTests) | ||
} | ||
|
||
async function getJson(url) { | ||
const res = await fetch(url) | ||
const text = await res.text() | ||
const jsonText = /(\{[^}]*\})/.exec(text) | ||
return JSON.parse(jsonText[0].replace(/"/g, '"')) | ||
} | ||
|
||
function runTests() { | ||
it('page with nodejs runtime should import node conditions', async () => { | ||
const json = await getJson('/page-nodejs') | ||
expect(json).toMatchObject({ | ||
edgeThenNode: 'node', | ||
nodeThenEdge: 'node', | ||
}) | ||
}) | ||
|
||
it('page with edge runtime should import edge conditions', async () => { | ||
const json = await getJson('/page-edge') | ||
// TODO We don't currently support edge config in page rendering. | ||
// When we do, this needs to be updated. | ||
expect(json).not.toMatchObject({ | ||
edgeThenNode: 'edge', | ||
nodeThenEdge: 'edge', | ||
}) | ||
// TODO: delete this. | ||
expect(json).toMatchObject({ | ||
edgeThenNode: 'node', | ||
nodeThenEdge: 'node', | ||
}) | ||
}) | ||
|
||
it('page api with nodejs runtime should import node conditions', async () => { | ||
const json = await getJson('/api/api-nodejs') | ||
expect(json).toMatchObject({ | ||
edgeThenNode: 'node', | ||
nodeThenEdge: 'node', | ||
}) | ||
}) | ||
|
||
it('page api with edge runtime should import edge conditions', async () => { | ||
const json = await getJson('/api/api-edge') | ||
expect(json).toMatchObject({ | ||
edgeThenNode: 'edge', | ||
nodeThenEdge: 'edge', | ||
}) | ||
}) | ||
|
||
it('app with nodejs runtime should import node conditions', async () => { | ||
const json = await getJson('/app-nodejs') | ||
expect(json).toMatchObject({ | ||
edgeThenNode: 'node', | ||
nodeThenEdge: 'node', | ||
}) | ||
}) | ||
|
||
it('app with edge runtime should import edge conditions', async () => { | ||
const json = await getJson('/app-edge') | ||
// TODO We don't currently support edge config in app rendering. | ||
// When we do, this needs to be updated. | ||
expect(json).not.toMatchObject({ | ||
edgeThenNode: 'edge', | ||
nodeThenEdge: 'edge', | ||
}) | ||
// TODO: delete this. | ||
expect(json).toMatchObject({ | ||
edgeThenNode: 'node', | ||
nodeThenEdge: 'node', | ||
}) | ||
}) | ||
|
||
it('app route with nodejs runtime should import node conditions', async () => { | ||
const json = await getJson('/route-nodejs') | ||
expect(json).toMatchObject({ | ||
edgeThenNode: 'node', | ||
nodeThenEdge: 'node', | ||
}) | ||
}) | ||
|
||
it('app route with edge runtime should import edge conditions', async () => { | ||
const json = await getJson('/route-edge') | ||
expect(json).toMatchObject({ | ||
edgeThenNode: 'edge', | ||
nodeThenEdge: 'edge', | ||
}) | ||
}) | ||
|
||
it('middleware should import edge conditions', async () => { | ||
const res = await fetch('/middleware') | ||
const json = await res.json() | ||
expect(json).toMatchObject({ | ||
edgeThenNode: 'edge', | ||
nodeThenEdge: 'edge', | ||
}) | ||
}) | ||
} |
1 change: 1 addition & 0 deletions
1
...s/next-dev-tests/tests/integration/next/import/conditions/input/edge-then-node/default.js
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 @@ | ||
module.exports = 'default' |
1 change: 1 addition & 0 deletions
1
...ates/next-dev-tests/tests/integration/next/import/conditions/input/edge-then-node/edge.js
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 @@ | ||
module.exports = 'edge' |
1 change: 1 addition & 0 deletions
1
...ates/next-dev-tests/tests/integration/next/import/conditions/input/edge-then-node/main.js
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 @@ | ||
module.exports = 'main' |
1 change: 1 addition & 0 deletions
1
...ates/next-dev-tests/tests/integration/next/import/conditions/input/edge-then-node/node.js
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 @@ | ||
module.exports = 'node' |
10 changes: 10 additions & 0 deletions
10
...next-dev-tests/tests/integration/next/import/conditions/input/edge-then-node/package.json
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,10 @@ | ||
{ | ||
"main": "main.js", | ||
"exports": { | ||
".": { | ||
"edge-light": "./edge.js", | ||
"node": "./node.js", | ||
"default": "./default.js" | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...xt-swc/crates/next-dev-tests/tests/integration/next/import/conditions/input/middleware.js
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,13 @@ | ||
import edgeThenNode from 'edge-then-node' | ||
import nodeThenEdge from 'node-then-edge' | ||
|
||
export const config = { | ||
matcher: '/middleware', | ||
} | ||
|
||
export function middleware() { | ||
return Response.json({ | ||
edgeThenNode, | ||
nodeThenEdge, | ||
}) | ||
} |
1 change: 1 addition & 0 deletions
1
...s/next-dev-tests/tests/integration/next/import/conditions/input/node-then-edge/default.js
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 @@ | ||
module.exports = 'default' |
1 change: 1 addition & 0 deletions
1
...ates/next-dev-tests/tests/integration/next/import/conditions/input/node-then-edge/edge.js
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 @@ | ||
module.exports = 'edge' |
1 change: 1 addition & 0 deletions
1
...ates/next-dev-tests/tests/integration/next/import/conditions/input/node-then-edge/main.js
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 @@ | ||
module.exports = 'main' |
1 change: 1 addition & 0 deletions
1
...ates/next-dev-tests/tests/integration/next/import/conditions/input/node-then-edge/node.js
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 @@ | ||
module.exports = 'node' |
10 changes: 10 additions & 0 deletions
10
...next-dev-tests/tests/integration/next/import/conditions/input/node-then-edge/package.json
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,10 @@ | ||
{ | ||
"main": "main.js", | ||
"exports": { | ||
".": { | ||
"node": "./node.js", | ||
"edge-light": "./edge.js", | ||
"default": "./default.js" | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...next-dev-tests/tests/integration/next/import/conditions/input/node_modules/edge-then-node
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
...next-dev-tests/tests/integration/next/import/conditions/input/node_modules/node-then-edge
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...ext-swc/crates/next-dev-tests/tests/integration/next/import/conditions/input/package.json
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,6 @@ | ||
{ | ||
"dependencies": { | ||
"edge-then-node": "file:edge-then-node", | ||
"node-then-edge": "file:node-then-edge" | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...rates/next-dev-tests/tests/integration/next/import/conditions/input/pages/api/api-edge.js
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,14 @@ | ||
import edgeThenNode from 'edge-then-node' | ||
import nodeThenEdge from 'node-then-edge' | ||
|
||
export const config = { | ||
runtime: 'edge', | ||
} | ||
|
||
export default function ApiEdge() { | ||
return Response.json({ | ||
NEXT_RUNTIME: process.env.NEXT_RUNTIME, | ||
edgeThenNode, | ||
nodeThenEdge, | ||
}) | ||
} |
14 changes: 14 additions & 0 deletions
14
...tes/next-dev-tests/tests/integration/next/import/conditions/input/pages/api/api-nodejs.js
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,14 @@ | ||
import edgeThenNode from 'edge-then-node' | ||
import nodeThenEdge from 'node-then-edge' | ||
|
||
export const config = { | ||
runtime: 'nodejs', | ||
} | ||
|
||
export default function ApiNodeJs(req, res) { | ||
res.status(200).json({ | ||
NEXT_RUNTIME: process.env.NEXT_RUNTIME, | ||
edgeThenNode, | ||
nodeThenEdge, | ||
}) | ||
} |
13 changes: 13 additions & 0 deletions
13
...c/crates/next-dev-tests/tests/integration/next/import/conditions/input/pages/page-edge.js
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,13 @@ | ||
import edgeThenNode from 'edge-then-node' | ||
import nodeThenEdge from 'node-then-edge' | ||
|
||
export const config = { | ||
runtime: 'experimental-edge', | ||
} | ||
|
||
export default function PageEdge() { | ||
return JSON.stringify({ | ||
edgeThenNode, | ||
nodeThenEdge, | ||
}) | ||
} |
13 changes: 13 additions & 0 deletions
13
...crates/next-dev-tests/tests/integration/next/import/conditions/input/pages/page-nodejs.js
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,13 @@ | ||
import edgeThenNode from 'edge-then-node' | ||
import nodeThenEdge from 'node-then-edge' | ||
|
||
export const config = { | ||
runtime: 'nodejs', | ||
} | ||
|
||
export default function PageNodeJs() { | ||
return JSON.stringify({ | ||
edgeThenNode, | ||
nodeThenEdge, | ||
}) | ||
} |
35 changes: 35 additions & 0 deletions
35
...xt/import/conditions/issues/error TP1003 require.resolve(__q____q____q____star-76ea5d.txt
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,35 @@ | ||
PlainIssue { | ||
severity: Warning, | ||
context: "[project]/packages/next/dist/compiled/edge-runtime/index.js", | ||
category: "parse", | ||
title: "error TP1003 require.resolve(???*0*, {\"paths\": [???*1*]}) is not statically analyse-able", | ||
description: "- *0* arguments[1]\n ⚠\u{fe0f} function calls are not analysed yet\n- *1* ???*2*[\"dirname\"](a)\n ⚠\u{fe0f} unknown callee object\n- *2* ???(17)\n ⚠\u{fe0f} unknown callee", | ||
detail: "", | ||
documentation_link: "", | ||
source: Some( | ||
PlainIssueSource { | ||
asset: PlainAsset { | ||
ident: "[project]/packages/next/dist/compiled/edge-runtime/index.js", | ||
}, | ||
start: SourcePos { | ||
line: 0, | ||
column: 6730, | ||
}, | ||
end: SourcePos { | ||
line: 0, | ||
column: 6730, | ||
}, | ||
}, | ||
), | ||
sub_issues: [], | ||
processing_path: Some( | ||
[ | ||
PlainIssueProcessingPathItem { | ||
context: Some( | ||
"[project]/packages/next-swc/crates/next-dev-tests/tests/temp/next/import/conditions/input/app", | ||
), | ||
description: "Next.js App Route /route-nodejs", | ||
}, | ||
], | ||
), | ||
} |
Oops, something went wrong.