From 266d4a15da169d5323621b720bdbcf6962f3dc86 Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Tue, 23 Jun 2020 03:18:16 -0400 Subject: [PATCH 01/11] Enable plugins to mark external dependencies --- packages/babel-core/src/config/full.ts | 4 ++++ packages/babel-core/src/index.ts | 1 + 2 files changed, 5 insertions(+) diff --git a/packages/babel-core/src/config/full.ts b/packages/babel-core/src/config/full.ts index 64ecd6e75fe2..15f2bd948c0a 100644 --- a/packages/babel-core/src/config/full.ts +++ b/packages/babel-core/src/config/full.ts @@ -226,6 +226,9 @@ function enhanceError(context, fn: T): T { } as any; } +const dependencies = new Set(); +export const getExternalDependencies = () => dependencies; + /** * Load a generic plugin/preset from the given descriptor loaded from the config object. */ @@ -252,6 +255,7 @@ const makeDescriptorLoader = ( ...context, ...apiFactory(cache), }; + api.addExternalDependency = fileName => dependencies.add(fileName); try { item = yield* factory(api, options, dirname); } catch (e) { diff --git a/packages/babel-core/src/index.ts b/packages/babel-core/src/index.ts index 8abd7cc17489..c8dee44c3baa 100644 --- a/packages/babel-core/src/index.ts +++ b/packages/babel-core/src/index.ts @@ -27,6 +27,7 @@ export { loadOptions, loadOptionsSync, loadOptionsAsync, + getExternalDependencies, } from "./config"; export { transform, transformSync, transformAsync } from "./transform"; From 3b04c0f80d0c57aa9059c70a9312ccdb101c43b1 Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Tue, 23 Jun 2020 12:59:42 -0400 Subject: [PATCH 02/11] Add missing export --- packages/babel-core/src/config/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/babel-core/src/config/index.ts b/packages/babel-core/src/config/index.ts index 2aad3de17bcf..4f28534c6c8e 100644 --- a/packages/babel-core/src/config/index.ts +++ b/packages/babel-core/src/config/index.ts @@ -60,3 +60,4 @@ export function createConfigItem( return createConfigItemRunner.sync(target, options); } } +export { getExternalDependencies } from "./full"; \ No newline at end of file From f0487955a08e391b922db897bf03dc8f51b03c53 Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Fri, 26 Jun 2020 02:27:38 -0400 Subject: [PATCH 03/11] Detailed external dependency tracking It's not sufficient to know the file paths of all external dependencies. Babel needs to know which source files correspond to which external dependencies and vice-versa. --- .../src/config/helpers/config-api.ts | 65 +++++++++++++++++++ packages/babel-core/src/index.ts | 6 +- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/packages/babel-core/src/config/helpers/config-api.ts b/packages/babel-core/src/config/helpers/config-api.ts index 16fe7040a28e..54198b8b864d 100644 --- a/packages/babel-core/src/config/helpers/config-api.ts +++ b/packages/babel-core/src/config/helpers/config-api.ts @@ -9,6 +9,8 @@ import type { SimpleType, } from "../caching"; +import path from "path"; + import type { CallerMetadata } from "../validation/options"; import * as Context from "../cache-contexts"; @@ -33,6 +35,10 @@ export type ConfigAPI = { async: () => boolean; assertVersion: typeof assertVersion; caller?: CallerFactory; + addExternalDependency: ( + externalDependencyFileName: string, + dependentFileName: string, + ) => void; }; export type PresetAPI = { @@ -43,6 +49,64 @@ export type PluginAPI = { assumption: AssumptionFunction; } & PresetAPI; +/** + * "dependencies" are source files that are directly compiled as part of the + * normal compilation process. + * + * "externalDependencies" are non-source files that should trigger a recompilation + * of some source file when they are changed. An example is a markdown file that + * is inlined into a source file as part of a Babel plugin. + */ + +const dependencies = new Map>(); +const externalDependencies = new Map>(); +/** + * @returns a map of source file paths to their external dependencies. + */ +export const getDependencies = () => dependencies; +/** + * @returns a map of external dependencies to the source file paths + * that depend on them. + */ +export const getExternalDependencies = () => externalDependencies; + +/** + * Indicate that Babel should recompile the file at @param dependentFilePath when + * the file at @param externalDependencyPath changes. @param externalDependencyPath can + * be any arbitrary file. NOTE: This currently only works with @babel/cli's --watch flag. + * @param externalDependencyPath Must be either + * absolute or relative to @param currentFilePath. + * @param dependentFilePath Must be absolute or relative to the directory Babel was launched from. + * For plugin authors, this is usually the current file being processed by Babel/your plugin. + * It can be found at: "state.file.opts.filename". + */ +function addExternalDependency( + externalDependencyPath: string, + dependentFilePath: string, +): void { + /** + * Inside the dependency maps we want to store all paths as absolute because we can + * derive a relative path from an absolute path but not vice-versa. Also, Webpack's + * `addDependency` requires absolute paths. + */ + const currentFileAbsolutePath = path.resolve(dependentFilePath); + const currentFileDir = path.dirname(currentFileAbsolutePath); + const externalDependencyAbsolutePath = path.isAbsolute(externalDependencyPath) + ? externalDependencyPath + : path.join(currentFileDir, externalDependencyPath); + + if (!dependencies.has(currentFileAbsolutePath)) { + dependencies.set(currentFileAbsolutePath, new Set()); + } + if (!externalDependencies.has(externalDependencyAbsolutePath)) { + externalDependencies.set(externalDependencyAbsolutePath, new Set()); + } + dependencies.get(currentFileAbsolutePath).add(externalDependencyAbsolutePath); + externalDependencies + .get(externalDependencyAbsolutePath) + .add(currentFileAbsolutePath); +} + export function makeConfigAPI( cache: CacheConfigurator, ): ConfigAPI { @@ -72,6 +136,7 @@ export function makeConfigAPI( async: () => false, caller, assertVersion, + addExternalDependency, }; } diff --git a/packages/babel-core/src/index.ts b/packages/babel-core/src/index.ts index c8dee44c3baa..b6f300806165 100644 --- a/packages/babel-core/src/index.ts +++ b/packages/babel-core/src/index.ts @@ -27,9 +27,13 @@ export { loadOptions, loadOptionsSync, loadOptionsAsync, - getExternalDependencies, } from "./config"; +export { + getDependencies, + getExternalDependencies, +} from "./config/helpers/config-api"; + export { transform, transformSync, transformAsync } from "./transform"; export { transformFile, From b9c073deec7d72be121145fbc5d3e48205c9d966 Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Fri, 26 Jun 2020 03:06:28 -0400 Subject: [PATCH 04/11] Integrate external dependency tracking with watch mode --- packages/babel-cli/src/babel/dir.ts | 90 +++++++++++--------- packages/babel-cli/src/babel/file.ts | 46 +++++----- packages/babel-cli/src/babel/util.ts | 120 ++++++++++++++++++++++++--- 3 files changed, 178 insertions(+), 78 deletions(-) diff --git a/packages/babel-cli/src/babel/dir.ts b/packages/babel-cli/src/babel/dir.ts index 2ab0652afbb3..93135be0e760 100644 --- a/packages/babel-cli/src/babel/dir.ts +++ b/packages/babel-cli/src/babel/dir.ts @@ -21,7 +21,7 @@ export default async function ({ cliOptions, babelOptions, }: CmdOptions): Promise { - const filenames = cliOptions.filenames; + const filepaths = cliOptions.filenames.map(name => path.resolve(name)); async function write( src: string, @@ -173,44 +173,56 @@ export default async function ({ } if (cliOptions.watch) { - const chokidar = util.requireChokidar(); - - filenames.forEach(function (filenameOrDir: string): void { - const watcher = chokidar.watch(filenameOrDir, { - persistent: true, - ignoreInitial: true, - awaitWriteFinish: { - stabilityThreshold: 50, - pollInterval: 10, - }, - }); + let processing = 0; - // This, alongside with debounce, allows us to only log - // when we are sure that all the files have been compiled. - let processing = 0; - - ["add", "change"].forEach(function (type: string): void { - watcher.on(type, async function (filename: string) { - processing++; - if (startTime === null) startTime = process.hrtime(); - - try { - await handleFile( - filename, - filename === filenameOrDir - ? path.dirname(filenameOrDir) - : filenameOrDir, - ); - - compiledFiles++; - } catch (err) { - console.error(err); - } - - processing--; - if (processing === 0 && !cliOptions.quiet) logSuccess(); - }); - }); - }); + util.onDependencyFileChanged(async (changedFilePath: string) => { + if ( + !util.isCompilableExtension(changedFilePath, cliOptions.extensions) && + // See comment on corresponding code in file.js + !filepaths.includes(changedFilePath) + ) { + return; + } + processing++; + if (startTime === null) startTime = process.hrtime(); + + /** + * The relative path from @var base to @var changedFilePath + * will be path of @var changedFilePath in the output directory. + */ + let base = null; + for (const filePath of filepaths) { + if (changedFilePath === filePath) { + // Case: "babel --watch src/bar/foo.js --out-dir dist" + // We want src/bar/foo.js --> dist/foo.js + base = path.dirname(changedFilePath); + break; + } else if (util.isChildPath(changedFilePath, filePath)) { + // Case: "babel --watch src/ --out-dir dist" + // src/foo/bar.js changes + // We want src/foo/bar.js --> dist/foo/bar.js + base = filePath; + break; + } + } + + if (base === null) { + throw new Error( + `path: ${changedFilePath} was not equal to/a child path of any of these paths: ${filepaths}`, + ); + } + + try { + await handleFile(changedFilePath, base); + + compiledFiles++; + } catch (err) { + console.error(err); + } + + processing--; + if (processing === 0 && !cliOptions.quiet) logSuccess(); + }, false); + util.watchFiles(filepaths); } } diff --git a/packages/babel-cli/src/babel/file.ts b/packages/babel-cli/src/babel/file.ts index e05f664979ad..1164c37b1c10 100644 --- a/packages/babel-cli/src/babel/file.ts +++ b/packages/babel-cli/src/babel/file.ts @@ -198,36 +198,30 @@ export default async function ({ } if (cliOptions.watch) { - const chokidar = util.requireChokidar(); - chokidar - .watch(filenames, { - disableGlobbing: true, - persistent: true, - ignoreInitial: true, - awaitWriteFinish: { - stabilityThreshold: 50, - pollInterval: 10, - }, - }) - .on("all", function (type: string, filename: string): void { - if ( - !util.isCompilableExtension(filename, cliOptions.extensions) && - !filenames.includes(filename) - ) { - return; - } + util.onDependencyFileChanged((filename: string | null) => { + if ( + filename !== null && + !util.isCompilableExtension(filename, cliOptions.extensions) && + // Used in the case: babel --watch foo.ts --out-file compiled.js + // In this, case ".ts" is not a compilable extension (since the user didn't pass + // the --extensions flag), but, we still want to watch "foo.ts" anyway. + !filenames.includes(filename) + ) { + return; + } - if (type === "add" || type === "change") { - if (cliOptions.verbose) { - console.log(type + " " + filename); - } + if (cliOptions.verbose) { + if (filename === null) { + console.log(`recompiling: external dependency changed`); + } else console.log(`compiling: ${filename}`); + } - walk(filenames).catch(err => { - console.error(err); - }); - } + walk(filenames).catch(err => { + console.error(err); }); + }, true); } + util.watchFiles(filenames); } if (cliOptions.filenames.length) { diff --git a/packages/babel-cli/src/babel/util.ts b/packages/babel-cli/src/babel/util.ts index 992a77d4a2ea..4110eccb9557 100644 --- a/packages/babel-cli/src/babel/util.ts +++ b/packages/babel-cli/src/babel/util.ts @@ -4,6 +4,10 @@ import path from "path"; import fs from "fs"; import { createRequire } from "module"; +/** + * Set the file permissions of dest to the file permissions + * of src. + */ export function chmod(src: string, dest: string): void { try { fs.chmodSync(dest, fs.statSync(src).mode); @@ -60,7 +64,7 @@ const CALLER = { name: "@babel/cli", }; -export function transform( +export async function transform( filename: string, code: string, opts: any, @@ -71,26 +75,116 @@ export function transform( filename, }; - return new Promise((resolve, reject) => { - babel.transform(code, opts, (err, result) => { - if (err) reject(err); - else resolve(result); - }); - }); + const result = await babel.transformAsync(code, opts); + registerNewExternalDependencies(filename); + return result; } -export function compile(filename: string, opts: any | Function): Promise { +export async function compile( + filename: string, + opts: any | Function, +): Promise { opts = { ...opts, caller: CALLER, }; - return new Promise((resolve, reject) => { - babel.transformFile(filename, opts, (err, result) => { - if (err) reject(err); - else resolve(result); + const result = await babel.transformFileAsync(filename, opts); + registerNewExternalDependencies(filename); + return result; +} + +/** + * Check if @param child is a child of @param parent + * Both paths must be absolute/resolved. (No "..") + */ +export function isChildPath(child: string, parent: string): boolean { + return ( + child.length > parent.length + 1 && child.startsWith(parent + path.sep) + ); +} + +function subtract(minuend: Set, subtrahend: Set): string[] { + const diff = []; + for (const e of minuend) { + if (!subtrahend.has(e)) diff.push(e); + } + return diff; +} + +const registerNewExternalDependencies = (() => { + let prevDeps = babel.getDependencies(); + return (filePath: string) => { + // make the file path absolute because + // dependencies are registered with absolute file paths + filePath = path.resolve(filePath); + const prevDepsForFile = prevDeps.get(filePath) || new Set(); + const newDeps = babel.getDependencies(); + const newDepsForFile = newDeps.get(filePath) || new Set(); + const unwatchedDepsForFile = subtract(newDepsForFile, prevDepsForFile); + for (const dep of unwatchedDepsForFile) { + watchFiles(dep); + } + prevDeps = newDeps; + }; +})(); + +const getWatcher = (() => { + // Use a closure to ensure the file watcher is only created once + // and never re-assigned. A const global variable isn't sufficient + // because we only want to create the file watcher if the user passes + // the --watch option, and a const variable must always be initialized. + let watcher = undefined; + return () => { + if (watcher) return watcher; + const { FSWatcher } = requireChokidar(); + watcher = new FSWatcher({ + disableGlobbing: true, + persistent: true, + ignoreInitial: true, + awaitWriteFinish: { + stabilityThreshold: 50, + pollInterval: 10, + }, }); - }); + return watcher; + }; +})(); + +export function onDependencyFileChanged( + callback: (filename_: string | null) => Promise, + sourceFilesAreCompiledIntoASingleFile: boolean, +): void { + /** + * + * @param filePath The path of a file that has changed. + * It will never be a path to a directory. + * */ + async function onFileChanged(filePath: string) { + // see corresponding line in registerNewExternalDependencies + filePath = path.resolve(filePath); + const externalFileDeps = babel.getExternalDependencies(); + if (externalFileDeps.has(filePath)) { + if (sourceFilesAreCompiledIntoASingleFile) { + // When using --out-file, Babel traverses all the files every time + // so there's no point in calling the callback multiple times. The callback + // for --out-file knows to recompile no matter what if it receives null. + return await callback(null); + } else { + for (const dependent of externalFileDeps.get(filePath)) { + await callback(dependent); + } + await callback(filePath); + } + } else { + await callback(filePath); + } + } + ["add", "change"].forEach(type => getWatcher().on(type, onFileChanged)); +} + +export function watchFiles(filenameOrFilenames: string | string[]): void { + getWatcher().add(filenameOrFilenames); } export function deleteDir(path: string): void { From 3bb337cb5343d75730053e9125e589a8795b6ca7 Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Fri, 26 Jun 2020 03:16:16 -0400 Subject: [PATCH 05/11] Formatting/naming --- packages/babel-cli/src/babel/dir.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/babel-cli/src/babel/dir.ts b/packages/babel-cli/src/babel/dir.ts index 93135be0e760..dc0f335d1245 100644 --- a/packages/babel-cli/src/babel/dir.ts +++ b/packages/babel-cli/src/babel/dir.ts @@ -21,7 +21,9 @@ export default async function ({ cliOptions, babelOptions, }: CmdOptions): Promise { - const filepaths = cliOptions.filenames.map(name => path.resolve(name)); + const absoluteFilePaths = cliOptions.filenames.map(name => + path.resolve(name), + ); async function write( src: string, @@ -179,7 +181,7 @@ export default async function ({ if ( !util.isCompilableExtension(changedFilePath, cliOptions.extensions) && // See comment on corresponding code in file.js - !filepaths.includes(changedFilePath) + !absoluteFilePaths.includes(changedFilePath) ) { return; } @@ -191,7 +193,7 @@ export default async function ({ * will be path of @var changedFilePath in the output directory. */ let base = null; - for (const filePath of filepaths) { + for (const filePath of absoluteFilePaths) { if (changedFilePath === filePath) { // Case: "babel --watch src/bar/foo.js --out-dir dist" // We want src/bar/foo.js --> dist/foo.js @@ -208,13 +210,12 @@ export default async function ({ if (base === null) { throw new Error( - `path: ${changedFilePath} was not equal to/a child path of any of these paths: ${filepaths}`, + `path: ${changedFilePath} was not equal to/a child path of any of these paths: ${absoluteFilePaths}`, ); } try { await handleFile(changedFilePath, base); - compiledFiles++; } catch (err) { console.error(err); @@ -223,6 +224,6 @@ export default async function ({ processing--; if (processing === 0 && !cliOptions.quiet) logSuccess(); }, false); - util.watchFiles(filepaths); + util.watchFiles(absoluteFilePaths); } } From d500b8cfb89d9e06aeeb460572080fddb11c407e Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Fri, 26 Jun 2020 16:58:29 -0400 Subject: [PATCH 06/11] Only watch files when watch flag is set --- packages/babel-cli/src/babel/file.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-cli/src/babel/file.ts b/packages/babel-cli/src/babel/file.ts index 1164c37b1c10..d494a2e709ef 100644 --- a/packages/babel-cli/src/babel/file.ts +++ b/packages/babel-cli/src/babel/file.ts @@ -220,8 +220,8 @@ export default async function ({ console.error(err); }); }, true); + util.watchFiles(filenames); } - util.watchFiles(filenames); } if (cliOptions.filenames.length) { From 896980399f2b4f411dae66196d97210062884e87 Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Fri, 26 Jun 2020 21:09:08 -0400 Subject: [PATCH 07/11] Remove outdated addExternalDependency --- packages/babel-core/src/config/full.ts | 4 ---- packages/babel-core/src/config/index.ts | 1 - 2 files changed, 5 deletions(-) diff --git a/packages/babel-core/src/config/full.ts b/packages/babel-core/src/config/full.ts index 15f2bd948c0a..64ecd6e75fe2 100644 --- a/packages/babel-core/src/config/full.ts +++ b/packages/babel-core/src/config/full.ts @@ -226,9 +226,6 @@ function enhanceError(context, fn: T): T { } as any; } -const dependencies = new Set(); -export const getExternalDependencies = () => dependencies; - /** * Load a generic plugin/preset from the given descriptor loaded from the config object. */ @@ -255,7 +252,6 @@ const makeDescriptorLoader = ( ...context, ...apiFactory(cache), }; - api.addExternalDependency = fileName => dependencies.add(fileName); try { item = yield* factory(api, options, dirname); } catch (e) { diff --git a/packages/babel-core/src/config/index.ts b/packages/babel-core/src/config/index.ts index 4f28534c6c8e..2aad3de17bcf 100644 --- a/packages/babel-core/src/config/index.ts +++ b/packages/babel-core/src/config/index.ts @@ -60,4 +60,3 @@ export function createConfigItem( return createConfigItemRunner.sync(target, options); } } -export { getExternalDependencies } from "./full"; \ No newline at end of file From ceb8667e640910f4258db83433d4529ce1371ca4 Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Fri, 26 Jun 2020 23:53:52 -0400 Subject: [PATCH 08/11] Properly register external dependencies --- packages/babel-cli/src/babel/util.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/babel-cli/src/babel/util.ts b/packages/babel-cli/src/babel/util.ts index 4110eccb9557..368b10d78f3a 100644 --- a/packages/babel-cli/src/babel/util.ts +++ b/packages/babel-cli/src/babel/util.ts @@ -113,12 +113,13 @@ function subtract(minuend: Set, subtrahend: Set): string[] { } const registerNewExternalDependencies = (() => { - let prevDeps = babel.getDependencies(); + let prevDeps = null; return (filePath: string) => { // make the file path absolute because // dependencies are registered with absolute file paths filePath = path.resolve(filePath); - const prevDepsForFile = prevDeps.get(filePath) || new Set(); + const prevDepsForFile = + prevDeps === null ? new Set() : prevDeps.get(filePath) || new Set(); const newDeps = babel.getDependencies(); const newDepsForFile = newDeps.get(filePath) || new Set(); const unwatchedDepsForFile = subtract(newDepsForFile, prevDepsForFile); From dcbb5ae1de6194df3c9681d47c29116ce2cd8ed5 Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Sat, 27 Jun 2020 01:45:58 -0400 Subject: [PATCH 09/11] Only watch external deps if watch cli option is present --- packages/babel-cli/src/babel/dir.ts | 3 +++ packages/babel-cli/src/babel/file.ts | 3 +++ packages/babel-cli/src/babel/util.ts | 12 +++++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/babel-cli/src/babel/dir.ts b/packages/babel-cli/src/babel/dir.ts index dc0f335d1245..238357cbacb4 100644 --- a/packages/babel-cli/src/babel/dir.ts +++ b/packages/babel-cli/src/babel/dir.ts @@ -152,6 +152,9 @@ export default async function ({ startTime = null; }, 100); + // Look at corresponding comment in file.js + if (cliOptions.watch) util.watchMode(); + if (!cliOptions.skipInitialBuild) { if (cliOptions.deleteDirOnStart) { util.deleteDir(cliOptions.outDir); diff --git a/packages/babel-cli/src/babel/file.ts b/packages/babel-cli/src/babel/file.ts index d494a2e709ef..17685e8fc82f 100644 --- a/packages/babel-cli/src/babel/file.ts +++ b/packages/babel-cli/src/babel/file.ts @@ -193,6 +193,9 @@ export default async function ({ } async function files(filenames: Array): Promise { + // We need to set watch mode before the initial compilation + // so external dependencies are registered during the first compilation pass. + if (cliOptions.watch) util.watchMode(); if (!cliOptions.skipInitialBuild) { await walk(filenames); } diff --git a/packages/babel-cli/src/babel/util.ts b/packages/babel-cli/src/babel/util.ts index 368b10d78f3a..bb4ca53237fa 100644 --- a/packages/babel-cli/src/babel/util.ts +++ b/packages/babel-cli/src/babel/util.ts @@ -76,7 +76,7 @@ export async function transform( }; const result = await babel.transformAsync(code, opts); - registerNewExternalDependencies(filename); + if (isWatchMode) watchNewExternalDependencies(filename); return result; } @@ -90,10 +90,16 @@ export async function compile( }; const result = await babel.transformFileAsync(filename, opts); - registerNewExternalDependencies(filename); + if (isWatchMode) watchNewExternalDependencies(filename); return result; } +let isWatchMode = false; + +export function watchMode() { + isWatchMode = true; +} + /** * Check if @param child is a child of @param parent * Both paths must be absolute/resolved. (No "..") @@ -112,7 +118,7 @@ function subtract(minuend: Set, subtrahend: Set): string[] { return diff; } -const registerNewExternalDependencies = (() => { +const watchNewExternalDependencies = (() => { let prevDeps = null; return (filePath: string) => { // make the file path absolute because From 344860bb17b76f7179232c3d3502745eb0b010e6 Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Sat, 27 Jun 2020 04:22:52 -0400 Subject: [PATCH 10/11] More comments --- packages/babel-cli/src/babel/util.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/babel-cli/src/babel/util.ts b/packages/babel-cli/src/babel/util.ts index bb4ca53237fa..06277338d27e 100644 --- a/packages/babel-cli/src/babel/util.ts +++ b/packages/babel-cli/src/babel/util.ts @@ -118,6 +118,10 @@ function subtract(minuend: Set, subtrahend: Set): string[] { return diff; } +/** + * Register new external dependencies with the file system + * watcher (chokidar). + */ const watchNewExternalDependencies = (() => { let prevDeps = null; return (filePath: string) => { @@ -158,6 +162,13 @@ const getWatcher = (() => { }; })(); +/** + * Call @param callback whenever a dependency (source file)/ + * external dependency (non-source file) changes. + * + * Handles mapping external dependencies to their corresponding + * dependencies. + */ export function onDependencyFileChanged( callback: (filename_: string | null) => Promise, sourceFilesAreCompiledIntoASingleFile: boolean, From 32cf26a5b59aa890f68add3390947a62eb58b339 Mon Sep 17 00:00:00 2001 From: Vedant Roy Date: Sat, 27 Jun 2020 21:08:05 -0400 Subject: [PATCH 11/11] remove unnecessary else Video-Doc-Id[b0]: babel/babel/1 --- packages/babel-cli/src/babel/util.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/babel-cli/src/babel/util.ts b/packages/babel-cli/src/babel/util.ts index 06277338d27e..c41c629c594c 100644 --- a/packages/babel-cli/src/babel/util.ts +++ b/packages/babel-cli/src/babel/util.ts @@ -192,11 +192,9 @@ export function onDependencyFileChanged( for (const dependent of externalFileDeps.get(filePath)) { await callback(dependent); } - await callback(filePath); } - } else { - await callback(filePath); } + return await callback(filePath); } ["add", "change"].forEach(type => getWatcher().on(type, onFileChanged)); }