Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch for changes to cached builds #324

Open
adamsanderson opened this issue Dec 30, 2020 · 1 comment
Open

Watch for changes to cached builds #324

adamsanderson opened this issue Dec 30, 2020 · 1 comment

Comments

@adamsanderson
Copy link

If I'm reading the code correctly, it looks like backfill will run and then exit, which is great for production builds.

One use case you might consider is to allow backfill to watch the package directory for changes if it serves up a cached build and then execute the build command so that it can be used with watch scripts. This allows people to use tools like wsrun to launch watchers for many packages in dev mode, but only actually spin up the watcher on either a cache miss or a local change.

I found this approach provided a good balance of performance and developer experience in a project where I wrote some one-off build caching scripts. On the other hand, this may be either a pathological use case or overly complicated.

@bweggersen
Copy link
Member

bweggersen commented Jan 19, 2021

Sorry for not replying to your great suggestion! It popped up again in my inbox when you closed the issue.

I think this can be solved with a simple script using backfill APIs.

We've had a similar concern in the past. We didn't have backfill at the time, our concern was more about starting watch processes in 50+ packages at the same time (this was before TS project references). We created a simple tool called sleep-until which would cause the watch process to "sleep" until the package was out of date, by comparing the oldest lib output and comparing it with the newest src. Something like:

    const [targetMtime, targetFilename] = findOldest(path.join(process.cwd(), target));
    const outdatedSource = findNewer(source, targetMtime);
    if (outdatedSource) {
        console.log(
            `Continuing: Found a source file (\`${path.relative(
                process.cwd(),
                outdatedSource
            )}\`) that's newer than the oldest target file \`${path.relative(process.cwd(), targetFilename)}\``
        );
        process.exit();
    } else {
        console.log(`Sleeping until changes in ${target}.`);
        fs.watch("src", (_event, filename) => {
            console.log("Something changed. Continuing: ", filename);
            process.exit();
        });
    }

You could change the logic here to include a cache check through backfill and then start the watch process as soon as the package is out of date.

The watch script in package.json would be something like:

"watch": "sleep-until src lib && tsc --watch"

@bweggersen bweggersen reopened this Jan 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants