@@ -49,6 +49,9 @@ export interface WatcherOptions {
4949 cwd ?: string ;
5050}
5151
52+ const NODE_MODULES = 'node_modules' ;
53+ const NODE_MODULES_SEGMENT = `/${ NODE_MODULES } /` ;
54+
5255// Watch workspace for package manager changes
5356const packageWatchFiles = [
5457 // manifest can affect module resolution
@@ -109,7 +112,7 @@ export async function setupWatcher(options: SetupWatcherOptions): Promise<BuildW
109112 // Ignore all node modules directories to avoid excessive file watchers.
110113 // Package changes are handled below by watching manifest and lock files.
111114 // NOTE: this is not enabled when preserveSymlinks is true as this would break `npm link` usages.
112- ignored . push ( ' **/node_modules /**' ) ;
115+ ignored . push ( ` **/${ NODE_MODULES } /**` ) ;
113116 }
114117
115118 const watcher = await createWatcher ( {
@@ -386,6 +389,36 @@ export function isPathInside(file: string, dir: string): boolean {
386389 return file . startsWith ( dirWithSlash ) ;
387390}
388391
392+ /**
393+ * Extracts the package directory inside node_modules for a given POSIX path.
394+ *
395+ * Input Expectations:
396+ * - `posixPath` must be a normalized POSIX-style path (using forward slashes '/').
397+ */
398+ export function extractNodeModulesPackageDir ( posixPath : string ) : string | undefined {
399+ const index = posixPath . lastIndexOf ( NODE_MODULES_SEGMENT ) ;
400+ if ( index === - 1 ) {
401+ return undefined ;
402+ }
403+
404+ const nodeModulesPath = posixPath . slice ( 0 , index + NODE_MODULES_SEGMENT . length - 1 ) ;
405+ const remainder = posixPath . slice ( index + NODE_MODULES_SEGMENT . length ) ;
406+ const segments = remainder . split ( '/' ) ;
407+ if ( segments . length === 0 || ! segments [ 0 ] ) {
408+ return undefined ;
409+ }
410+
411+ if ( segments [ 0 ] . startsWith ( '@' ) ) {
412+ if ( segments . length < 2 || ! segments [ 1 ] ) {
413+ return undefined ;
414+ }
415+
416+ return `${ nodeModulesPath } /${ segments [ 0 ] } /${ segments [ 1 ] } ` ;
417+ }
418+
419+ return `${ nodeModulesPath } /${ segments [ 0 ] } ` ;
420+ }
421+
389422class ParcelExternalManager {
390423 private readonly extraSubscriptions = new Map < string , ParcelWatcher . AsyncSubscription > ( ) ;
391424 private readonly pendingSubscriptions = new Map <
@@ -647,15 +680,69 @@ async function createChokidarWatcher(
647680 const rootDirPosix = toPosixPathNormalized ( rootDir ) ;
648681 const rootDirLookupKey = toLookupKey ( rootDirPosix , isCaseSensitive ) ;
649682
650- const ignored = options ?. ignored ?. map ( ( pattern ) => {
651- if ( / [ * ? [ \] { } ( ) ] / . test ( pattern ) ) {
652- const isMatch = picomatch ( pattern , { dot : true } ) ;
683+ const allowedNodeModulesPackages = new Map < string , Set < string > > ( ) ;
653684
654- return ( filePath : string ) => isMatch ( toPosixPathNormalized ( filePath ) ) ;
685+ const registerPackage = ( posixPath : string , lookupKey : string ) : string | undefined => {
686+ const packageDir = extractNodeModulesPackageDir ( posixPath ) ;
687+ if ( ! packageDir ) {
688+ return undefined ;
655689 }
656690
657- return { path : toPosixPathNormalized ( pattern ) , recursive : true } ;
658- } ) ;
691+ const packageKey = toLookupKey ( packageDir , isCaseSensitive ) ;
692+ let watchedInPkg = allowedNodeModulesPackages . get ( packageKey ) ;
693+ if ( ! watchedInPkg ) {
694+ watchedInPkg = new Set ( ) ;
695+ allowedNodeModulesPackages . set ( packageKey , watchedInPkg ) ;
696+ watchedInPkg . add ( lookupKey ) ;
697+
698+ return packageDir ;
699+ }
700+
701+ watchedInPkg . add ( lookupKey ) ;
702+
703+ return undefined ;
704+ } ;
705+
706+ const isNodeModulesIgnored = ( filePath : string ) : boolean => {
707+ // Fast path: avoid expensive normalization for non-node_modules files
708+ if ( ! filePath . includes ( NODE_MODULES ) ) {
709+ return false ;
710+ }
711+
712+ const posixPath = toPosixPathNormalized ( filePath ) ;
713+ const lookupKey = toLookupKey ( posixPath , isCaseSensitive ) ;
714+
715+ if ( ! `${ lookupKey } /` . includes ( NODE_MODULES_SEGMENT ) ) {
716+ return false ;
717+ }
718+
719+ if ( allowedNodeModulesPackages . has ( lookupKey ) ) {
720+ return false ;
721+ }
722+
723+ for ( const pkg of allowedNodeModulesPackages . keys ( ) ) {
724+ if ( isPathInside ( lookupKey , pkg ) ) {
725+ return false ;
726+ }
727+ }
728+
729+ return true ;
730+ } ;
731+
732+ const customIgnored =
733+ options ?. ignored
734+ ?. filter ( ( pattern ) => ! pattern . includes ( NODE_MODULES ) )
735+ . map ( ( pattern ) => {
736+ if ( / [ * ? [ \] { } ( ) ] / . test ( pattern ) ) {
737+ const isMatch = picomatch ( pattern , { dot : true } ) ;
738+
739+ return ( filePath : string ) => isMatch ( toPosixPathNormalized ( filePath ) ) ;
740+ }
741+
742+ return { path : toPosixPathNormalized ( pattern ) , recursive : true } ;
743+ } ) ?? [ ] ;
744+
745+ const ignored = [ ...customIgnored , isNodeModulesIgnored ] ;
659746
660747 const watcher = chokidar . watch ( rootDir , {
661748 ignoreInitial : true ,
@@ -714,7 +801,11 @@ async function createChokidarWatcher(
714801 const lookupKey = toLookupKey ( posixPath , isCaseSensitive ) ;
715802 if ( ! watchedFiles . has ( lookupKey ) ) {
716803 watchedFiles . add ( lookupKey ) ;
717- if ( ! isPathInside ( lookupKey , rootDirLookupKey ) && lookupKey !== rootDirLookupKey ) {
804+
805+ const newPkgDir = registerPackage ( posixPath , lookupKey ) ;
806+ if ( newPkgDir ) {
807+ newPaths . push ( newPkgDir ) ;
808+ } else if ( ! isPathInside ( lookupKey , rootDirLookupKey ) && lookupKey !== rootDirLookupKey ) {
718809 newPaths . push ( posixPath ) ;
719810 }
720811 }
@@ -732,7 +823,19 @@ async function createChokidarWatcher(
732823 const lookupKey = toLookupKey ( posixPath , isCaseSensitive ) ;
733824 if ( watchedFiles . has ( lookupKey ) ) {
734825 watchedFiles . delete ( lookupKey ) ;
735- if ( ! isPathInside ( lookupKey , rootDirLookupKey ) && lookupKey !== rootDirLookupKey ) {
826+
827+ const packageDir = extractNodeModulesPackageDir ( posixPath ) ;
828+ if ( packageDir ) {
829+ const packageKey = toLookupKey ( packageDir , isCaseSensitive ) ;
830+ const watchedInPkg = allowedNodeModulesPackages . get ( packageKey ) ;
831+ if ( watchedInPkg ) {
832+ watchedInPkg . delete ( lookupKey ) ;
833+ if ( watchedInPkg . size === 0 ) {
834+ allowedNodeModulesPackages . delete ( packageKey ) ;
835+ removePaths . push ( packageDir ) ;
836+ }
837+ }
838+ } else if ( ! isPathInside ( lookupKey , rootDirLookupKey ) && lookupKey !== rootDirLookupKey ) {
736839 removePaths . push ( posixPath ) ;
737840 }
738841 }
0 commit comments