Unverified Commit 701e8d2c authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

fix(standalone): support `type: "module"` (#41268)

Fixes #41258

When we detect `type: "module"` in `package.json`:

~1. generate `server.mjs`~ Not necessary when `type: "module"` is set.
2. use `import` instead of `require`
3. replace `__dirname` with [ESM compatible
alternative](https://blog.logrocket.com/alternatives-dirname-node-js-es-modules/)

In this PR, I also moved some tests (those using `output: "standalone"`)
to a new `test/production/standalone-mode` directory for easier
discoverability in the future.

Run the related test with `pnpm test
test/production/standalone-mode/type-module/index.test.ts`

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 93bdaf10
...@@ -1558,6 +1558,12 @@ export async function copyTracedFiles( ...@@ -1558,6 +1558,12 @@ export async function copyTracedFiles(
middlewareManifest: MiddlewareManifest middlewareManifest: MiddlewareManifest
) { ) {
const outputPath = path.join(distDir, 'standalone') const outputPath = path.join(distDir, 'standalone')
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() const copiedFiles = new Set()
await recursiveDelete(outputPath) await recursiveDelete(outputPath)
...@@ -1635,12 +1641,21 @@ export async function copyTracedFiles( ...@@ -1635,12 +1641,21 @@ export async function copyTracedFiles(
) )
await fs.writeFile( await fs.writeFile(
serverOutputPath, serverOutputPath,
` `${
process.env.NODE_ENV = 'production' moduleType
process.chdir(__dirname) ? `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 NextServer = require('next/dist/server/next-server').default
const http = require('http') 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) // Make sure commands gracefully respect termination signals (e.g. from Docker)
// Allow the graceful termination to be manually configurable // Allow the graceful termination to be manually configurable
...@@ -1680,9 +1695,12 @@ server.listen(currentPort, (err) => { ...@@ -1680,9 +1695,12 @@ server.listen(currentPort, (err) => {
}) })
handler = nextServer.getRequestHandler() handler = nextServer.getRequestHandler()
console.log("Listening on port", currentPort) console.log(
}) 'Listening on port',
` currentPort,
'url: http://localhost:' + currentPort
)
})`
) )
} }
export function isReservedPage(page: string) { export function isReservedPage(page: string) {
......
...@@ -23,9 +23,9 @@ describe('pnpm support', () => { ...@@ -23,9 +23,9 @@ describe('pnpm support', () => {
it('should build with dependencies installed via pnpm', async () => { it('should build with dependencies installed via pnpm', async () => {
next = await createNext({ next = await createNext({
files: { files: {
pages: new FileRef(path.join(__dirname, '..', 'app/pages')), pages: new FileRef(path.join(__dirname, 'app/pages')),
'next.config.js': new FileRef( 'next.config.js': new FileRef(
path.join(__dirname, '..', 'app/next.config.js') path.join(__dirname, 'app/next.config.js')
), ),
}, },
packageJson: { packageJson: {
...@@ -48,12 +48,10 @@ describe('pnpm support', () => { ...@@ -48,12 +48,10 @@ describe('pnpm support', () => {
it('should execute client-side JS on each page in output: "standalone"', async () => { it('should execute client-side JS on each page in output: "standalone"', async () => {
next = await createNext({ next = await createNext({
files: { files: {
pages: new FileRef(path.join(__dirname, '..', 'app-multi-page/pages')), pages: new FileRef(path.join(__dirname, 'app-multi-page/pages')),
'.npmrc': new FileRef( '.npmrc': new FileRef(path.join(__dirname, 'app-multi-page/.npmrc')),
path.join(__dirname, '..', 'app-multi-page/.npmrc')
),
'next.config.js': new FileRef( 'next.config.js': new FileRef(
path.join(__dirname, '..', 'app-multi-page/next.config.js') path.join(__dirname, 'app-multi-page/next.config.js')
), ),
}, },
packageJson: { packageJson: {
......
...@@ -41,11 +41,9 @@ describe('should set-up next', () => { ...@@ -41,11 +41,9 @@ describe('should set-up next', () => {
next = await createNext({ next = await createNext({
files: { files: {
pages: new FileRef(join(__dirname, 'required-server-files/pages')), pages: new FileRef(join(__dirname, 'pages')),
lib: new FileRef(join(__dirname, 'required-server-files/lib')), lib: new FileRef(join(__dirname, 'lib')),
'data.txt': new FileRef( 'data.txt': new FileRef(join(__dirname, 'data.txt')),
join(__dirname, 'required-server-files/data.txt')
),
}, },
packageJson: { packageJson: {
scripts: { scripts: {
......
...@@ -28,21 +28,13 @@ describe('should set-up next', () => { ...@@ -28,21 +28,13 @@ describe('should set-up next', () => {
next = await createNext({ next = await createNext({
files: { files: {
pages: new FileRef(join(__dirname, 'required-server-files/pages')), pages: new FileRef(join(__dirname, 'pages')),
lib: new FileRef(join(__dirname, 'required-server-files/lib')), lib: new FileRef(join(__dirname, 'lib')),
'middleware.js': new FileRef( 'middleware.js': new FileRef(join(__dirname, 'middleware.js')),
join(__dirname, 'required-server-files/middleware.js') 'data.txt': new FileRef(join(__dirname, 'data.txt')),
), '.env': new FileRef(join(__dirname, '.env')),
'data.txt': new FileRef( '.env.local': new FileRef(join(__dirname, '.env.local')),
join(__dirname, 'required-server-files/data.txt') '.env.production': new FileRef(join(__dirname, '.env.production')),
),
'.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: { nextConfig: {
eslint: { eslint: {
......
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { join } from 'path'
import fs from 'fs-extra'
import {
fetchViaHTTP,
findPort,
initNextServerScript,
killApp,
} from 'next-test-utils'
describe('type-module', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
'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.js')
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)
})
})
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