TypeScript build process

One of the framework's goals is to provide first-class support for TypeScript. This goes beyond the static types and IntelliSense you can enjoy while writing the code.

We also ensure that you never have to install any additional build tools to compile your code during development or for production.

This guide assumes that you have some knowledge about TypeScript and the build tools ecosystem.

Common bundling approaches

Following are some of the common approaches for developing a Node.js application with TypeScript.

Using tsc

The simplest way to compile your TypeScript code to JavaScript is using the official tsc command line.

Using ts-node

Ts node does improve the development experience, as it compiles code in memory and does not output it on the disk. You can combine tsnode and nodemon and run your typescript code as a first-class citizen.

However, for larger applications, tsnode may get slow as it has to recompile the entire project on every file change. In contrast, tsc was re-building only the changed file.

Do note, tsnode is a development only tool. For production, you still have to compile your code to JavaScript using tsc and write custom scripts to copy static files.

Using Webpack

After trying the above approaches, you may decide to give Webpack a try. Webpack is a build tool and has a lot to offer. But, it comes with its own set of downsides.

AdonisJS approach

We are not a big fan of over-complicated build tools and bleeding-edge compilers. Having a calm development experience is way more valuable than exposing config to tune every single knob.

We started with the following set of goals.

In-memory development compiler

Similar to ts-node, we created @adonisjs/require-ts module. It uses the TypeScript compiler API, meaning all the TypeScript features works, and your tsconfig.json file is the single source of truth.

However, @adonisjs/require-ts is slightly different from ts-node in the following ways.

Every time you run node ace serve --watch, we start the HTTP server along with the in-memory compiler and use nodemon to watch for the file changes.

Standalone production builds

You build your code production by running the node ace build --production command. It performs the following operations.

Why do we call it a standalone build?

After running the build command, the output folder has everything you need to deploy your application in production.

You can copy the build folder without your TypeScript source code, and your application will work just fine.

Creating a standalone build folder does help in reducing the size of code that you deploy on your production server. This is usually helpful when you package your app as a Docker image. There is no need to have both the source and build output in your Docker image and keep it lightweight.

Points to keep in mind