Commit c694e2a4 authored by Tim Neutkens's avatar Tim Neutkens

Create server.js that runs the code directly as part of the application to…

Create server.js that runs the code directly as part of the application to create separate child process to monitor
parent f300d8ae
...@@ -12,7 +12,8 @@ import type { ...@@ -12,7 +12,8 @@ import type {
UpdateInfo, UpdateInfo,
} from 'next/dist/build/swc/types' } from 'next/dist/build/swc/types'
import loadConfig from 'next/dist/server/config' import loadConfig from 'next/dist/server/config'
import path from 'path' import path, { join } from 'path'
import { spawnSync } from 'child_process'
function normalizePath(path: string) { function normalizePath(path: string) {
return path return path
...@@ -148,102 +149,118 @@ export default () => <div>${text}<Client /></div>;` ...@@ -148,102 +149,118 @@ export default () => <div>${text}<Client /></div>;`
} }
describe('next.rs api writeToDisk multiple times', () => { describe('next.rs api writeToDisk multiple times', () => {
let project: Project it('should allow to write to disk multiple times', async () => {
let next: NextInstance let next: NextInstance
beforeAll(async () => {
await trace('setup next instance').traceAsyncFn(async (rootSpan) => { next = await createNext({
next = await createNext({ skipStart: true,
skipStart: true, files: {
files: { 'pages/index.js': pagesIndexCode('hello world'),
'pages/index.js': pagesIndexCode('hello world'), 'lib/props.js': 'export default {}',
'lib/props.js': 'export default {}', 'pages/page-nodejs.js': 'export default () => <div>hello world</div>',
'pages/page-nodejs.js': 'export default () => <div>hello world</div>', 'pages/page-edge.js':
'pages/page-edge.js': 'export default () => <div>hello world</div>\nexport const config = { runtime: "experimental-edge" }',
'export default () => <div>hello world</div>\nexport const config = { runtime: "experimental-edge" }', 'pages/api/nodejs.js':
'pages/api/nodejs.js': 'export default () => Response.json({ hello: "world" })',
'export default () => Response.json({ hello: "world" })', 'pages/api/edge.js':
'pages/api/edge.js': 'export default () => Response.json({ hello: "world" })\nexport const config = { runtime: "edge" }',
'export default () => Response.json({ hello: "world" })\nexport const config = { runtime: "edge" }', 'app/layout.tsx':
'app/layout.tsx': 'export default function RootLayout({ children }: { children: any }) { return (<html><body>{children}</body></html>)}',
'export default function RootLayout({ children }: { children: any }) { return (<html><body>{children}</body></html>)}', 'app/loading.tsx':
'app/loading.tsx': 'export default function Loading() { return <>Loading</> }',
'export default function Loading() { return <>Loading</> }', 'app/app/page.tsx': appPageCode('hello world'),
'app/app/page.tsx': appPageCode('hello world'), 'app/app/client.tsx':
'app/app/client.tsx': '"use client";\nexport default () => <div>hello world</div>',
'"use client";\nexport default () => <div>hello world</div>', 'app/app-edge/page.tsx':
'app/app-edge/page.tsx': 'export default () => <div>hello world</div>\nexport const runtime = "edge"',
'export default () => <div>hello world</div>\nexport const runtime = "edge"', 'app/app-nodejs/page.tsx':
'app/app-nodejs/page.tsx': 'export default () => <div>hello world</div>',
'export default () => <div>hello world</div>', 'app/route-nodejs/route.ts':
'app/route-nodejs/route.ts': 'export function GET() { return Response.json({ hello: "world" }) }',
'export function GET() { return Response.json({ hello: "world" }) }', 'app/route-edge/route.ts':
'app/route-edge/route.ts': 'export function GET() { return Response.json({ hello: "world" }) }\nexport const runtime = "edge"',
'export function GET() { return Response.json({ hello: "world" }) }\nexport const runtime = "edge"', 'server.js': `
}, const path = require('path')
}) const {PHASE_DEVELOPMENT_SERVER} = require('next/constants')
const nextConfig = await loadConfig( const loadConfig = require('next/dist/server/config')
PHASE_DEVELOPMENT_SERVER, const {loadBindings} = require('next/dist/build/swc')
next.testDir const {createDefineEnv} = require('next/dist/build/swc')
) async function main() {
const bindings = await loadBindings() const nextConfig = await loadConfig.default(PHASE_DEVELOPMENT_SERVER, __dirname)
const rootPath = process.env.NEXT_SKIP_ISOLATE const bindings = await loadBindings()
? path.resolve(__dirname, '../../..') const rootPath = __dirname
: next.testDir const distDir = '.next'
const distDir = '.next' project = await bindings.turbo.createProject({
project = await bindings.turbo.createProject({ env: {},
env: {}, nextConfig: nextConfig,
nextConfig: nextConfig, rootPath,
rootPath, projectPath: '.',
projectPath: path.relative(rootPath, next.testDir) || '.', distDir,
distDir, watch: {
watch: { enable: true,
enable: true, },
}, dev: true,
defineEnv: createDefineEnv({
projectPath: __dirname,
isTurbopack: true,
clientRouterFilters: undefined,
config: nextConfig,
dev: true, dev: true,
defineEnv: createDefineEnv({ distDir: path.join(rootPath, distDir),
projectPath: next.testDir, fetchCacheKeyPrefix: undefined,
isTurbopack: true, hasRewrites: false,
clientRouterFilters: undefined, middlewareMatchers: undefined,
config: nextConfig, rewrites: {
dev: true, beforeFiles: [],
distDir: path.join(rootPath, distDir), afterFiles: [],
fetchCacheKeyPrefix: undefined, fallback: [],
hasRewrites: false,
middlewareMatchers: undefined,
rewrites: {
beforeFiles: [],
afterFiles: [],
fallback: [],
},
}),
buildId: 'development',
encryptionKey: '12345',
previewProps: {
previewModeId: 'development',
previewModeEncryptionKey: '12345',
previewModeSigningKey: '12345',
}, },
browserslistQuery: 'last 2 versions', }),
noMangling: false, buildId: 'development',
currentNodeJsVersion: '18.0.0', encryptionKey: '12345',
}) previewProps: {
previewModeId: 'development',
previewModeEncryptionKey: '12345',
previewModeSigningKey: '12345',
},
browserslistQuery: 'last 2 versions',
noMangling: false,
currentNodeJsVersion: '18.0.0',
}) })
})
afterAll(() => next.destroy())
it('should allow to write to disk multiple times', async () => {
const entrypointsSubscription = project.entrypointsSubscribe() const entrypointsSubscription = project.entrypointsSubscribe()
const entrypoints: TurbopackResult<RawEntrypoints> = ( const entrypoints = (
await entrypointsSubscription.next() await entrypointsSubscription.next()
).value ).value
const route = entrypoints.routes.get('/app-nodejs') const route = entrypoints.routes.get('/app-nodejs')
if (route.type === 'app-page') { if (route.type === 'app-page') {
for (let i = 0; i < 100; i++) { for (let i = 0; i < Infinity; i++) {
console.log(i) console.log(i)
await route.pages[0].htmlEndpoint.writeToDisk() await route.pages[0].htmlEndpoint.writeToDisk()
} }
} }
};
main().then(() => {
process.exit(0);
})
.catch((err) => {
console.error(err);
process.exit(1);
})
`,
},
})
spawnSync('node', [join(next.testDir, 'server.js')], {
stdio: 'inherit',
env: {
...process.env,
},
})
await next.destroy()
}) })
}) })
......
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