Nest-cli: Support for "build" option for TypeScript project references?

Created on 1 Oct 2019  路  9Comments  路  Source: nestjs/nest-cli

Does the CLi support the build flag (-b for tsc)? It seems not.... I'm trying to setup a project with TypeScript project references... any help?

$ nest build --help
Usage: nest build [options] [app]

Build Nest application

Options:
  -p, --path [path]     Path to tsconfig file
  -w, --watch           Run in watch mode (live-reload)
  --webpack             Use webpack for compilation
  --webpackPath [path]  Path to webpack configuration
  -h, --help            output usage information

blocked

Most helpful comment

Are there any changes on this?

All 9 comments

I'm not sure whether it makes sense to use TS project references in combination with Nest CLI. Can you share a sample repository?

Hi! Thanks for the reply. Sure, here is an example: https://github.com/gremo/nestangular-boilerplate

As you can see in packages/server/package.json I'm still using the "old" scripts (tsc, ts-node, tsc-watch). Scripts has been all modified to use the -b option. But I'd like to switch to Nest CLI commands. In order to do so, it should support TypeScript project references.

Why I'm using TypeScript project references? That's simple. That is a Yarn workspace project here directory packages/server holds the NestJS application, packages/shared holds common utilities and domain models, while packages/client is an Angular 8 application.

With Yarn workspaces and TS project references I can manage dependencies and output "structure" very easily.

Thanks!

There are no docs for TypeScript Compiler API with Project references so far. I'd recommend sticking with the existing configuration for now.

What do you mean with "existing configuration for now"?

With tsc & tsc-watch. Migration to NestJS CLI is not required, there are no breaking changes

In the latest version of the CLI (^6.10.x), we support project references partially, similarly to this issue: https://github.com/TypeStrong/ts-loader/issues/851#issue-367532340

Copy & pasted from ts-loader (and slightly modified):

Using project references currently requires building referenced projects outside of Nest CLI.

Nest CLI has partial support for project references in that it will _load_ dependent composite projects that are already built, but will not currently _build/rebuild_ those upstream projects. The best way to explain exactly what this means is through an example. Say you have a project with a project reference pointing to the lib/ directory:

tsconfig.json
lib/
  tsconfig.json
  niftyUtil.ts

And we鈥檒l assume that the root tsconfig.json has { "references": { "path": "lib" } }, which means that any import of a file that鈥檚 part of the lib sub-project is treated as a reference to another project, not just a reference to a TypeScript file. Before discussing how Nest handles this, it鈥檚 helpful to review at a really basic level what tsc itself does here. If you were to run tsc on this tiny example project, the build would fail with the error:

error TS6305: Output file 'lib/niftyUtil.d.ts' has not been built from source file 'lib/niftyUtil.ts'.

Using project references actually instructs tsc _not_ to build anything that鈥檚 part of another project from source, but rather to look for any .d.ts and .js files that have already been generated from a previous build. Since we鈥檝e never built the project in lib before, those files don鈥檛 exist, so building the root project fails. Still just thinking about how tsc works, there are two options to make the build succeed: either run tsc -p lib/tsconfig.json _first_, or simply run tsc --build, which will figure out that lib hasn鈥檛 been built and build it first for you.

TEMPORARY SOLUTION

First, run tsc -b manually. Once the compilation is complete, you'll be able to run nest build without errors

@kamilmysliwiec thank you a lot. I don't like to build with tsc before building with nest. I'll stick with the "old" scripts.

Are there any changes on this?

Just wrote a workaround on this problem for you guys on Yarn Workspaces:

  1. Create a "buildReferences.js" file at the root of the package you want to build the references for:
const typescript = require('typescript')
const { exec } = require('child_process')

const tsconfig = typescript.readConfigFile('tsconfig.json', typescript.sys.readFile)
const references = tsconfig.config.references;

references.forEach(({ path }) => {
  exec(`tsc --project ${path}`, () => {
    console.log(`BUILT REFERENCE: ${path}`);
  })
});
  1. Add the following scripts to your package.json file named as followed or "prewhateverscriptyouhave":
  {
    "references:build": "node buildReferences.js",
    "prebuild": "yarn references:build",
    "predev": "yarn references:build"
  }

Might not be the fastest option out there, but I think is better than running "tsc" twice every time.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

METACEO picture METACEO  路  6Comments

alexbuczynsky picture alexbuczynsky  路  5Comments

jylin picture jylin  路  5Comments

victordidenko picture victordidenko  路  4Comments

bovandersteene picture bovandersteene  路  5Comments