@@ -17,6 +17,7 @@ import getModuleDependencies from './lib/getModuleDependencies'
17
17
import log from './util/log'
18
18
import packageJson from '../package.json'
19
19
import normalizePath from 'normalize-path'
20
+ import micromatch from 'micromatch'
20
21
import { validateConfig } from './util/validateConfig.js'
21
22
22
23
let env = {
@@ -837,6 +838,23 @@ async function build() {
837
838
}
838
839
839
840
let config = refreshConfig ( configPath )
841
+ let contentPatterns = refreshContentPatterns ( config )
842
+
843
+ /**
844
+ * @param {import('../types/config.js').RequiredConfig } config
845
+ * @return {{all: string[], dynamic: string[], static: string[]} }
846
+ **/
847
+ function refreshContentPatterns ( config ) {
848
+ let globs = extractFileGlobs ( config )
849
+ let tasks = fastGlob . generateTasks ( globs , { absolute : true } )
850
+ let dynamicPatterns = tasks . filter ( ( task ) => task . dynamic ) . flatMap ( ( task ) => task . patterns )
851
+ let staticPatterns = tasks . filter ( ( task ) => ! task . dynamic ) . flatMap ( ( task ) => task . patterns )
852
+
853
+ return {
854
+ all : [ ...staticPatterns , ...dynamicPatterns ] ,
855
+ dynamic : dynamicPatterns ,
856
+ }
857
+ }
840
858
841
859
if ( input ) {
842
860
contextDependencies . add ( path . resolve ( input ) )
@@ -867,6 +885,7 @@ async function build() {
867
885
env . DEBUG && console . time ( 'Resolve config' )
868
886
context = null
869
887
config = refreshConfig ( configPath )
888
+ contentPatterns = refreshContentPatterns ( config )
870
889
env . DEBUG && console . timeEnd ( 'Resolve config' )
871
890
872
891
env . DEBUG && console . time ( 'Watch new files' )
@@ -924,7 +943,14 @@ async function build() {
924
943
// Restore watching any files that are "removed"
925
944
// This can happen when a file is pseudo-atomically replaced (a copy is created, overwritten, the old one is unlinked, and the new one is renamed)
926
945
// TODO: An an optimization we should allow removal when the config changes
927
- watcher . on ( 'unlink' , ( file ) => watcher . add ( file ) )
946
+ watcher . on ( 'unlink' , ( file ) => {
947
+ file = normalizePath ( file )
948
+
949
+ // Only re-add the file if it's not covered by a dynamic pattern
950
+ if ( ! micromatch . some ( [ file ] , contentPatterns . dynamic ) ) {
951
+ watcher . add ( file )
952
+ }
953
+ } )
928
954
929
955
// Some applications such as Visual Studio (but not VS Code)
930
956
// will only fire a rename event for atomic writes and not a change event
@@ -935,11 +961,16 @@ async function build() {
935
961
return
936
962
}
937
963
938
- let watchedPath = path . resolve ( meta . watchedPath )
964
+ let watchedPath = meta . watchedPath
939
965
940
966
// Watched path might be the file itself
941
967
// Or the directory it is in
942
- filePath = watchedPath . endsWith ( filePath ) ? watchedPath : path . resolve ( watchedPath , filePath )
968
+ filePath = watchedPath . endsWith ( filePath ) ? watchedPath : path . join ( watchedPath , filePath )
969
+
970
+ // Skip this event since the files it is for does not match any of the registered content globs
971
+ if ( ! micromatch . some ( [ filePath ] , contentPatterns . all ) ) {
972
+ return
973
+ }
943
974
944
975
// Skip since we've already queued a rebuild for this file that hasn't happened yet
945
976
if ( pendingRebuilds . has ( filePath ) ) {
0 commit comments