Configure tasks with inputs, outputs, dependencies, and smart caching
The primary focus of moon is a task runner. In moon, a task is a binary or system command that is run as a child process within the context of a project. Tasks are defined per project with moon.yml, or inherited by many projects with .moon/tasks/**/*.
Most projects utilize the same core tasks: linting, testing, code formatting, typechecking, and building. Let’s implement a build task within a project.Begin by creating the moon.yml file at the root of a project and add build to the tasks field with a command parameter:
Our task above works, but isn’t very efficient as it always runs, regardless of what has changed since the last time it ran. This becomes problematic in CI environments and locally.To mitigate this, moon provides inputs - file paths, globs, and environment variables that are used by the task. Moon will compare these inputs to calculate whether to run or return the previous run state from cache.Let’s expand the task with the inputs setting:
Outputs are the opposite of inputs - they are files and folders created as a result of running the task. Outputs are optional, as not all tasks require them, and the ones that do are typically build-related.Declaring outputs is important for incremental builds and smart caching! When moon encounters a build that has already been built, it hydrates all necessary outputs from the cache, then immediately exits. No more waiting for long builds!Let’s expand our task with the outputs setting:
For scenarios where you need to run a task before another task, use the deps setting with targets:
<project>:<task> - Full canonical target
~:<task> or <task> - A task within the current project
^:<task> - A task from all depended on projects
moon.yml
dependsOn: - 'ui-library'tasks: build: command: 'webpack build' deps: # Run the ui-library build before this build - '^:build' start: command: 'node server.js' deps: # Run the build task in this project first - 'build'
Once you’re familiar with configuring tasks, you may notice certain inputs being repeated. To reduce boilerplate, moon provides file groups, which enable grouping of similar file types: