docs/config/extensions/custom.mdx
Build extensions allow you to hook into the build system and customize the build process or the resulting bundle and container image (in the case of deploying). See our build extension overview for more information on how to install and use our built-in extensions. Build extensions can do the following:
Build extensions are added to your trigger.config.ts file, with a required name and optional build hook functions. Here's a simple example of a build extension that just logs a message when the build starts:
import { defineConfig } from "@trigger.dev/sdk";
export default defineConfig({
project: "my-project",
build: {
extensions: [
{
name: "my-extension",
onBuildStart: async (context) => {
console.log("Build starting!");
},
},
],
},
});
You can also extract that out into a function instead of defining it inline, in which case you will need to import the BuildExtension type from the @trigger.dev/build package:
import { defineConfig } from "@trigger.dev/sdk";
import { BuildExtension } from "@trigger.dev/build";
export default defineConfig({
project: "my-project",
build: {
extensions: [myExtension()],
},
});
function myExtension(): BuildExtension {
return {
name: "my-extension",
onBuildStart: async (context) => {
console.log("Build starting!");
},
};
}
This allows the extension to add additional dependencies to the list of externals for the build. This is useful for dependencies that are not included in the bundle, but are expected to be available at runtime.
import { defineConfig } from "@trigger.dev/sdk";
export default defineConfig({
project: "my-project",
build: {
extensions: [
{
name: "my-extension",
externalsForTarget: async (target) => {
return ["my-dependency"];
},
},
],
},
});
This hook runs before the build starts. It receives the BuildContext object as an argument.
import { defineConfig } from "@trigger.dev/sdk";
export default defineConfig({
project: "my-project",
build: {
extensions: [
{
name: "my-extension",
onBuildStart: async (context) => {
console.log("Build starting!");
},
},
],
},
});
If you want to add an esbuild plugin, you must do so in the onBuildStart hook. Here's an example of adding a custom esbuild plugin:
import { defineConfig } from "@trigger.dev/sdk";
export default defineConfig({
project: "my-project",
build: {
extensions: [
{
name: "my-extension",
onBuildStart: async (context) => {
context.registerPlugin({
name: "my-plugin",
setup(build) {
build.onLoad({ filter: /.*/, namespace: "file" }, async (args) => {
return {
contents: "console.log('Hello, world!')",
loader: "js",
};
});
},
});
},
},
],
},
});
You can use the BuildContext.target property to determine if the build is for dev or deploy:
import { defineConfig } from "@trigger.dev/sdk";
export default defineConfig({
project: "my-project",
build: {
extensions: [
{
name: "my-extension",
onBuildStart: async (context) => {
if (context.target === "dev") {
console.log("Building for dev");
} else {
console.log("Building for deploy");
}
},
},
],
},
});
This hook runs after the build completes. It receives the BuildContext object and a BuildManifest object as arguments. This is where you can add in one or more BuildLayer's to the context.
import { defineConfig } from "@trigger.dev/sdk";
export default defineConfig({
project: "my-project",
build: {
extensions: [
{
name: "my-extension",
onBuildComplete: async (context, manifest) => {
context.addLayer({
id: "more-dependencies",
dependencies,
});
},
},
],
},
});
See the addLayer documentation for more information on how to use addLayer.
Can either be dev or deploy, matching the CLI command name that is being run.
npx trigger.dev@latest dev # BuildTarget is "dev"
npx trigger.dev@latest deploy # BuildTarget is "deploy"
Resolves a path relative to the project's working directory.
<ParamField path="path" type="string"> The path to resolve. </ParamField>const resolvedPath = context.resolvePath("my-other-dependency");
commands: ["echo 'Hello, world!'"];
These commands are run after packages have been installed and the code copied into the container in the "build" stage of the Dockerfile. This means you cannot install system packages in these commands because they won't be available in the final stage. To do that, please use the pkgs property of the image object.
dependencies: {
"my-dependency": "^1.0.0",
};
Add a command that will echo the value of an environment variable:
context.addLayer({
id: "my-layer",
commands: [`echo $MY_ENV_VAR`],
build: {
env: {
MY_ENV_VAR: "Hello, world!",
},
},
});
When creating a build extension, you may run into issues with the build process. One thing that can help is turning on debug logging when running either dev or deploy:
npx trigger.dev@latest dev --log-level debug
npx trigger.dev@latest deploy --log-level debug
Another helpful tool is the --dry-run flag on the deploy command, which will bundle your project and generate the Containerfile (e.g. the Dockerfile) without actually deploying it. This can help you see what the final image will look like and debug any issues with the build process.
npx trigger.dev@latest deploy --dry-run
You should also take a look at our built in extensions for inspiration on how to create your own. You can find them in in the source code here.