Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"@eslint/js": "^10.0.1",
"@eslint/markdown": "^8.0.1",
"@faker-js/faker": "^10.4.0",
"@rollup/plugin-run": "^3.1.0",
"@types/cors": "^2.8.19",
"@types/express": "^5.0.6",
"@types/multer": "^2.1.0",
Expand Down
98 changes: 98 additions & 0 deletions packages/common/src/dev-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { fork, type ChildProcess, type ForkOptions } from 'node:child_process'
import { access, stat } from 'node:fs/promises'
import path from 'node:path'
import { nodeEnv } from './system'

interface OutputChunk {
facadeModuleId?: string | null;
isEntry?: boolean;
}

interface OutputOptions {
dir?: string;
file?: string;
}

export interface DevServerRunnerOptions extends ForkOptions {
input: string;
args?: readonly string[];
}

/**
* Run the emitted server entry after a watched build has completely settled.
*
* This runner does not replace the live process immediately in `writeBundle`.
* Tsdown can still finalize or replace its output after that hook, creating a
* short window where Node cannot resolve the entry.
*/
export const devServer = (options: DevServerRunnerOptions) => {
if (nodeEnv() !== 'dev' || process.env.CLI_BUILD === 'true') {
return { name: 'arkstack-dev-server-disabled' }
}

let child: ChildProcess | undefined
let restart = Promise.resolve()
const {
input: sourceInput,
args: runnerArgs = [],
...forkOptions
} = options
const input = path.resolve(sourceInput)
const args = [...runnerArgs]

const waitUntilStable = async (entry: string): Promise<void> => {
let previousSize = -1

for (let attempt = 0; attempt < 50; attempt++) {
try {
await access(entry)
const size = (await stat(entry)).size

if (size > 0 && size === previousSize) return
previousSize = size
} catch {
previousSize = -1
}

await new Promise(resolve => setTimeout(resolve, 10))
}

throw new Error(`The emitted server entry was not ready: ${entry}`)
}

const stopChild = async (): Promise<void> => {
if (!child || child.exitCode !== null) return

const exited = new Promise<void>(resolve => child?.once('exit', () => resolve()))
child.kill()
await exited
}

return {
name: 'arkstack-dev-server',
writeBundle(output: OutputOptions, bundle: Record<string, OutputChunk>) {
const fileName = Object.keys(bundle).find(name => {
const chunk = bundle[name]

return chunk?.isEntry && path.resolve(chunk.facadeModuleId ?? '') === input
})

if (!fileName) throw new Error('Could not find the emitted server entry')

const directory = output.dir ?? path.dirname(output.file ?? '')
const entry = path.join(directory, fileName)

restart = restart.then(async () => {
// Yield past `writeBundle` so tsdown can finish swapping its
// staged output into place before we inspect or execute it.
await new Promise(resolve => setTimeout(resolve, 0))
await waitUntilStable(entry)
await stopChild()
child = fork(entry, args, forkOptions)
})
},
async closeWatcher() {
await stopChild()
},
}
}
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from './utils/helpers'
export * from './EnvLoader'
export * from './ConfigLoader'
export * from './tls'
export * from './dev-server'
1 change: 0 additions & 1 deletion packages/console/src/commands/DevCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class DevCommand extends Command {

if (url) {
vars.TUNNEL_URL = url
console.log(`Traffic has been tunnelled to ${url}`)
}
}

Expand Down
32 changes: 0 additions & 32 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions templates/express-inertia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"@eslint/js": "^10.0.1",
"@eslint/markdown": "^8.0.1",
"@faker-js/faker": "^10.4.0",
"@rollup/plugin-run": "^3.1.0",
"@types/cors": "^2.8.19",
"@types/express": "^5.0.6",
"@types/node": "^25.6.2",
Expand All @@ -71,4 +70,4 @@
"unrun": "^0.3.0",
"vitest": "^4.1.5"
}
}
}
26 changes: 10 additions & 16 deletions templates/express-inertia/tsdown.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { nodeEnv, outputDir } from '@arkstack/common'
import { devServer, nodeEnv, outputDir } from '@arkstack/common'
import { readFileSync, writeFileSync } from 'node:fs'

import { Arkstack } from '@arkstack/contract'
import { defineConfig } from 'tsdown'
import path from 'node:path'
import run from '@rollup/plugin-run'

