Skip to content

Commit

Permalink
Update server.ts to v2 best practices remix-run/remix#6919 (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
xHomu authored Sep 16, 2023
1 parent 993d4d9 commit 4626d54
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import { wrapExpressCreateRequestHandler } from '@sentry/remix'
import address from 'address'
import chalk from 'chalk'
import chokidar from 'chokidar'
import closeWithGrace from 'close-with-grace'
import compression from 'compression'
import express from 'express'
Expand All @@ -22,10 +21,6 @@ import getPort, { portNumbers } from 'get-port'
import helmet from 'helmet'
import morgan from 'morgan'

// @ts-ignore - this file may not exist if you haven't built yet, but it will
// definitely exist by the time the dev or prod server actually runs.
import * as remixBuild from '#build/index.js'

installGlobals()

const MODE = process.env.NODE_ENV
Expand All @@ -35,8 +30,13 @@ const createRequestHandler = wrapExpressCreateRequestHandler(
)

const BUILD_PATH = '../build/index.js'
const WATCH_PATH = '../build/version.txt'

const build = remixBuild as unknown as ServerBuild
/**
* Initial build
* @type {ServerBuild}
*/
const build = await import(BUILD_PATH)
let devBuild = build

const app = express()
Expand Down Expand Up @@ -262,8 +262,14 @@ if (MODE === 'development') {
broadcastDevReady(devBuild)
}

// We'll import chokidar here so doesn't get bundled in production.
const chokidar = await import('chokidar')

const dirname = path.dirname(fileURLToPath(import.meta.url))
const watchPath = path.join(dirname, BUILD_PATH).replace(/\\/g, '/')
const watcher = chokidar.watch(watchPath, { ignoreInitial: true })
watcher.on('all', reloadBuild)
const watchPath = path.join(dirname, WATCH_PATH).replace(/\\/g, '/')

chokidar
.watch(watchPath, { ignoreInitial: true })
.on('add', reloadBuild)
.on('change', reloadBuild)
}

0 comments on commit 4626d54

Please sign in to comment.