babel-watch
Advanced tools
Comparing version 7.1.0 to 7.2.0
@@ -16,3 +16,8 @@ #!/usr/bin/env node | ||
const isRegExp = require('lodash.isregexp'); | ||
const Debug = require('debug'); | ||
const debugInit = Debug('babel-watch:init'); | ||
const debugCompile = Debug('babel-watch:compile'); | ||
const debugWatcher = Debug('babel-watch:watcher'); | ||
const RESTART_COMMAND = 'rs'; | ||
@@ -39,3 +44,3 @@ const DEBOUNCE_DURATION = 100; //milliseconds | ||
function arrayify(val) { | ||
if (!val) return []; | ||
if (!val) return null; | ||
if (isString(val)) return (val ? val.split(',') : []); | ||
@@ -46,2 +51,3 @@ if (Array.isArray(val)) return val; | ||
class IgnoredFileError extends Error {}; | ||
@@ -52,11 +58,12 @@ program.option('-d, --debug [port]', 'Set debugger port') | ||
program.option('-X, --inspect-brk [address]', 'Enable inspect break mode') | ||
program.option('-o, --only [globs]', 'Matching files will be transpiled'); | ||
program.option('-i, --ignore [globs]', 'Matching files will not be transpiled'); | ||
program.option('-e, --extensions [extensions]', 'List of extensions to hook into [.es6,.js,.es,.jsx]'); | ||
program.option('-o, --only [globs]', 'Matching files will *only* be transpiled', arrayify, null); | ||
program.option('-i, --ignore [globs]', 'Matching files will not be transpiled. Default value is "node_modules". If you specify this option and still want to exclude modules, be sure to add it to the list.', arrayify, ['node_modules']); | ||
program.option('-e, --extensions [extensions]', 'List of extensions to hook into', arrayify, babel.DEFAULT_EXTENSIONS); | ||
program.option('-w, --watch [dir]', 'Watch directory "dir" or files. Use once for each directory or file to watch', collect, []); | ||
program.option('-x, --exclude [dir]', 'Exclude matching directory/files from watcher. Use once for each directory or file.', collect, []); | ||
program.option('-L, --use-polling', 'In some filesystems watch events may not work correcly. This option enables "polling" which should mitigate this type of issues'); | ||
program.option('-x, --exclude [dir]', 'Exclude matching directory/files from watcher. Use once for each directory or file', collect, []); | ||
program.option('-L, --use-polling', 'In some filesystems watch events may not work correcly. This option enables "polling" which should mitigate this type of issue'); | ||
program.option('-D, --disable-autowatch', 'Don\'t automatically start watching changes in files "required" by the program'); | ||
program.option('-H, --disable-ex-handler', 'Disable source-map-enhanced uncaught exception handler. (you may want to use this option in case your app registers a custom uncaught exception handler)'); | ||
program.option('-m, --message [string]', 'Set custom message displayed on restart (default is ">>> RESTARTING <<<")'); | ||
program.option('-H, --disable-ex-handler', 'Disable source-map-enhanced uncaught exception handler. You may want to use this option in case your app registers a custom uncaught exception handler'); | ||
program.option('-m, --message [string]', 'Set custom message displayed on restart', '>>> RESTARTING <<<'); | ||
program.option('--clear-console', 'If set, will clear console on each restart. Restart message will not be shown'); | ||
@@ -89,2 +96,7 @@ const pkg = require('./package.json'); | ||
Debugging: | ||
If you want to know which file caused a restart, or why a file was not processed, add | ||
\`env DEBUG="babel-watch:*"\` before your command to see babel-watch internals. | ||
See more: | ||
@@ -99,14 +111,7 @@ | ||
let only, ignore; | ||
const only = program.only; | ||
const ignore = program.ignore; | ||
const transpileExtensions = program.extensions; | ||
const restartMessage = program.message; | ||
if (program.only != null) only = arrayify(program.only, regexify); | ||
if (program.ignore != null) ignore = arrayify(program.ignore, regexify); | ||
let transpileExtensions = babel.DEFAULT_EXTENSIONS; | ||
if (program.extensions) { | ||
transpileExtensions = transpileExtensions.concat(arrayify(program.extensions)); | ||
} | ||
const mainModule = program.args[0]; | ||
@@ -125,2 +130,3 @@ if (!mainModule) { | ||
const errors = {}; | ||
const ignored = {}; | ||
@@ -134,4 +140,6 @@ const watcher = chokidar.watch(program.watch, { | ||
let watcherInitialized = (program.watch.length === 0); | ||
debugInit('Initializing babel-watch with options: %j', program.opts()); | ||
process.on('SIGINT', function() { | ||
debugInit('SIGINT caught, closing.'); | ||
watcher.close(); | ||
@@ -142,3 +150,2 @@ killApp(); | ||
watcher.on('change', handleChange); | ||
@@ -173,7 +180,12 @@ watcher.on('add', handleChange); | ||
const absoluteFile = file.startsWith('/') ? file : path.join(cwd, file); | ||
delete cache[absoluteFile]; | ||
delete errors[absoluteFile]; | ||
const isUsed = Boolean(cache[absoluteFile] || errors[absoluteFile]); | ||
if (isUsed) { | ||
delete cache[absoluteFile]; | ||
delete errors[absoluteFile]; | ||
// file is in use by the app, let's restart! | ||
debouncedRestartApp(); | ||
// file is in use by the app, let's restart! | ||
debouncedRestartApp(); | ||
} | ||
// File was not in use, don't restart | ||
debugWatcher('Change detected in file: %s. File used by program and not ignored? %s', file, isUsed); | ||
} | ||
@@ -203,3 +215,10 @@ | ||
compile(filename, (err, result) => { | ||
debugCompile('Compiled file: %s. Success? %s', filename, !err); | ||
if (err) { | ||
// Intentional ignore | ||
if (err instanceof IgnoredFileError) { | ||
ignored[filename] = true; | ||
debugCompile('File %s ignored due to extension or intentional ignore rule.', filename); | ||
return callback(); | ||
} | ||
console.error('Babel compilation error', err.stack); | ||
@@ -267,9 +286,11 @@ errors[filename] = true; | ||
function prepareRestart() { | ||
if (watcherInitialized && childApp) { | ||
function restartApp() { | ||
if (!watcherInitialized) return; | ||
if (childApp) { | ||
if (program.clearConsole) console.clear(); | ||
else if (program.message) console.log(program.message); | ||
// kill app early as `compile` may take a while | ||
var restartMessage = program.message ? program.message : ">>> RESTARTING <<<"; | ||
console.log(restartMessage); | ||
killApp(); | ||
} else { | ||
// First start | ||
restartAppInternal(); | ||
@@ -279,7 +300,2 @@ } | ||
function restartApp() { | ||
if (!watcherInitialized) return; | ||
prepareRestart(); | ||
} | ||
function restartAppInternal() { | ||
@@ -375,3 +391,3 @@ if (Object.keys(errors).length != 0) { | ||
handleUncaughtExceptions: !program.disableExHandler, | ||
transpileExtensions: transpileExtensions, | ||
transpileExtensions, | ||
}); | ||
@@ -382,18 +398,23 @@ pipeFd = fs.openSync(pipeFilename, 'w'); | ||
// Only ignore based on extension for now, which we keep track of on our own for file watcher | ||
// purposes. `ignore` and `only` are passed to `babel.OptionManager` to let it make its own | ||
// determinations. | ||
function shouldIgnore(filename) { | ||
if (transpileExtensions.indexOf(path.extname(filename)) < 0) { | ||
if (!transpileExtensions.includes(path.extname(filename))) { | ||
return true; | ||
} else if (!ignore && !only) { | ||
// Ignore node_modules by default | ||
return path.relative(cwd, filename).split(path.sep).indexOf('node_modules') >= 0; | ||
} else { | ||
return babel.util.shouldIgnore(filename, ignore || [], only); | ||
} else if (ignored[filename]) { | ||
// ignore cache for extra speed | ||
return true; | ||
} | ||
return false; | ||
} | ||
function compile(filename, callback) { | ||
const optsManager = new babel.OptionManager; | ||
const opts = new babel.OptionManager().init({ filename, ignore, only }); | ||
const opts = optsManager.init({ filename }); | ||
// If opts is not present, the file is ignored, either by explicit input into | ||
// babel-watch or by `.babelignore`. | ||
if (!opts) { | ||
return callback(new IgnoredFileError()); | ||
} | ||
// Do not process config files since has already been done with the OptionManager | ||
@@ -400,0 +421,0 @@ // calls above and would introduce duplicates. |
{ | ||
"name": "babel-watch", | ||
"version": "7.1.0", | ||
"version": "7.2.0", | ||
"description": "Reload your babel-node app on JS source file changes. And do it *fast*.", | ||
@@ -35,2 +35,3 @@ "main": "babel-watch.js", | ||
"commander": "^6.2.0", | ||
"debug": "^4.3.1", | ||
"lodash.debounce": "^4.0.8", | ||
@@ -46,3 +47,6 @@ "lodash.isregexp": "^4.0.1", | ||
"babel-watch": "./babel-watch.js" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.12.10" | ||
} | ||
} |
@@ -31,3 +31,3 @@ # babel-watch | ||
(Make sure you have `babel-core` installed as dependency in your project as `babel-watch` only defines `babel-core` as a "peerDependency") | ||
(Make sure you have `@babel/core` installed as dependency in your project as `babel-watch` only defines `@babel/core` as a "peerDependency") | ||
@@ -46,17 +46,19 @@ Then use `babel-watch` in your `package.json` in scripts section like this: | ||
``` | ||
-d, --debug [port] Start debugger on port | ||
-d, --debug [port] Set debugger port | ||
-B, --debug-brk Enable debug break mode | ||
-I, --inspect Enable inspect mode | ||
-I, --inspect [address] Enable inspect mode | ||
-X, --inspect-brk [address] Enable inspect break mode | ||
-o, --only [globs] Matching files will be transpiled | ||
-i, --ignore [globs] Matching files will not be transpiled | ||
-e, --extensions [extensions] List of extensions to hook into [.es6,.js,.es,.jsx] | ||
-p, --plugins [string] | ||
-b, --presets [string] | ||
-w, --watch [dir] Watch directory "dir" or files. Use once for each directory or file to watch | ||
-x, --exclude [dir] Exclude matching directory/files from watcher. Use once for each directory or file. | ||
-L, --use-polling In some filesystems watch events may not work correcly. This option enables "polling" which should mitigate this type of issues | ||
-o, --only [globs] Matching files will *only* be transpiled (default: null) | ||
-i, --ignore [globs] Matching files will not be transpiled. Default value is "node_modules". If you specify this option and still want to exclude modules, be sure to add it to the list. | ||
(default: ["node_modules"]) | ||
-e, --extensions [extensions] List of extensions to hook into (default: [".js",".jsx",".es6",".es",".mjs"]) | ||
-w, --watch [dir] Watch directory "dir" or files. Use once for each directory or file to watch (default: []) | ||
-x, --exclude [dir] Exclude matching directory/files from watcher. Use once for each directory or file (default: []) | ||
-L, --use-polling In some filesystems watch events may not work correcly. This option enables "polling" which should mitigate this type of issue | ||
-D, --disable-autowatch Don't automatically start watching changes in files "required" by the program | ||
-H, --disable-ex-handler Disable source-map-enhanced uncaught exception handler. (you may want to use this option in case your app registers a custom uncaught exception handler) | ||
-m, --message [string] Set custom message displayed on restart (default is ">>> RESTARTING <<<") | ||
-H, --disable-ex-handler Disable source-map-enhanced uncaught exception handler. You may want to use this option in case your app registers a custom uncaught exception handler | ||
-m, --message [string] Set custom message displayed on restart (default: ">>> RESTARTING <<<") | ||
--clear-console If set, will clear console on each restart. Restart message will not be shown | ||
-V, --version output the version number | ||
-h, --help display help for command | ||
``` | ||
@@ -114,2 +116,6 @@ | ||
#### Debugging | ||
If you want to know which file caused a restart, or why a file was not processed, add `env DEBUG="babel-watch:*"` before your command to see babel-watch internals. Please do this before filing a bug report. | ||
#### Application doesn't restart automatically | ||
@@ -122,3 +128,2 @@ | ||
#### Application doesn't restart when I change one of the view templates (html file or similar): | ||
@@ -128,5 +133,5 @@ | ||
#### I'm getting an error: *Cannot find module 'babel-core'* | ||
#### I'm getting an error: *Cannot find module '@babel/core'* | ||
`babel-watch` does not have `babel-core` listed as a direct dependency but as a "peerDependency". If you're using `babel` in your app you should already have `babel-core` installed. If not you should do `npm install --save-dev babel-core`. We decided not to make `babel-core` a direct dependency as in some cases having it defined this way would make your application pull two versions of `babel-core` from `npm` during installation and since `babel-core` is quite a huge package that's something we wanted to avoid. | ||
`babel-watch` does not have `@babel/core` listed as a direct dependency but as a "peerDependency". If you're using `babel` in your app you should already have `@babel/core` installed. If not you should do `npm install --save-dev @babel/core`. We decided not to make `@babel/core` a direct dependency as in some cases having it defined this way would make your application pull two versions of `@babel/core` from `npm` during installation and since `@babel/core` is quite a huge package that's something we wanted to avoid. | ||
@@ -141,3 +146,2 @@ #### Every time I run a script, I get a load of temporary files clogging up my project root | ||
#### Still having some issues | ||
@@ -144,0 +148,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
703352
26949
149
2
8
1
+ Addeddebug@^4.3.1