Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 12 commits into from Oct 11, 2022
31 changes: 23 additions & 8 deletions packages/next/build/utils.ts
Expand Up @@ -1558,6 +1558,9 @@ export async function copyTracedFiles(
middlewareManifest: MiddlewareManifest
) {
const outputPath = path.join(distDir, 'standalone')
const packageJsonPath = path.join(distDir, '../package.json')
balazsorban44 marked this conversation as resolved.
Show resolved Hide resolved
const packageJson = JSON.parse(await fs.readFile(packageJsonPath, 'utf8'))
const moduleType = packageJson.type === 'module'
const copiedFiles = new Set()
await recursiveDelete(outputPath)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1680,9 +1692,12 @@ server.listen(currentPort, (err) => {
})
handler = nextServer.getRequestHandler()

console.log("Listening on port", currentPort)
})
`
console.log(
'Listening on port',
currentPort,
'url: http://localhost:' + currentPort
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept wanting to click on a link after server startup, so I added this similar to next start.

)
})`
)
}
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
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.mjs')
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)
})
})