Commit 164ccd1c authored by Jimmy Lai's avatar Jimmy Lai

tree shake correctly

parent 22c23392
...@@ -1055,5 +1055,6 @@ ...@@ -1055,5 +1055,6 @@
"1054": "processNodePrelude is not supported in edge runtime", "1054": "processNodePrelude is not supported in edge runtime",
"1055": "createUnclosingNodeStream is not supported in edge runtime", "1055": "createUnclosingNodeStream is not supported in edge runtime",
"1056": "createClosingNodeStream is not supported in edge runtime", "1056": "createClosingNodeStream is not supported in edge runtime",
"1057": "Cannot tee a Node.js stream in the edge runtime" "1057": "Cannot tee a Node.js stream in the edge runtime",
"1058": "toNodeDebugChannel can only be used in Node.js environments, this is a bug in the Next.js codebase"
} }
...@@ -25,26 +25,33 @@ export type DebugChannelClient = { ...@@ -25,26 +25,33 @@ export type DebugChannelClient = {
export function toNodeDebugChannel( export function toNodeDebugChannel(
webDebugChannel: DebugChannelServer webDebugChannel: DebugChannelServer
): import('node:stream').Writable { ): import('node:stream').Writable {
const { Writable } = require('node:stream') as typeof import('node:stream') // Guard so webpack can DCE node:stream require for edge builds.
const writer = webDebugChannel.writable.getWriter() if (!process.env.__NEXT_USE_NODE_STREAMS) {
return new Writable({ throw new Error(
write( 'toNodeDebugChannel can only be used in Node.js environments, this is a bug in the Next.js codebase'
chunk: Buffer | Uint8Array, )
_encoding: string, } else {
callback: (error?: Error | null) => void const { Writable } = require('node:stream') as typeof import('node:stream')
) { const writer = webDebugChannel.writable.getWriter()
writer.write(chunk).then(() => callback(), callback) return new Writable({
}, write(
final(callback: (error?: Error | null) => void) { chunk: Buffer | Uint8Array,
writer.close().then(() => callback(), callback) _encoding: string,
}, callback: (error?: Error | null) => void
destroy(_err, callback) { ) {
writer.abort(_err).then( writer.write(chunk).then(() => callback(), callback)
() => callback(_err), },
() => callback(_err) final(callback: (error?: Error | null) => void) {
) writer.close().then(() => callback(), callback)
}, },
}) destroy(_err, callback) {
writer.abort(_err).then(
() => callback(_err),
() => callback(_err)
)
},
})
}
} }
export function createDebugChannel(): DebugChannelPair | undefined { export function createDebugChannel(): DebugChannelPair | undefined {
......
...@@ -156,7 +156,10 @@ export function renderToFlightPipeableStream( ...@@ -156,7 +156,10 @@ export function renderToFlightPipeableStream(
if (options?.debugChannel) { if (options?.debugChannel) {
const { toNodeDebugChannel } = const { toNodeDebugChannel } =
require('./debug-channel-server') as typeof import('./debug-channel-server') require('./debug-channel-server') as typeof import('./debug-channel-server')
options = { ...options, debugChannel: toNodeDebugChannel(options.debugChannel) } options = {
...options,
debugChannel: toNodeDebugChannel(options.debugChannel),
}
} }
const { pipe } = renderToPipeableStreamFn(model, webpackMap, options) const { pipe } = renderToPipeableStreamFn(model, webpackMap, options)
......
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