const env = nodeEnv()
const dist = path.relative(Arkstack.rootDir(), outputDir())
Expand All @@ -23,20 +22,15 @@ export default defineConfig([
skipNodeModulesBundle: true,
},
watch: env === 'dev' && process.env.CLI_BUILD !== 'true' ? ['.env', '.env.*', 'src', 'tsconfig.json'] : false,
plugins:
env === 'dev' && process.env.CLI_BUILD !== 'true'
? [
run({
env: Object.assign({}, process.env, {
NODE_ENV: env,
}),
execArgv: ['-r', 'source-map-support/register'],
allowRestarts: true,
input: path.join(Arkstack.rootDir(), 'src/server.ts'),
})
]
: [
],
plugins: [
devServer({
env: Object.assign({}, process.env, {
NODE_ENV: env,
}),
execArgv: ['-r', 'source-map-support/register'],
input: path.join(Arkstack.rootDir(), 'src/server.ts'),
})
],
outExtensions: () => {
return {
js: '.js',
Expand Down
1 change: 0 additions & 1 deletion templates/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@eslint/js": "^10.0.1",
"@eslint/markdown": "^8.0.1",
"@faker-js/faker": "^10.4.0",
"@rollup/plugin-run": "^3.1.0",
"@types/cors": "^2.8.19",
"@types/express": "^5.0.6",
"@types/node": "^25.6.2",
Expand Down
26 changes: 10 additions & 16 deletions templates/express/tsdown.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { nodeEnv, outputDir } from '@arkstack/common'
import { devServer, nodeEnv, outputDir } from '@arkstack/common'
import { readFileSync, writeFileSync } from 'node:fs'

import { Arkstack } from '@arkstack/contract'
import { defineConfig } from 'tsdown'
import path from 'node:path'
import run from '@rollup/plugin-run'

const env = nodeEnv()
const dist = path.relative(Arkstack.rootDir(), outputDir())
Expand All @@ -23,20 +22,15 @@ export default defineConfig([
skipNodeModulesBundle: true,
},
watch: env === 'dev' && process.env.CLI_BUILD !== 'true' ? ['.env', '.env.*', 'src', 'tsconfig.json'] : false,
plugins:
env === 'dev' && process.env.CLI_BUILD !== 'true'
? [
run({
env: Object.assign({}, process.env, {
NODE_ENV: env,
}),
execArgv: ['-r', 'source-map-support/register'],
allowRestarts: true,
input: path.join(Arkstack.rootDir(), 'src/server.ts'),
})
]
: [
],
plugins: [
devServer({
env: Object.assign({}, process.env, {
NODE_ENV: env,
}),
execArgv: ['-r', 'source-map-support/register'],
input: path.join(Arkstack.rootDir(), 'src/server.ts'),
})
],
outExtensions: () => {
return {
js: '.js',
Expand Down
1 change: 0 additions & 1 deletion templates/h3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@eslint/js": "^10.0.1",
"@eslint/markdown": "^8.0.1",
"@faker-js/faker": "^10.4.0",
"@rollup/plugin-run": "^3.1.0",
"@types/node": "^25.6.2",
"@types/pg": "^8.16.0",
"eslint": "^10.3.0",
Expand Down
25 changes: 10 additions & 15 deletions templates/h3/tsdown.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { nodeEnv, outputDir } from '@arkstack/common'
import { devServer, nodeEnv, outputDir } from '@arkstack/common'
import { readFileSync, writeFileSync } from 'node:fs'

import { Arkstack } from '@arkstack/contract'
import { defineConfig } from 'tsdown'
import path from 'node:path'
import run from '@rollup/plugin-run'

const env = nodeEnv()
const dist = path.relative(Arkstack.rootDir(), outputDir())
Expand All @@ -27,19 +26,15 @@ export default defineConfig([
skipNodeModulesBundle: true,
},
watch: env === 'dev' && process.env.CLI_BUILD !== 'true' ? ['.env', '.env.*', 'src', 'tsconfig.json'] : false,
plugins:
env === 'dev' && process.env.CLI_BUILD !== 'true'
? [
run({
env: Object.assign({}, process.env, {
NODE_ENV: env,
}),
execArgv: ['-r', 'source-map-support/register'],
allowRestarts: true,
input: path.join(Arkstack.rootDir(), 'src/server.ts'),
}),
]
: [],
plugins: [
devServer({
env: Object.assign({}, process.env, {
NODE_ENV: env,
}),
execArgv: ['-r', 'source-map-support/register'],
input: path.join(Arkstack.rootDir(), 'src/server.ts'),
}),
],
outExtensions: () => {
return {
js: '.js',
Expand Down
Loading