How can I speed up Typescript compilation for my generated code?

During combined UI and DAML model development I often have to recompile DAML, regenerate the Typescript libraries and restart the sandbox. By far the most time is spent compiling the 20-odd Typescript projects generated from the codegen. Is there any trick or setting in tsconfig.json that I can use to speed up things? Maybe disabling some optimization or similar. The whole cycle is around 3mins long, which is not optimal for rapid development.

For everyone’s benefit, here is my handy Makefile, which at least makes invoking the cycle just a make:

build:
  daml build
  daml codegen ts -o daml2ts -p package.json .daml/dist/*.dar
  yarn workspaces run build
  daml start --start-navigator='no'
1 Like

If you’re running on TypeScript 3.4+ you can probably try to enable incremental compilation as in the following example.

// tsconfig.json
{
    "compilerOptions": {
        "incremental": true,
        "outDir": "./lib"
    },
    "include": ["./src"]
}
2 Likes

The problem seems to be that the tsconfig.json is generated as well, so it would need to be an option during code generation. @shaynefletcher maybe you have a view if that would yield a benefit. The other option would be not to regenerate the dependencies that didn’t change, such that if I change my own model only that would be regenerated. This would considerably improve the workflow.

4 Likes

Making daml codegen js only regenerate the packages that have changed is definitely something that we want to do! I don’t have a great solution in the meantime sadly.

1 Like

If it helps, I believe this was compiling pretty fast (within less than a minute) before a recent change to the DAML UI Template

1 Like

@manishGrover yes, that’s because the UI template was updated to use the JS codegen recently.

1 Like

I found a workable solution:

After you’ve run yarn workspaces run build once, you’ll only need to recompile the one project that contains the generated code from your own DAML models. This is my new Makefile which works nicely:

build:
  daml build
  daml codegen ts -o daml2ts -p package.json .daml/dist/*.dar
  cd daml2ts/my-daml-model-0.0.1 && yarn build
  cd ui && yarn build
  daml start --start-navigator='no'

You’ll need to rerun yarn workspaces run build only when you upgrade the SDK version as then the generated packages for the standard libraries will be different.

1 Like

I believe this approach no longer works in the release candidate for 1.0 since daml2ts now produces JavaScript + TypeScript typings which means that it takes care of the compilation as part of running daml codegen ts.

1 Like

argh :man_facepalming: and there I was, happily accepting my answer…

In return for crushing my dreams, I’ve create this ticket for you :wink:

1 Like

Thanks, sorry for crushing your dreams :smiley:

1 Like

I couldn’t accept the fact that I’ve crushed your dreams so I’ve merged a PR yesterday that speeds up the JS codegen significantly (it now takes ~0.5s on the GSG as opposed to > 20s)

2 Likes

:clap::muscle: Generating JS directly, thus skipping TS compilation entirely?

Exactly we generate JS + typescript typings so from a user perspective you get the same output but we don’t have to run the typescript compiler which is super slow.