error-handled-node-hmr
v1.0.0
Published
Module to handle errors when using node-hmr.
Downloads
1
Maintainers
Readme
error-handled-node-hmr
Module to handle errors when using node-hmr.
API
ehhmr(hmr, watchFile, [callbackImported], [callbackPreImport], [options], [callbackCatch])
hmr
node-hmr module. RequiredwatchFile
import module name or path. RequiredcallbackImported
Function called when module is importedcallbackPreImport
Function called before importing a moduleoptions
node-hmr options- The
watchFilePatterns
option is set to[watchFile]
by default
- The
callbackCatch
- By default, errors are output to the console,
- but you can output errors to the console yourself by assigning a function to callbackCatch.
Usage
const hmr = require('node-hmr')
const ehhmr = require('error-handled-node-hmr')
var watchFile = './watched.js'
ehhmr(hmr, watchFile, importedModule => {
console.log('imported')
})
Equivalent to:
const hmr = require('node-hmr')
var watchFile = './watched.js'
hmr(() => {
try {
const importedModule = require(watchFile)
console.log('imported')
} catch (e) {
console.error(e)
const moduleId = require.resolve(watchFile)
require.cache[moduleId] = { id: moduleId }
}
}, { watchFilePatterns: [watchFile] })
Customize the handling before importing a module
let watchedModule
ehhmr(hmr, watchFile, importedModule => {
watchedModule = importedModule
}, () => console.log('pre import'))
Custom node-hmr options
ehhmr(hmr, watchFile, importedModule => {
console.log('imported')
}, undefined, {
watchFilePatterns: [
watchFile,
'./moduleB.js'
]
})
Use hmr only in development mode
const dev = process.env.NODE_ENV !== 'production'
let handleModule
const fn = importedModule => {
handleModule = importedModule
}
if (dev) {
const hmr = require('node-hmr')
const ehhmr = require('error-handled-node-hmr')
var handleFile = './watched.js'
ehhmr(hmr, handleFile, importedModule => {
fn(importedModule)
})
} else
fn(require(handleFile))