forked from vercel/next.js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(standalone): support
type: "module"
(vercel#41268)
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
1 parent
cbee60c
commit 52fb039
Showing
39 changed files
with
95 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |