Unverified Commit ff19e7ad authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Add built-in server-only and client-only (#44861)

parent 4b33bc34
......@@ -189,6 +189,7 @@
"chalk": "2.4.2",
"ci-info": "watson/ci-info#f43f6a1cefff47fb361c88cf4b943fdbcaafe540",
"cli-select": "1.1.2",
"client-only": "0.0.1",
"comment-json": "3.0.3",
"compression": "1.7.4",
"conf": "5.0.0",
......@@ -260,6 +261,7 @@
"schema-utils3": "npm:schema-utils@3.0.0",
"semver": "7.3.2",
"send": "0.17.1",
"server-only": "0.0.1",
"setimmediate": "1.0.5",
"shell-quote": "1.7.3",
"source-map": "0.6.1",
......
......@@ -34,9 +34,12 @@ export function loadRequireHook(aliases: [string, string][] = []) {
const defaultAliases = [
...aliases,
// Use `require.resolve` explicitly to make them statically analyzable
// styled-jsx needs to be resolved as the external dependency.
['styled-jsx', require.resolve('styled-jsx')],
['styled-jsx/style', require.resolve('styled-jsx/style')],
['styled-jsx/style', require.resolve('styled-jsx/style')],
['server-only', require.resolve('next/dist/compiled/server-only')],
['client-only', require.resolve('next/dist/compiled/client-only')],
] as [string, string][]
setRequireOverrides(defaultAliases)
......
throw new Error(
"This module cannot be imported from a Server Component module. " +
"It should only be used from a Client Component."
);
{
"name": "client-only",
"description": "This is a marker package to indicate that a module can only be used in Client Components.",
"keywords": [
"react"
],
"version": "0.0.1",
"homepage": "https://reactjs.org/",
"bugs": "https://github.com/facebook/react/issues",
"license": "MIT",
"files": ["index.js", "error.js"],
"main": "index.js",
"exports": {
".": {
"react-server": "./error.js",
"default": "./index.js"
}
}
}
throw new Error(
"This module cannot be imported from a Client Component module. " +
"It should only be used from a Server Component."
);
{
"name": "server-only",
"description": "This is a marker package to indicate that a module can only be used in Server Components.",
"keywords": [
"react"
],
"version": "0.0.1",
"homepage": "https://reactjs.org/",
"bugs": "https://github.com/facebook/react/issues",
"license": "MIT",
"files": ["index.js", "empty.js"],
"main": "index.js",
"exports": {
".": {
"react-server": "./empty.js",
"default": "./index.js"
}
}
}
......@@ -1572,6 +1572,16 @@ export async function ncc_react(task, opts) {
)
}
// eslint-disable-next-line camelcase
export async function ncc_rsc_poison_packages(task, opts) {
await task
.source(join(dirname(require.resolve('server-only')), '*'))
.target('src/compiled/server-only')
await task
.source(join(dirname(require.resolve('client-only')), '*'))
.target('src/compiled/client-only')
}
// eslint-disable-next-line camelcase
export async function ncc_react_server_dom_webpack(task, opts) {
// Use installed versions instead of bundled version
......
......@@ -572,6 +572,7 @@ importers:
chalk: 2.4.2
ci-info: watson/ci-info#f43f6a1cefff47fb361c88cf4b943fdbcaafe540
cli-select: 1.1.2
client-only: 0.0.1
comment-json: 3.0.3
compression: 1.7.4
conf: 5.0.0
......@@ -649,6 +650,7 @@ importers:
schema-utils3: npm:schema-utils@3.0.0
semver: 7.3.2
send: 0.17.1
server-only: 0.0.1
setimmediate: 1.0.5
shell-quote: 1.7.3
source-map: 0.6.1
......@@ -777,6 +779,7 @@ importers:
chalk: 2.4.2
ci-info: github.com/watson/ci-info/f43f6a1cefff47fb361c88cf4b943fdbcaafe540
cli-select: 1.1.2
client-only: 0.0.1
comment-json: 3.0.3
compression: 1.7.4
conf: 5.0.0
......@@ -843,11 +846,12 @@ importers:
react-is: 18.2.0
react-refresh: 0.12.0
regenerator-runtime: 0.13.4
sass-loader: 12.4.0_y7k4qxzyfvxio3t476zhrzlwf4
sass-loader: 12.4.0_webpack@5.74.0
schema-utils2: /schema-utils/2.7.1
schema-utils3: /schema-utils/3.0.0
semver: 7.3.2
send: 0.17.1
server-only: 0.0.1
setimmediate: 1.0.5
shell-quote: 1.7.3
source-map: 0.6.1
......@@ -8223,6 +8227,7 @@ packages:
dependencies:
delegates: 1.0.0
readable-stream: 3.6.0
dev: false
/are-we-there-yet/3.0.1:
resolution: {integrity: sha512-QZW4EDmGwlYur0Yyf/b2uGucHQMa8aFUP7eu9ddR73vvhFyt4V0Vl3QHPcTNJ8l6qYOBdxgXdnBXQrHilfRQBg==}
......@@ -8230,6 +8235,7 @@ packages:
dependencies:
delegates: 1.0.0
readable-stream: 3.6.0
dev: false
/arg/4.1.0:
resolution: {integrity: sha512-ZWc51jO3qegGkVh8Hwpv636EkbesNV5ZNQPCtRa+0qytRYPEs9IYT9qITY9buezqUH5uqyzlWLcufrzU2rffdg==}
......@@ -8454,6 +8460,7 @@ packages:
/async-foreach/0.1.3:
resolution: {integrity: sha512-VUeSMD8nEGBWaZK4lizI1sf3yEC7pnAQ/mrI7pC2fBz2s/tq5jWWEngTwaf0Gruu/OoXRGLGg1XFqpYBiGTYJA==}
dev: false
/async-retry/1.2.1:
resolution: {integrity: sha512-FadV8UDcyZDjzb6eV7MCJj0bfrNjwKw7/X0QHPFCbYP6T20FXgZCYXpJKlQC8RxEQP1E6Xs8pNHdh3bcrZAuAw==}
......@@ -9805,6 +9812,7 @@ packages:
/color-support/1.1.3:
resolution: {integrity: sha512-qiBjkpbMLO/HL68y+lh4q0/O1MZFj2RX6X/KmMa3+gJD3z+WwI1ZzDHysvqHGS3mP6mznPckpXmw1nI9cJjyRg==}
hasBin: true
dev: false
/color/3.1.3:
resolution: {integrity: sha512-xgXAcTHa2HeFCGLE9Xs/R82hujGtu9Jd9x4NW3T34+OMs7VoPsjwzRczKHvTAHeJwWFwX5j15+MgAppE8ztObQ==}
......@@ -12544,6 +12552,7 @@ packages:
requiresBuild: true
dependencies:
detect-libc: 1.0.3
dev: false
/figgy-pudding/3.5.1:
resolution: {integrity: sha512-vNKxJHTEKNThjfrdJwHc7brvM6eVevuO5nTj6ez8ZQ1qbXTvGthucRF7S4vf2cr71QVnT70V34v0S1DyQsti0w==}
......@@ -13011,6 +13020,7 @@ packages:
string-width: 4.2.3
strip-ansi: 6.0.1
wide-align: 1.1.5
dev: false
/gauge/4.0.4:
resolution: {integrity: sha512-f9m+BEN5jkg6a0fZjleidjN51VE1X+mPFQ2DJ0uv1V39oCLCbsGe6yjbBnp7eK7z/+GAon99a3nHuqbuuthyPg==}
......@@ -13024,12 +13034,14 @@ packages:
string-width: 4.2.3
strip-ansi: 6.0.1
wide-align: 1.1.5
dev: false
/gaze/1.1.3:
resolution: {integrity: sha512-BRdNm8hbWzFzWHERTrejLqwHDfS4GibPoq5wjTPIoJHoBtKGPg3xAFfxmM+9ztbXelxcf2hwQcaz1PtmFeue8g==}
engines: {node: '>= 4.0.0'}
dependencies:
globule: 1.3.4
dev: false
/generic-names/2.0.1:
resolution: {integrity: sha512-kPCHWa1m9wGG/OwQpeweTwM/PYiQLrUIxXbt/P4Nic3LbGjCP0YwrALHW1uNLKZ0LIMg+RF+XRlj2ekT9ZlZAQ==}
......@@ -13463,6 +13475,7 @@ packages:
glob: 7.1.7
lodash: 4.17.21
minimatch: 3.0.4
dev: false
/got/10.7.0:
resolution: {integrity: sha512-aWTDeNw9g+XqEZNcTjMMZSy7B7yE9toWOFYip7ofFTLleJhvZwUxxTxkTpKvF+p1SAA4VHmuEy7PiHTHyq8tJg==}
......@@ -16739,6 +16752,7 @@ packages:
transitivePeerDependencies:
- bluebird
- supports-color
dev: false
/make-iterator/1.0.1:
resolution: {integrity: sha512-pxiuXh0iVEq7VM7KMIhs5gxsfxCux2URptUQaXo4iZZJxBAzTPOLE2BumO5dbfVYq/hBJFBR/a1mFDmOx5AGmw==}
......@@ -16983,6 +16997,7 @@ packages:
trim-newlines: 3.0.0
type-fest: 0.18.1
yargs-parser: 20.2.4
dev: false
/merge-descriptors/1.0.1:
resolution: {integrity: sha512-cCi6g3/Zr1iqQi6ySbseM1Xvooa98N0w31jzUYrXPX2xqObmFGHJ0tQ5u74H3mVh7wLouTseZyYIq39g8cNp1w==}
......@@ -17641,6 +17656,7 @@ packages:
transitivePeerDependencies:
- bluebird
- supports-color
dev: false
/node-html-parser/5.3.3:
resolution: {integrity: sha512-ncg1033CaX9UexbyA7e1N0aAoAYRDiV8jkTvzEnfd1GDvzFdrsXLzR4p4ik8mwLgnaKP/jyUFWDy9q3jvRT2Jw==}
......@@ -17699,6 +17715,7 @@ packages:
transitivePeerDependencies:
- bluebird
- supports-color
dev: false
/node-version/1.1.3:
resolution: {integrity: sha512-rEwE51JWn0yN3Wl5BXeGn5d52OGbSXzWiiXRjAQeuyvcGKyvuSILW2rb3G7Xh+nexzLwhTpek6Ehxd6IjvHePg==}
......@@ -17897,6 +17914,7 @@ packages:
console-control-strings: 1.1.0
gauge: 3.0.2
set-blocking: 2.0.0
dev: false
/npmlog/6.0.2:
resolution: {integrity: sha512-/vBvz5Jfr9dT/aFWd0FIRf+T/Q2WBsLENygUaFUqstqsycmZAP/t5BvFJTK0viFmSUxiUKTUplWy5vt+rvKIxg==}
......@@ -17906,6 +17924,7 @@ packages:
console-control-strings: 1.1.0
gauge: 4.0.4
set-blocking: 2.0.0
dev: false
/nprogress/0.2.0:
resolution: {integrity: sha1-y480xTIT2JVyP8urkH6UIq28r7E=}
......@@ -21275,8 +21294,9 @@ packages:
lodash: 4.17.21
scss-tokenizer: 0.4.3
yargs: 17.5.1
dev: false
/sass-loader/12.4.0_y7k4qxzyfvxio3t476zhrzlwf4:
/sass-loader/12.4.0_webpack@5.74.0:
resolution: {integrity: sha512-7xN+8khDIzym1oL9XyS6zP6Ges+Bo2B2xbPrjdMHEYyV3AQYhd/wXeru++3ODHF0zMjYmVadblSKrPrjEkL8mg==}
engines: {node: '>= 12.13.0'}
peerDependencies:
......@@ -21292,11 +21312,8 @@ packages:
sass:
optional: true
dependencies:
fibers: 5.0.3
klona: 2.0.4
neo-async: 2.6.2
node-sass: 7.0.3
sass: 1.54.0
webpack: 5.74.0
dev: true
......@@ -21381,6 +21398,7 @@ packages:
dependencies:
js-base64: 2.5.1
source-map: 0.7.3
dev: false
/seedrandom/3.0.5:
resolution: {integrity: sha512-8OwmbklUNzwezjGInmZ+2clQmExQPvomqjL7LFqOYqtmuxRgQYqOD3mHaU+MvZn5FLUeVxVfQjwLZW/n/JFuqg==}
......@@ -21501,6 +21519,10 @@ packages:
- supports-color
dev: true
/server-only/0.0.1:
resolution: {integrity: sha512-qepMx2JxAa5jjfzxG79yPPq+8BuFToHd1hm7kI+Z4zAq1ftQiP7HcxMhDDItrbtwVeLg/cY2JnKnrcFkmiswNA==}
dev: true
/set-blocking/2.0.0:
resolution: {integrity: sha512-KiKBS8AnWGEyLzofFfmvKwpdPzqiy16LvQfK3yv/fVH7Bj13/wl3JSR1J+rfgRE9q7xUJK4qvgS8raSOeLUehw==}
......@@ -21944,6 +21966,7 @@ packages:
resolution: {integrity: sha512-j4emi03KXqJWcIeF8eIXkjMFN1Cmb8gUlDYGeBALLPo5qdyTfA9bOtl8m33lRoC+vFMkP3gl0WsDr6+gzxbbTA==}
dependencies:
readable-stream: 2.3.7
dev: false
/stream-browserify/3.0.0:
resolution: {integrity: sha512-H73RAHsVBapbim0tU2JwwOiXUj+fikfiaoYAKHF3VJfA0pe2BCzkhAHBlLG6REzE+2WNZcxOXjK7lkso+9euLA==}
......@@ -22905,6 +22928,7 @@ packages:
resolution: {integrity: sha512-m6s2OdQe5wgpFMC+pAJ+q9djG82O2jcHPOI6RNg1yy9rCYR+WD6Nbpl32fDpfC56nirdRy+opFa/Vk7HYhqaew==}
dependencies:
glob: 7.2.0
dev: false
/tsconfig-paths/3.14.1:
resolution: {integrity: sha512-fxDhWnFSLt3VuTwtvJt5fpwxBHg5AdKWMsgcPOOIilyjymcYVZoCQF8fvFRezCNfblEXmi+PcM1eYHeOAgXCOQ==}
......
'use client'
import ServerOnly from './server-only-lib'
export default function page() {
return <ServerOnly />
}
import 'server-only'
export default function ServerOnlyLib() {
return 'server-only-lib'
}
import 'client-only'
export default function ClientOnlyLib() {
return 'client-only-lib'
}
import ClientOnly from './client-only-lib'
export default function page() {
return <ClientOnly />
}
import { check } from 'next-test-utils'
import { check, getRedboxSource, hasRedbox } from 'next-test-utils'
import { createNextDescribe } from 'e2e-utils'
if (!(globalThis as any).isNextDev) {
......@@ -109,6 +109,30 @@ if (!(globalThis as any).isNextDev) {
`You’re importing a class component. It only works in a Client Component`
)
})
// TODO-APP: investigate why the error keeps triggering reloading the page
it.skip('should allow to use and handle rsc poisoning client-only', async () => {
const browser = await next.browser(
'/server-with-errors/client-only-in-server'
)
await hasRedbox(browser)
const text = await getRedboxSource(browser)
expect(text).toContain(
`You're importing a component that imports client-only. It only works in a Client Component but none of its parents are marked with "use client", so they're Server Components by default.`
)
})
it('should allow to use and handle rsc poisoning server-only', async () => {
const browser = await next.browser(
'/client-with-errors/server-only-in-client'
)
await hasRedbox(browser)
const text = await getRedboxSource(browser)
expect(text).toContain(
`You're importing a component that needs server-only. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.`
)
})
}
)
}
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