From b19f0980ecd195fa43afb18022e521e885ff8b6b Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Wed, 6 Nov 2024 00:05:42 +0100 Subject: [PATCH] Fix: revert the bad node binary handling (#72356) backport #71723 --- packages/next/src/build/webpack-config.ts | 20 +++++------ .../loaders/next-server-binary-loader.ts | 14 -------- ...ernalize-node-binary-browser-error.test.ts | 35 +++++++++---------- .../externalize-node-binary.test.ts | 3 +- 4 files changed, 27 insertions(+), 45 deletions(-) delete mode 100644 packages/next/src/build/webpack/loaders/next-server-binary-loader.ts diff --git a/packages/next/src/build/webpack-config.ts b/packages/next/src/build/webpack-config.ts index 6e84ea45b5fba..6f72f2db528b5 100644 --- a/packages/next/src/build/webpack-config.ts +++ b/packages/next/src/build/webpack-config.ts @@ -1192,7 +1192,6 @@ export default async function getBaseWebpackConfig( 'next-metadata-route-loader', 'modularize-import-loader', 'next-barrel-loader', - 'next-server-binary-loader', 'next-error-browser-binary-loader', ].reduce((alias, loader) => { // using multiple aliases to replace `resolveLoader.modules` @@ -1278,17 +1277,14 @@ export default async function getBaseWebpackConfig( or: WEBPACK_LAYERS.GROUP.nonClientServerTarget, }, }, - { - test: /[\\/].*?\.node$/, - loader: isNodeServer - ? 'next-server-binary-loader' - : 'next-error-browser-binary-loader', - // On server side bundling, only apply to app router, do not apply to pages router; - // On client side or edge runtime bundling, always error. - ...(isNodeServer && { - issuerLayer: isWebpackAppLayer, - }), - }, + ...(isNodeServer + ? [] + : [ + { + test: /[\\/].*?\.node$/, + loader: 'next-error-browser-binary-loader', + }, + ]), ...(hasAppDir ? [ { diff --git a/packages/next/src/build/webpack/loaders/next-server-binary-loader.ts b/packages/next/src/build/webpack/loaders/next-server-binary-loader.ts deleted file mode 100644 index 6525734216728..0000000000000 --- a/packages/next/src/build/webpack/loaders/next-server-binary-loader.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { webpack } from 'next/dist/compiled/webpack/webpack' -import path from 'path' - -export default function nextErrorBrowserBinaryLoader( - this: webpack.LoaderContext -) { - let relativePath = path.relative(this.rootContext, this.resourcePath) - if (!relativePath.startsWith('.')) { - relativePath = './' + relativePath - } - return `module.exports = __non_webpack_require__(${JSON.stringify( - relativePath - )})` -} diff --git a/test/development/app-dir/externalize-node-binary-browser-error/externalize-node-binary-browser-error.test.ts b/test/development/app-dir/externalize-node-binary-browser-error/externalize-node-binary-browser-error.test.ts index 2b6ef114bb561..0b250bd443e23 100644 --- a/test/development/app-dir/externalize-node-binary-browser-error/externalize-node-binary-browser-error.test.ts +++ b/test/development/app-dir/externalize-node-binary-browser-error/externalize-node-binary-browser-error.test.ts @@ -4,26 +4,25 @@ import { getRedboxDescription, getRedboxSource, } from 'next-test-utils' -;(process.env.TURBOPACK ? describe.skip : describe)( - 'externalize-node-binary-browser-error', - () => { - const { next } = nextTestSetup({ - files: __dirname, - }) - it('should error when import node binary on browser side', async () => { - const browser = await next.browser('/') - await hasRedbox(browser) - const redbox = { - description: await getRedboxDescription(browser), - source: await getRedboxSource(browser), - } +// FIXME: er-enable when we have a better implementation of node binary resolving +describe.skip('externalize-node-binary-browser-error', () => { + const { next } = nextTestSetup({ + files: __dirname, + }) - expect(redbox.description).toBe('Failed to compile') - expect(redbox.source).toMatchInlineSnapshot(` + it('should error when import node binary on browser side', async () => { + const browser = await next.browser('/') + await hasRedbox(browser) + const redbox = { + description: await getRedboxDescription(browser), + source: await getRedboxSource(browser), + } + + expect(redbox.description).toBe('Failed to compile') + expect(redbox.source).toMatchInlineSnapshot(` "./node_modules/foo-browser-import-binary/binary.node Error: Node.js binary module ./node_modules/foo-browser-import-binary/binary.node is not supported in the browser. Please only use the module on server side" `) - }) - } -) + }) +}) diff --git a/test/e2e/app-dir/externalize-node-binary/externalize-node-binary.test.ts b/test/e2e/app-dir/externalize-node-binary/externalize-node-binary.test.ts index bf5090cb24ee7..07017d378bd4e 100644 --- a/test/e2e/app-dir/externalize-node-binary/externalize-node-binary.test.ts +++ b/test/e2e/app-dir/externalize-node-binary/externalize-node-binary.test.ts @@ -1,6 +1,7 @@ import { nextTestSetup } from 'e2e-utils' -describe('externalize-node-binary', () => { +// FIXME: er-enable when we have a better implementation of node binary resolving +describe.skip('externalize-node-binary', () => { const { next } = nextTestSetup({ files: __dirname, })