@@ -31,6 +31,7 @@ module.exports.bootstrap = async ({ setPluginContext, options, refresh }) => {
31
31
const stackbitYamlPath = path . resolve ( stackbitYamlFileName ) ;
32
32
const stackbitYamlExists = await fse . pathExists ( stackbitYamlPath ) ;
33
33
if ( stackbitYamlExists ) {
34
+ log ( 'loading stackbit.yaml and models...' ) ;
34
35
const { models, stackbitYaml } = await loadStackbitYaml ( stackbitYamlPath )
35
36
setPluginContext ( { models, stackbitYaml } ) ;
36
37
@@ -52,18 +53,36 @@ module.exports.bootstrap = async ({ setPluginContext, options, refresh }) => {
52
53
setPluginContext ( { files } ) ;
53
54
54
55
if ( watch ) {
55
- const update = async ( eventName , path ) => {
56
- log ( `file '${ path } ' has been ${ eventName } , reloading files...` ) ;
56
+ let changedFilePaths = [ ] ;
57
57
58
- if ( path === stackbitYamlFileName ) {
59
- const { models, stackbitYaml } = loadStackbitYaml ( stackbitYamlPath )
58
+ // don't call refresh on every file change, as multiple files could be written at once.
59
+ // instead, debounce the update function for 50ms, up to 200ms
60
+ const debouncedUpdate = _ . debounce ( async ( ) => {
61
+ log ( `reload files and refresh sourcebit plugins...` ) ;
62
+
63
+ const filePathsCopy = changedFilePaths . slice ( ) ;
64
+ changedFilePaths = [ ] ;
65
+
66
+ if ( filePathsCopy . includes ( stackbitYamlFileName ) ) {
67
+ log ( 'reloading stackbit.yaml and models...' ) ;
68
+ const { models, stackbitYaml } = await loadStackbitYaml ( stackbitYamlPath )
60
69
setPluginContext ( { models, stackbitYaml } ) ;
61
- } else {
70
+ }
71
+
72
+ if ( _ . some ( filePathsCopy , filePath => filePath !== stackbitYamlFileName ) ) {
73
+ log ( 'reloading content files...' ) ;
62
74
const files = await readFiles ( sources ) ;
75
+ log ( `loaded ${ files . length } files` ) ;
63
76
setPluginContext ( { files } ) ;
64
77
}
65
78
66
79
refresh ( ) ;
80
+ } , 50 , { maxWait : 200 } ) ;
81
+
82
+ const update = async ( eventName , filePath ) => {
83
+ log ( `file '${ filePath } ' has been ${ eventName } , reloading files...` ) ;
84
+ changedFilePaths . push ( filePath ) ;
85
+ await debouncedUpdate ( ) ;
67
86
} ;
68
87
69
88
const watchPaths = _ . map ( sources , _ . property ( 'path' ) ) ;
0 commit comments