Skip to content

Commit

Permalink
Add the node condition to import resolution (#50007)
Browse files Browse the repository at this point in the history
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
jridgewell committed May 23, 2023
1 parent df85ad1 commit 1e86c8b
Show file tree
Hide file tree
Showing 31 changed files with 422 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub async fn get_server_resolve_options_context(
enable_node_externals: true,
enable_node_native_modules: true,
module: true,
custom_conditions: vec![mode.node_env().to_string()],
custom_conditions: vec![mode.node_env().to_string(), "node".to_string()],
import_map: Some(next_server_import_map),
plugins: vec![
external_cjs_modules_plugin.into(),
Expand All @@ -126,7 +126,11 @@ pub async fn get_server_resolve_options_context(
enable_node_externals: true,
enable_node_native_modules: true,
module: true,
custom_conditions: vec![mode.node_env().to_string()],
custom_conditions: vec![
mode.node_env().to_string(),
// TODO!
"node".to_string(),
],
import_map: Some(next_server_import_map),
plugins: vec![
server_component_externals_plugin.into(),
Expand All @@ -150,7 +154,12 @@ pub async fn get_server_resolve_options_context(
enable_node_externals: true,
enable_node_native_modules: true,
module: true,
custom_conditions: vec![mode.node_env().to_string(), "react-server".to_string()],
custom_conditions: vec![
mode.node_env().to_string(),
"react-server".to_string(),
// TODO
"node".to_string(),
],
import_map: Some(next_server_import_map),
plugins: vec![
server_component_externals_plugin.into(),
Expand All @@ -172,7 +181,7 @@ pub async fn get_server_resolve_options_context(
let resolve_options_context = ResolveOptionsContext {
enable_node_modules: Some(root_dir),
module: true,
custom_conditions: vec![mode.node_env().to_string()],
custom_conditions: vec![mode.node_env().to_string(), "node".to_string()],
import_map: Some(next_server_import_map),
plugins: vec![
server_component_externals_plugin.into(),
Expand Down
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,
})
}
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,
})
}
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>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Loading() {
return <>Loading</>
}
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>
)
}
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,
})
}
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,
})
}
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(/&quot;/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',
})
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'default'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'edge'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'main'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'node'
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"
}
}
}
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,
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'default'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'edge'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'main'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'node'
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"
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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"
}
}
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,
})
}
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,
})
}
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,
})
}
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,
})
}
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",
},
],
),
}

0 comments on commit 1e86c8b

Please sign in to comment.