Skip to content

Commit

Permalink
Support config file
Browse files Browse the repository at this point in the history
  • Loading branch information
pacexy committed Jul 10, 2021
1 parent 56f28d4 commit d42ef2f
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions bin/mv3-hot-reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,32 @@ import { debounce } from 'lodash'
import path from 'path'
import { Message } from '../src/utils'

const PORT = 9012
const DIST_DIRECTORY = path.resolve('dist')
interface Config {
port?: number
directory?: string
exclude?: string[]
}

const wss = new WebSocket.Server({ port: PORT })
let port = 9012
let directory = 'dist'
let exclude: string[] = []

try {
const CONFIG_PATH = path.resolve('mv3-hot-reload.config.js')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const config: Config = require(CONFIG_PATH)

port = config.port || port
directory = config.directory || directory
exclude = config.exclude || exclude
} catch (err) {
// ignore
}

const directoryPath = path.resolve(directory)
const excludePaths = exclude.map((file) => path.join(directoryPath, file))

const wss = new WebSocket.Server({ port })

wss.on('listening', () => {
console.log('hot reload server is listening...')
Expand All @@ -22,14 +44,16 @@ wss.on('close', () => {

wss.on('connection', (ws) => {
chokidar
.watch(DIST_DIRECTORY, {
.watch(directoryPath, {
ignoreInitial: true,
})
.on(
'all',
debounce(() => {
console.log('file change detected.')
ws.send(Message.FileChange)
debounce((_, path) => {
if (!excludePaths.includes(path)) {
console.log('file change detected.')
ws.send(Message.FileChange)
}
}, 500),
)
})

0 comments on commit d42ef2f

Please sign in to comment.