diff --git a/index.js b/index.js index 0e16463..e38e3d9 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var fs = require('fs'); var React = require('react-tools'); +var watch = require('node-watch'); var installed = false; @@ -23,12 +24,39 @@ function install(options) { } catch (e) { throw new Error('Error transforming ' + filename + ' to JSX: ' + e.toString()); } + + // Watch the fie we just transformed and expire it + if (options.watch) { + watch(filename, watchFn); + } + module._compile(src, filename); }; installed = true; } +function watchFn(filename) { + expire(filename, true); +} + +function expire(id, recursive) { + var modules = [id]; + var module = require.cache[id]; + + // Go through all the module's parents. Seems to be necessary to clear the cache + while (recursive && module && module.parent) { + module = module.parent; + + modules.unshift(module.id); + hasParents = module.parent; + } + + for (var i = 0; i < modules.length; i++) { + delete require.cache[modules[i]]; + } +} + module.exports = { install: install }; diff --git a/package.json b/package.json index 3e3e34a..f8c9407 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "transparently require() jsx from node", "main": "index.js", "dependencies": { + "node-watch": "~0.3.4", "react-tools": "^0.12.1" }, "devDependencies": {