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

tree shake correctly

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