dl6
v0.3.0
Published
Dynamic Loading Module (by ES6 Proxy)
Downloads
2
Readme
dl6 -- Dynamic Loading Module for Node.js
dl6.require() delay the loading until the first time you call it.
dl6.require() reload your module automatically when the module is modified !
We use the es6 proxy and fs.watch() to implement the dl6 package.
Install
$ npm i dl6
Example
File: dl6ex.js
const dl6 = require('dl6')
const fs = require('fs')
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
async function main () {
fs.writeFileSync('cat.js', `
module.exports = {
say: () => 'meow'
}
`)
const cat = dl6.dload(__dirname, 'cat')
console.log('cat=', cat)
console.log(cat.say())
console.log('cat=', cat)
fs.writeFileSync('cat.js', `
module.exports = {
say: () => 'hello'
}
`)
await sleep(1000)
console.log(cat.say())
dl6.end()
}
main().catch((error) => console.log(error))
Run:
$ node dl6ex.js
cat= {}
call D:\ccc\code\js\dl6\example\cat : say
meow
cat= { say: [Function: say] }
call D:\ccc\code\js\dl6\example\cat : say
hello