From 477e71e67fb43318f455e9990eb8ff9922a87ecd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Sat, 8 Oct 2022 01:20:01 +0200 Subject: [PATCH 1/9] fix(standalone): support `type: "module"` --- packages/next/build/utils.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index 4e51ce1ed75bd..b86270d10d0f6 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -1558,6 +1558,9 @@ export async function copyTracedFiles( middlewareManifest: MiddlewareManifest ) { const outputPath = path.join(distDir, 'standalone') + const packageJsonPath = path.join(distDir, '../package.json') + const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8')) + const moduleType = packageJson.type === 'module' const copiedFiles = new Set() await recursiveDelete(outputPath) @@ -1631,16 +1634,25 @@ export async function copyTracedFiles( const serverOutputPath = path.join( outputPath, path.relative(tracingRoot, dir), - 'server.js' + moduleType ? 'server.mjs' : 'server.js' ) await fs.writeFile( serverOutputPath, - ` -process.env.NODE_ENV = 'production' -process.chdir(__dirname) + `${ + moduleType + ? `import Server from 'next/dist/server/next-server.js' +import http from 'http' +import path from 'path' +import { fileURLToPath } from 'url' +const __dirname = fileURLToPath(new URL('.', import.meta.url)) +const NextServer = Server.default` + : ` const NextServer = require('next/dist/server/next-server').default const http = require('http') -const path = require('path') +const path = require('path')` + } +process.env.NODE_ENV = 'production' +process.chdir(__dirname) // Make sure commands gracefully respect termination signals (e.g. from Docker) // Allow the graceful termination to be manually configurable @@ -1680,9 +1692,11 @@ server.listen(currentPort, (err) => { }) handler = nextServer.getRequestHandler() - console.log("Listening on port", currentPort) -}) - ` + console.log( + 'started server on 0.0.0.0:' + currentPort, + 'url: http://localhost:' + currentPort + ) +})` ) } export function isReservedPage(page: string) { From 943ac7efd44a2dc7377dc8d23389335d38c4fdfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Sat, 8 Oct 2022 01:20:42 +0200 Subject: [PATCH 2/9] test(standalone): add `type: "module"` test --- .../standalone-mode/type-module/index.test.ts | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 test/production/standalone-mode/type-module/index.test.ts diff --git a/test/production/standalone-mode/type-module/index.test.ts b/test/production/standalone-mode/type-module/index.test.ts new file mode 100644 index 0000000000000..d2466e8e2be76 --- /dev/null +++ b/test/production/standalone-mode/type-module/index.test.ts @@ -0,0 +1,48 @@ +import { createNext } from 'e2e-utils' +import { NextInstance } from 'test/lib/next-modes/base' +import { join } from 'path' +import { fork } from 'child_process' +import fs from 'fs-extra' +import { fetchViaHTTP, waitFor } from 'next-test-utils' + +describe('type-module', () => { + let next: NextInstance + + beforeAll(async () => { + next = await createNext({ + files: { + 'pages/index.js': ` + export default function Page() { + return

hello world

+ } + `, + 'next.config.mjs': `export default ${JSON.stringify({ + output: 'standalone', + })}`, + }, + packageJson: { type: 'module' }, + }) + await next.stop() + }) + + afterAll(() => next.destroy()) + + it('should work', async () => { + const standalonePath = join(next.testDir, '.next/standalone') + const staticSrc = join(next.testDir, '.next/static') + + const staticDest = join(standalonePath, '.next/static') + + await fs.move(staticSrc, staticDest) + + const serverFile = join(standalonePath, 'server.mjs') + const childProcess = fork(serverFile) + try { + await waitFor(1000) + const res = await fetchViaHTTP(3000, '/') + expect(await res.text()).toContain('hello world') + } finally { + childProcess.kill() + } + }) +}) From 9398b80b54d39842bf835fe9b533c0ccdcd6e35a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Sat, 8 Oct 2022 01:21:16 +0200 Subject: [PATCH 3/9] chore: group `output: "standalone"` tests --- .../optimizecss}/index.test.ts | 0 .../required-server-files/.env | 0 .../required-server-files/.env.local | 0 .../required-server-files/.env.production | 0 .../required-server-files/data.txt | 0 .../required-server-files/lib/config.js | 0 .../required-server-files/middleware.js | 0 .../pages/[slug]/index.js | 0 .../pages/[slug]/social/[[...rest]].js | 0 .../required-server-files/pages/_app.js | 0 .../required-server-files/pages/api/env.js | 0 .../required-server-files/pages/api/error.js | 0 .../pages/api/optional/[[...rest]].js | 0 .../pages/auto-static.js | 0 .../pages/catch-all/[[...rest]].js | 0 .../pages/dynamic/[slug].js | 0 .../required-server-files/pages/errors/gip.js | 0 .../pages/errors/gsp/[post].js | 0 .../pages/errors/gssp.js | 0 .../pages/fallback-false/[slug].js | 0 .../pages/fallback/[slug].js | 0 .../pages/gsp-long-revalidate.js | 0 .../required-server-files/pages/gsp.js | 0 .../required-server-files/pages/gssp.js | 0 .../required-server-files/pages/index.js | 0 .../pages/optional-ssg/[[...rest]].js | 0 .../pages/optional-ssp/[[...rest]].js | 0 .../pages/route-resolving/[slug]/[project].js | 0 .../pages/route-resolving/import/[slug].js | 0 .../required-server-files-i18n.test.ts | 8 +++---- .../required-server-files.test.ts | 22 ++++++------------- .../response-cache}/app/next.config.js | 0 .../response-cache}/app/pages/blog/[slug].js | 0 .../response-cache}/app/pages/index.js | 0 .../response-cache}/app/pages/news.js | 0 .../response-cache}/index.test.ts | 0 36 files changed, 10 insertions(+), 20 deletions(-) rename test/production/{standalone-mode-and-optimizecss => standalone-mode/optimizecss}/index.test.ts (100%) rename test/production/{ => standalone-mode}/required-server-files/.env (100%) rename test/production/{ => standalone-mode}/required-server-files/.env.local (100%) rename test/production/{ => standalone-mode}/required-server-files/.env.production (100%) rename test/production/{ => standalone-mode}/required-server-files/data.txt (100%) rename test/production/{ => standalone-mode}/required-server-files/lib/config.js (100%) rename test/production/{ => standalone-mode}/required-server-files/middleware.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/[slug]/index.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/[slug]/social/[[...rest]].js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/_app.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/api/env.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/api/error.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/api/optional/[[...rest]].js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/auto-static.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/catch-all/[[...rest]].js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/dynamic/[slug].js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/errors/gip.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/errors/gsp/[post].js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/errors/gssp.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/fallback-false/[slug].js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/fallback/[slug].js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/gsp-long-revalidate.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/gsp.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/gssp.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/index.js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/optional-ssg/[[...rest]].js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/optional-ssp/[[...rest]].js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/route-resolving/[slug]/[project].js (100%) rename test/production/{ => standalone-mode}/required-server-files/pages/route-resolving/import/[slug].js (100%) rename test/production/{ => standalone-mode/required-server-files}/required-server-files-i18n.test.ts (98%) rename test/production/{ => standalone-mode/required-server-files}/required-server-files.test.ts (98%) rename test/production/{minimal-mode-response-cache => standalone-mode/response-cache}/app/next.config.js (100%) rename test/production/{minimal-mode-response-cache => standalone-mode/response-cache}/app/pages/blog/[slug].js (100%) rename test/production/{minimal-mode-response-cache => standalone-mode/response-cache}/app/pages/index.js (100%) rename test/production/{minimal-mode-response-cache => standalone-mode/response-cache}/app/pages/news.js (100%) rename test/production/{minimal-mode-response-cache => standalone-mode/response-cache}/index.test.ts (100%) diff --git a/test/production/standalone-mode-and-optimizecss/index.test.ts b/test/production/standalone-mode/optimizecss/index.test.ts similarity index 100% rename from test/production/standalone-mode-and-optimizecss/index.test.ts rename to test/production/standalone-mode/optimizecss/index.test.ts diff --git a/test/production/required-server-files/.env b/test/production/standalone-mode/required-server-files/.env similarity index 100% rename from test/production/required-server-files/.env rename to test/production/standalone-mode/required-server-files/.env diff --git a/test/production/required-server-files/.env.local b/test/production/standalone-mode/required-server-files/.env.local similarity index 100% rename from test/production/required-server-files/.env.local rename to test/production/standalone-mode/required-server-files/.env.local diff --git a/test/production/required-server-files/.env.production b/test/production/standalone-mode/required-server-files/.env.production similarity index 100% rename from test/production/required-server-files/.env.production rename to test/production/standalone-mode/required-server-files/.env.production diff --git a/test/production/required-server-files/data.txt b/test/production/standalone-mode/required-server-files/data.txt similarity index 100% rename from test/production/required-server-files/data.txt rename to test/production/standalone-mode/required-server-files/data.txt diff --git a/test/production/required-server-files/lib/config.js b/test/production/standalone-mode/required-server-files/lib/config.js similarity index 100% rename from test/production/required-server-files/lib/config.js rename to test/production/standalone-mode/required-server-files/lib/config.js diff --git a/test/production/required-server-files/middleware.js b/test/production/standalone-mode/required-server-files/middleware.js similarity index 100% rename from test/production/required-server-files/middleware.js rename to test/production/standalone-mode/required-server-files/middleware.js diff --git a/test/production/required-server-files/pages/[slug]/index.js b/test/production/standalone-mode/required-server-files/pages/[slug]/index.js similarity index 100% rename from test/production/required-server-files/pages/[slug]/index.js rename to test/production/standalone-mode/required-server-files/pages/[slug]/index.js diff --git a/test/production/required-server-files/pages/[slug]/social/[[...rest]].js b/test/production/standalone-mode/required-server-files/pages/[slug]/social/[[...rest]].js similarity index 100% rename from test/production/required-server-files/pages/[slug]/social/[[...rest]].js rename to test/production/standalone-mode/required-server-files/pages/[slug]/social/[[...rest]].js diff --git a/test/production/required-server-files/pages/_app.js b/test/production/standalone-mode/required-server-files/pages/_app.js similarity index 100% rename from test/production/required-server-files/pages/_app.js rename to test/production/standalone-mode/required-server-files/pages/_app.js diff --git a/test/production/required-server-files/pages/api/env.js b/test/production/standalone-mode/required-server-files/pages/api/env.js similarity index 100% rename from test/production/required-server-files/pages/api/env.js rename to test/production/standalone-mode/required-server-files/pages/api/env.js diff --git a/test/production/required-server-files/pages/api/error.js b/test/production/standalone-mode/required-server-files/pages/api/error.js similarity index 100% rename from test/production/required-server-files/pages/api/error.js rename to test/production/standalone-mode/required-server-files/pages/api/error.js diff --git a/test/production/required-server-files/pages/api/optional/[[...rest]].js b/test/production/standalone-mode/required-server-files/pages/api/optional/[[...rest]].js similarity index 100% rename from test/production/required-server-files/pages/api/optional/[[...rest]].js rename to test/production/standalone-mode/required-server-files/pages/api/optional/[[...rest]].js diff --git a/test/production/required-server-files/pages/auto-static.js b/test/production/standalone-mode/required-server-files/pages/auto-static.js similarity index 100% rename from test/production/required-server-files/pages/auto-static.js rename to test/production/standalone-mode/required-server-files/pages/auto-static.js diff --git a/test/production/required-server-files/pages/catch-all/[[...rest]].js b/test/production/standalone-mode/required-server-files/pages/catch-all/[[...rest]].js similarity index 100% rename from test/production/required-server-files/pages/catch-all/[[...rest]].js rename to test/production/standalone-mode/required-server-files/pages/catch-all/[[...rest]].js diff --git a/test/production/required-server-files/pages/dynamic/[slug].js b/test/production/standalone-mode/required-server-files/pages/dynamic/[slug].js similarity index 100% rename from test/production/required-server-files/pages/dynamic/[slug].js rename to test/production/standalone-mode/required-server-files/pages/dynamic/[slug].js diff --git a/test/production/required-server-files/pages/errors/gip.js b/test/production/standalone-mode/required-server-files/pages/errors/gip.js similarity index 100% rename from test/production/required-server-files/pages/errors/gip.js rename to test/production/standalone-mode/required-server-files/pages/errors/gip.js diff --git a/test/production/required-server-files/pages/errors/gsp/[post].js b/test/production/standalone-mode/required-server-files/pages/errors/gsp/[post].js similarity index 100% rename from test/production/required-server-files/pages/errors/gsp/[post].js rename to test/production/standalone-mode/required-server-files/pages/errors/gsp/[post].js diff --git a/test/production/required-server-files/pages/errors/gssp.js b/test/production/standalone-mode/required-server-files/pages/errors/gssp.js similarity index 100% rename from test/production/required-server-files/pages/errors/gssp.js rename to test/production/standalone-mode/required-server-files/pages/errors/gssp.js diff --git a/test/production/required-server-files/pages/fallback-false/[slug].js b/test/production/standalone-mode/required-server-files/pages/fallback-false/[slug].js similarity index 100% rename from test/production/required-server-files/pages/fallback-false/[slug].js rename to test/production/standalone-mode/required-server-files/pages/fallback-false/[slug].js diff --git a/test/production/required-server-files/pages/fallback/[slug].js b/test/production/standalone-mode/required-server-files/pages/fallback/[slug].js similarity index 100% rename from test/production/required-server-files/pages/fallback/[slug].js rename to test/production/standalone-mode/required-server-files/pages/fallback/[slug].js diff --git a/test/production/required-server-files/pages/gsp-long-revalidate.js b/test/production/standalone-mode/required-server-files/pages/gsp-long-revalidate.js similarity index 100% rename from test/production/required-server-files/pages/gsp-long-revalidate.js rename to test/production/standalone-mode/required-server-files/pages/gsp-long-revalidate.js diff --git a/test/production/required-server-files/pages/gsp.js b/test/production/standalone-mode/required-server-files/pages/gsp.js similarity index 100% rename from test/production/required-server-files/pages/gsp.js rename to test/production/standalone-mode/required-server-files/pages/gsp.js diff --git a/test/production/required-server-files/pages/gssp.js b/test/production/standalone-mode/required-server-files/pages/gssp.js similarity index 100% rename from test/production/required-server-files/pages/gssp.js rename to test/production/standalone-mode/required-server-files/pages/gssp.js diff --git a/test/production/required-server-files/pages/index.js b/test/production/standalone-mode/required-server-files/pages/index.js similarity index 100% rename from test/production/required-server-files/pages/index.js rename to test/production/standalone-mode/required-server-files/pages/index.js diff --git a/test/production/required-server-files/pages/optional-ssg/[[...rest]].js b/test/production/standalone-mode/required-server-files/pages/optional-ssg/[[...rest]].js similarity index 100% rename from test/production/required-server-files/pages/optional-ssg/[[...rest]].js rename to test/production/standalone-mode/required-server-files/pages/optional-ssg/[[...rest]].js diff --git a/test/production/required-server-files/pages/optional-ssp/[[...rest]].js b/test/production/standalone-mode/required-server-files/pages/optional-ssp/[[...rest]].js similarity index 100% rename from test/production/required-server-files/pages/optional-ssp/[[...rest]].js rename to test/production/standalone-mode/required-server-files/pages/optional-ssp/[[...rest]].js diff --git a/test/production/required-server-files/pages/route-resolving/[slug]/[project].js b/test/production/standalone-mode/required-server-files/pages/route-resolving/[slug]/[project].js similarity index 100% rename from test/production/required-server-files/pages/route-resolving/[slug]/[project].js rename to test/production/standalone-mode/required-server-files/pages/route-resolving/[slug]/[project].js diff --git a/test/production/required-server-files/pages/route-resolving/import/[slug].js b/test/production/standalone-mode/required-server-files/pages/route-resolving/import/[slug].js similarity index 100% rename from test/production/required-server-files/pages/route-resolving/import/[slug].js rename to test/production/standalone-mode/required-server-files/pages/route-resolving/import/[slug].js diff --git a/test/production/required-server-files-i18n.test.ts b/test/production/standalone-mode/required-server-files/required-server-files-i18n.test.ts similarity index 98% rename from test/production/required-server-files-i18n.test.ts rename to test/production/standalone-mode/required-server-files/required-server-files-i18n.test.ts index 8d290d779df53..26210e4539307 100644 --- a/test/production/required-server-files-i18n.test.ts +++ b/test/production/standalone-mode/required-server-files/required-server-files-i18n.test.ts @@ -41,11 +41,9 @@ describe('should set-up next', () => { next = await createNext({ files: { - pages: new FileRef(join(__dirname, 'required-server-files/pages')), - lib: new FileRef(join(__dirname, 'required-server-files/lib')), - 'data.txt': new FileRef( - join(__dirname, 'required-server-files/data.txt') - ), + pages: new FileRef(join(__dirname, 'pages')), + lib: new FileRef(join(__dirname, 'lib')), + 'data.txt': new FileRef(join(__dirname, 'data.txt')), }, packageJson: { scripts: { diff --git a/test/production/required-server-files.test.ts b/test/production/standalone-mode/required-server-files/required-server-files.test.ts similarity index 98% rename from test/production/required-server-files.test.ts rename to test/production/standalone-mode/required-server-files/required-server-files.test.ts index de7d1bcc26c32..0912f96dc3e39 100644 --- a/test/production/required-server-files.test.ts +++ b/test/production/standalone-mode/required-server-files/required-server-files.test.ts @@ -28,21 +28,13 @@ describe('should set-up next', () => { next = await createNext({ files: { - pages: new FileRef(join(__dirname, 'required-server-files/pages')), - lib: new FileRef(join(__dirname, 'required-server-files/lib')), - 'middleware.js': new FileRef( - join(__dirname, 'required-server-files/middleware.js') - ), - 'data.txt': new FileRef( - join(__dirname, 'required-server-files/data.txt') - ), - '.env': new FileRef(join(__dirname, 'required-server-files/.env')), - '.env.local': new FileRef( - join(__dirname, 'required-server-files/.env.local') - ), - '.env.production': new FileRef( - join(__dirname, 'required-server-files/.env.production') - ), + pages: new FileRef(join(__dirname, 'pages')), + lib: new FileRef(join(__dirname, 'lib')), + 'middleware.js': new FileRef(join(__dirname, 'middleware.js')), + 'data.txt': new FileRef(join(__dirname, 'data.txt')), + '.env': new FileRef(join(__dirname, '.env')), + '.env.local': new FileRef(join(__dirname, '.env.local')), + '.env.production': new FileRef(join(__dirname, '.env.production')), }, nextConfig: { eslint: { diff --git a/test/production/minimal-mode-response-cache/app/next.config.js b/test/production/standalone-mode/response-cache/app/next.config.js similarity index 100% rename from test/production/minimal-mode-response-cache/app/next.config.js rename to test/production/standalone-mode/response-cache/app/next.config.js diff --git a/test/production/minimal-mode-response-cache/app/pages/blog/[slug].js b/test/production/standalone-mode/response-cache/app/pages/blog/[slug].js similarity index 100% rename from test/production/minimal-mode-response-cache/app/pages/blog/[slug].js rename to test/production/standalone-mode/response-cache/app/pages/blog/[slug].js diff --git a/test/production/minimal-mode-response-cache/app/pages/index.js b/test/production/standalone-mode/response-cache/app/pages/index.js similarity index 100% rename from test/production/minimal-mode-response-cache/app/pages/index.js rename to test/production/standalone-mode/response-cache/app/pages/index.js diff --git a/test/production/minimal-mode-response-cache/app/pages/news.js b/test/production/standalone-mode/response-cache/app/pages/news.js similarity index 100% rename from test/production/minimal-mode-response-cache/app/pages/news.js rename to test/production/standalone-mode/response-cache/app/pages/news.js diff --git a/test/production/minimal-mode-response-cache/index.test.ts b/test/production/standalone-mode/response-cache/index.test.ts similarity index 100% rename from test/production/minimal-mode-response-cache/index.test.ts rename to test/production/standalone-mode/response-cache/index.test.ts From 216068aa128c0958c513deeeff25c50bb1dc7f6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Sat, 8 Oct 2022 01:47:24 +0200 Subject: [PATCH 4/9] move --- .../required-server-files-i18n.test.ts | 8 ++++--- .../required-server-files.test.ts | 22 +++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) rename test/production/standalone-mode/{required-server-files => }/required-server-files-i18n.test.ts (98%) rename test/production/standalone-mode/{required-server-files => }/required-server-files.test.ts (98%) diff --git a/test/production/standalone-mode/required-server-files/required-server-files-i18n.test.ts b/test/production/standalone-mode/required-server-files-i18n.test.ts similarity index 98% rename from test/production/standalone-mode/required-server-files/required-server-files-i18n.test.ts rename to test/production/standalone-mode/required-server-files-i18n.test.ts index 26210e4539307..8d290d779df53 100644 --- a/test/production/standalone-mode/required-server-files/required-server-files-i18n.test.ts +++ b/test/production/standalone-mode/required-server-files-i18n.test.ts @@ -41,9 +41,11 @@ describe('should set-up next', () => { next = await createNext({ files: { - pages: new FileRef(join(__dirname, 'pages')), - lib: new FileRef(join(__dirname, 'lib')), - 'data.txt': new FileRef(join(__dirname, 'data.txt')), + pages: new FileRef(join(__dirname, 'required-server-files/pages')), + lib: new FileRef(join(__dirname, 'required-server-files/lib')), + 'data.txt': new FileRef( + join(__dirname, 'required-server-files/data.txt') + ), }, packageJson: { scripts: { diff --git a/test/production/standalone-mode/required-server-files/required-server-files.test.ts b/test/production/standalone-mode/required-server-files.test.ts similarity index 98% rename from test/production/standalone-mode/required-server-files/required-server-files.test.ts rename to test/production/standalone-mode/required-server-files.test.ts index 0912f96dc3e39..de7d1bcc26c32 100644 --- a/test/production/standalone-mode/required-server-files/required-server-files.test.ts +++ b/test/production/standalone-mode/required-server-files.test.ts @@ -28,13 +28,21 @@ describe('should set-up next', () => { next = await createNext({ files: { - pages: new FileRef(join(__dirname, 'pages')), - lib: new FileRef(join(__dirname, 'lib')), - 'middleware.js': new FileRef(join(__dirname, 'middleware.js')), - 'data.txt': new FileRef(join(__dirname, 'data.txt')), - '.env': new FileRef(join(__dirname, '.env')), - '.env.local': new FileRef(join(__dirname, '.env.local')), - '.env.production': new FileRef(join(__dirname, '.env.production')), + pages: new FileRef(join(__dirname, 'required-server-files/pages')), + lib: new FileRef(join(__dirname, 'required-server-files/lib')), + 'middleware.js': new FileRef( + join(__dirname, 'required-server-files/middleware.js') + ), + 'data.txt': new FileRef( + join(__dirname, 'required-server-files/data.txt') + ), + '.env': new FileRef(join(__dirname, 'required-server-files/.env')), + '.env.local': new FileRef( + join(__dirname, 'required-server-files/.env.local') + ), + '.env.production': new FileRef( + join(__dirname, 'required-server-files/.env.production') + ), }, nextConfig: { eslint: { From e514e6d09e2635bc28bedf671272bf333e215c19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Sat, 8 Oct 2022 02:45:55 +0200 Subject: [PATCH 5/9] move --- .../production/pnpm-support/{test => }/index.test.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) rename test/production/pnpm-support/{test => }/index.test.ts (88%) diff --git a/test/production/pnpm-support/test/index.test.ts b/test/production/pnpm-support/index.test.ts similarity index 88% rename from test/production/pnpm-support/test/index.test.ts rename to test/production/pnpm-support/index.test.ts index c18b8de7b3d1f..e0328f5001bac 100644 --- a/test/production/pnpm-support/test/index.test.ts +++ b/test/production/pnpm-support/index.test.ts @@ -23,9 +23,9 @@ describe('pnpm support', () => { it('should build with dependencies installed via pnpm', async () => { next = await createNext({ files: { - pages: new FileRef(path.join(__dirname, '..', 'app/pages')), + pages: new FileRef(path.join(__dirname, 'app/pages')), 'next.config.js': new FileRef( - path.join(__dirname, '..', 'app/next.config.js') + path.join(__dirname, 'app/next.config.js') ), }, packageJson: { @@ -48,12 +48,10 @@ describe('pnpm support', () => { it('should execute client-side JS on each page in output: "standalone"', async () => { next = await createNext({ files: { - pages: new FileRef(path.join(__dirname, '..', 'app-multi-page/pages')), - '.npmrc': new FileRef( - path.join(__dirname, '..', 'app-multi-page/.npmrc') - ), + pages: new FileRef(path.join(__dirname, 'app-multi-page/pages')), + '.npmrc': new FileRef(path.join(__dirname, 'app-multi-page/.npmrc')), 'next.config.js': new FileRef( - path.join(__dirname, '..', 'app-multi-page/next.config.js') + path.join(__dirname, 'app-multi-page/next.config.js') ), }, packageJson: { From 249518497e3e2920a2af2f2a24e3a2c66ebf661a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Sat, 8 Oct 2022 02:46:53 +0200 Subject: [PATCH 6/9] change log message --- packages/next/build/utils.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index b86270d10d0f6..4a2b9a925717a 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -1693,7 +1693,8 @@ server.listen(currentPort, (err) => { handler = nextServer.getRequestHandler() console.log( - 'started server on 0.0.0.0:' + currentPort, + 'Listening on port', + currentPort, 'url: http://localhost:' + currentPort ) })` From bae6c843008f6c25375ae9d6a4681a3e52dbd8d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Sat, 8 Oct 2022 02:48:27 +0200 Subject: [PATCH 7/9] move --- .../required-server-files-i18n.test.ts | 8 +++--- .../required-server-files.test.ts | 22 +++++---------- .../standalone-mode/type-module/index.test.ts | 27 ++++++++++++------- 3 files changed, 27 insertions(+), 30 deletions(-) rename test/production/standalone-mode/{ => required-server-files}/required-server-files-i18n.test.ts (98%) rename test/production/standalone-mode/{ => required-server-files}/required-server-files.test.ts (98%) diff --git a/test/production/standalone-mode/required-server-files-i18n.test.ts b/test/production/standalone-mode/required-server-files/required-server-files-i18n.test.ts similarity index 98% rename from test/production/standalone-mode/required-server-files-i18n.test.ts rename to test/production/standalone-mode/required-server-files/required-server-files-i18n.test.ts index 8d290d779df53..26210e4539307 100644 --- a/test/production/standalone-mode/required-server-files-i18n.test.ts +++ b/test/production/standalone-mode/required-server-files/required-server-files-i18n.test.ts @@ -41,11 +41,9 @@ describe('should set-up next', () => { next = await createNext({ files: { - pages: new FileRef(join(__dirname, 'required-server-files/pages')), - lib: new FileRef(join(__dirname, 'required-server-files/lib')), - 'data.txt': new FileRef( - join(__dirname, 'required-server-files/data.txt') - ), + pages: new FileRef(join(__dirname, 'pages')), + lib: new FileRef(join(__dirname, 'lib')), + 'data.txt': new FileRef(join(__dirname, 'data.txt')), }, packageJson: { scripts: { diff --git a/test/production/standalone-mode/required-server-files.test.ts b/test/production/standalone-mode/required-server-files/required-server-files.test.ts similarity index 98% rename from test/production/standalone-mode/required-server-files.test.ts rename to test/production/standalone-mode/required-server-files/required-server-files.test.ts index de7d1bcc26c32..0912f96dc3e39 100644 --- a/test/production/standalone-mode/required-server-files.test.ts +++ b/test/production/standalone-mode/required-server-files/required-server-files.test.ts @@ -28,21 +28,13 @@ describe('should set-up next', () => { next = await createNext({ files: { - pages: new FileRef(join(__dirname, 'required-server-files/pages')), - lib: new FileRef(join(__dirname, 'required-server-files/lib')), - 'middleware.js': new FileRef( - join(__dirname, 'required-server-files/middleware.js') - ), - 'data.txt': new FileRef( - join(__dirname, 'required-server-files/data.txt') - ), - '.env': new FileRef(join(__dirname, 'required-server-files/.env')), - '.env.local': new FileRef( - join(__dirname, 'required-server-files/.env.local') - ), - '.env.production': new FileRef( - join(__dirname, 'required-server-files/.env.production') - ), + pages: new FileRef(join(__dirname, 'pages')), + lib: new FileRef(join(__dirname, 'lib')), + 'middleware.js': new FileRef(join(__dirname, 'middleware.js')), + 'data.txt': new FileRef(join(__dirname, 'data.txt')), + '.env': new FileRef(join(__dirname, '.env')), + '.env.local': new FileRef(join(__dirname, '.env.local')), + '.env.production': new FileRef(join(__dirname, '.env.production')), }, nextConfig: { eslint: { diff --git a/test/production/standalone-mode/type-module/index.test.ts b/test/production/standalone-mode/type-module/index.test.ts index d2466e8e2be76..5c137868df689 100644 --- a/test/production/standalone-mode/type-module/index.test.ts +++ b/test/production/standalone-mode/type-module/index.test.ts @@ -1,9 +1,13 @@ import { createNext } from 'e2e-utils' import { NextInstance } from 'test/lib/next-modes/base' import { join } from 'path' -import { fork } from 'child_process' import fs from 'fs-extra' -import { fetchViaHTTP, waitFor } from 'next-test-utils' +import { + fetchViaHTTP, + findPort, + initNextServerScript, + killApp, +} from 'next-test-utils' describe('type-module', () => { let next: NextInstance @@ -36,13 +40,16 @@ describe('type-module', () => { await fs.move(staticSrc, staticDest) const serverFile = join(standalonePath, 'server.mjs') - const childProcess = fork(serverFile) - try { - await waitFor(1000) - const res = await fetchViaHTTP(3000, '/') - expect(await res.text()).toContain('hello world') - } finally { - childProcess.kill() - } + const appPort = await findPort() + const server = await initNextServerScript( + serverFile, + /Listening on/, + { ...process.env, PORT: appPort }, + undefined, + { cwd: next.testDir } + ) + const res = await fetchViaHTTP(appPort, '/') + expect(await res.text()).toContain('hello world') + await killApp(server) }) }) From 8fd1d917a6a70df41cc025dcd3aed30cd64301bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Tue, 11 Oct 2022 15:55:17 +0200 Subject: [PATCH 8/9] fall back to CJS if no `package.json` found --- packages/next/build/utils.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index 4a2b9a925717a..72c34e9e15efa 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -1558,9 +1558,12 @@ export async function copyTracedFiles( middlewareManifest: MiddlewareManifest ) { const outputPath = path.join(distDir, 'standalone') - const packageJsonPath = path.join(distDir, '../package.json') - const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8')) - const moduleType = packageJson.type === 'module' + let moduleType = false + try { + const packageJsonPath = path.join(distDir, '../package.json') + const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8')) + moduleType = packageJson.type === 'module' + } catch {} const copiedFiles = new Set() await recursiveDelete(outputPath) From e68278bd02b4beac0037330d7551f2f3eb6d7799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Tue, 11 Oct 2022 16:32:07 +0200 Subject: [PATCH 9/9] always use `server.js` since copy `package.json` --- packages/next/build/utils.ts | 2 +- test/production/standalone-mode/type-module/index.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/next/build/utils.ts b/packages/next/build/utils.ts index 72c34e9e15efa..9114cef2e2e0f 100644 --- a/packages/next/build/utils.ts +++ b/packages/next/build/utils.ts @@ -1637,7 +1637,7 @@ export async function copyTracedFiles( const serverOutputPath = path.join( outputPath, path.relative(tracingRoot, dir), - moduleType ? 'server.mjs' : 'server.js' + 'server.js' ) await fs.writeFile( serverOutputPath, diff --git a/test/production/standalone-mode/type-module/index.test.ts b/test/production/standalone-mode/type-module/index.test.ts index 5c137868df689..51a34e6d0bdce 100644 --- a/test/production/standalone-mode/type-module/index.test.ts +++ b/test/production/standalone-mode/type-module/index.test.ts @@ -39,7 +39,7 @@ describe('type-module', () => { await fs.move(staticSrc, staticDest) - const serverFile = join(standalonePath, 'server.mjs') + const serverFile = join(standalonePath, 'server.js') const appPort = await findPort() const server = await initNextServerScript( serverFile,