Avoiding rebuilding every time the container restarts

Hi! My project has a build script that does all sorts of stuff like hashing assets, converting TypeScript, bundling, minifying etc etc.

I’m currently calling the build script in the start npm script, but this means it runs every time the container restarts.

The build script only needs to run if files have changed. Is there a way to achieve this?

It seems you want some sort of conditional functionality in your build pipeline. You might find Make to be quite useful for this.

Under the hood, Make is a simple rule-based script engine. One of the main highlights of Make is the ease with which you’re able to instruct it to do something when a file changes.

For example, take a look at one of the Makefiles I have for a project:

# Makefile

OUT := bin/www

# Collect any file dependencies
FILES := $(shell find src -name "*")

$(OUT): Makefile $(FILES)
	sh etc/build.sh

The Makefile declares the bin/www folder as the final target of the build script and assigns it at least one dependency: the Makefile itself. The FILES variable, when expanded, resolves to whatever files are found in the src directory; these files are then treated as dependencies themselves. If any one of the dependencies change (compared to the last successful build), Make will run the indented line—which itself executes another the main build script.

To clarify, I’m asking if Glitch differentiates “container restarted” and “container restarted due to file changes” in some meaningful way.

Hey @jaffathecake you can take a look at watch.json for some finer-grained control over what causes a project restart; we only completely restart the container itself every 12 hours. You could also put your compilation steps in the install script in package.json which would run those steps any time an install is required; watch.json can help you control that as well.

Hope this helps!

This feels like what I’m looking for, but I can’t find any docs for the install property of watch.json.

What is the default value for the install property? If I set an include value, am I overwriting that default, or is it being merged? Can there be an exclude property? Are there any other properties?

Sorry @jaffathecake, I think I was less than clear; the install script property is in package.json and tells your project what commands to run during the install process. watch.json can help you control which changes to files trigger the install script.

Both install and restart follow the same patterns in watch.json, so should support both includes and excludes. Your settings in a local watch,json file override whatever Glitch would normally do in these cases.

Hope this helps!

1 Like

Perfect, I’ll give that a spin.

Sorry to be a pain, but I’m still having trouble.

{
  "install": {
    "include": [".*"],
    "exclude": ["^\\.data/"]
  }
}

My intent here is "If any file changes, except those in the .data directory, reinstall (by running npm run install). If only files in .data change, or the app simply needs restarting, run npm run start.

However, if I change files, it only seems to restart, it doesn’t rebuild.

`.*’ regexp only matches files that starts with dot

+ .git/
+ .assets
+ .eslintrc.js
- index.ts
- files/

Try using src/*/**.

Also, why you need a rebuild after every change? Why don’t you use ts-node as you write on TypeScript?

It isn’t regexp then :grinning:. Is there any documentation on the actual format?

The build is doing more than just typescript conversion, such as hashing asset names, and bundling clientside JS.