Skip to content

Commit

Permalink
fix(standalone): support type: "module" (vercel#41268)
Browse files Browse the repository at this point in the history
Fixes vercel#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)
  • Loading branch information
balazsorban44 authored and Kikobeats committed Oct 24, 2022
1 parent cbee60c commit 52fb039
Show file tree
Hide file tree
Showing 39 changed files with 95 additions and 34 deletions.
32 changes: 25 additions & 7 deletions packages/next/build/utils.ts
Expand Up @@ -1558,6 +1558,12 @@ export async function copyTracedFiles(
middlewareManifest: MiddlewareManifest
) {
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()
await recursiveDelete(outputPath)

Expand Down Expand Up @@ -1635,12 +1641,21 @@ export async function copyTracedFiles(
)
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
Expand Down Expand Up @@ -1680,9 +1695,12 @@ server.listen(currentPort, (err) => {
})
handler = nextServer.getRequestHandler()
console.log("Listening on port", currentPort)
})
`
console.log(
'Listening on port',
currentPort,
'url: http://localhost:' + currentPort
)
})`
)
}
export function isReservedPage(page: string) {
Expand Down
Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down
File renamed without changes.
Expand Up @@ -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: {
Expand Down
Expand Up @@ -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: {
Expand Down
55 changes: 55 additions & 0 deletions test/production/standalone-mode/type-module/index.test.ts
@@ -0,0 +1,55 @@
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)
})
})

0 comments on commit 52fb039

Please sign in to comment.