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

Update chokidar to watch version.txt #450

Merged
merged 1 commit into from
Sep 16, 2023
Merged
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
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)
}
Loading