Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nx and typia setup docs #900

Closed
renesass opened this issue Dec 8, 2023 · 13 comments
Closed

Nx and typia setup docs #900

renesass opened this issue Dec 8, 2023 · 13 comments
Labels
help wanted Extra attention is needed

Comments

@renesass
Copy link

renesass commented Dec 8, 2023

Hey @samchon,

I love typia and desperately want to integrate it with Nx (express app).
I tried all I could find

However, no matter what I do, I always get errors:

  • 'rootDir' is expected to contain all source files.
  • throw new Error(Could not find ${fileToRun}. Make sure your build succeeded.);

Since some people got it running, I would love to see the specific steps on how to set it up.
Here is my current setup:

/apps/api/project.json

{
  "name": "api",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/api/src",
  "projectType": "application",
  "targets": {
    "build": {
      "executor": "@nx/js:tsc",
      "outputs": ["{options.outputPath}"],
      "defaultConfiguration": "production",
      "options": {
        "target": "node",
        "compiler": "tsc",
        "outputPath": "dist/apps/api",
        "main": "apps/api/src/main.ts",
        "tsConfig": "apps/api/tsconfig.app.json",
        "assets": ["apps/api/src/assets"],
        "isolatedConfig": true,
        "webpackConfig": "apps/api/webpack.config.js"
      },
      "configurations": {
        "development": {},
        "production": {}
      }
    },
    "serve": {
      "executor": "@nx/js:node",
      "defaultConfiguration": "development",
      "options": {
        "buildTarget": "api:build"
      },
      "configurations": {
        "development": {
          "buildTarget": "api:build:development"
        },
        "production": {
          "buildTarget": "api:build:production"
        }
      }
    },

(I tried all kind of executors, specifically @nx/js:tsc and @nx/webpack:webpack)

apps/api/webpack.config.js

 const path = require('path')
const nodeExternals = require('webpack-node-externals')
const { composePlugins, withNx } = require('@nx/webpack')

module.exports = composePlugins(withNx(), (config) => {
  // Update the webpack config as needed here.
  // e.g. `config.plugins.push(new MyPlugin())`

  // CUSTOMIZE HERE
  config.entry = ['./src/main.ts']
  config.output = {
    path: path.join(__dirname, 'dist'),
    filename: 'main.js',
  }
  config.optimization = {
    minimize: false,
  }

  // JUST KEEP THEM
  config.mode = 'development'
  config.target = 'node'
  config.module = {
    rules: [
      {
        test: /\.ts$/,
        exclude: /node_modules/,
        loader: 'ts-loader',
      },
    ],
  }
  config.resolve = {
    extensions: ['.tsx', '.ts', '.js'],
  }

  return config
})

(basically merged nx default but also tried the provided webpack.config.js from the docs)

tsconfig.base.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "rootDir": ".",
    "moduleResolution": "node",
    "strict": true,
    "strictNullChecks": true,
    "noUncheckedIndexedAccess": true,
    "forceConsistentCasingInFileNames": true,
    "stripInternal": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,
    "module": "commonjs",
    "target": "ES2022",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "typeRoots": ["node_modules/@types"],
    "skipLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "@XXX/config": ["libs/config/src/main.ts"],
      "@XXX/services": ["libs/services/src/index.ts"]
    }
  },
  "ignorePatterns": [".eslintrc.cjs", "vite.config.ts"],
  "transformers": ["typia/lib/transform"]
}

I'm looking forward to any answer!
Best,
René

@renesass
Copy link
Author

renesass commented Dec 8, 2023

With this option

"targets": {
    "build": {
      "executor": "@nx/webpack:webpack",
      "outputs": ["{options.outputPath}"],
      "defaultConfiguration": "production",
      "options": {
        "target": "node",
        "compiler": "tsc",
        "outputPath": "dist/apps/api",
        "main": "apps/api/src/main.ts",
        "tsConfig": "apps/api/tsconfig.app.json",
        "assets": ["apps/api/src/assets"],
        "isolatedConfig": true,
        "webpackConfig": "apps/api/webpack.config.js"
      },
      "configurations": {
        "development": {},
        "production": {}
      }
    },
    "serve": {
      "executor": "@nx/js:node",
      "defaultConfiguration": "development",
      "options": {
        "buildTarget": "api:build"
      },
      "configurations": {
        "development": {
          "buildTarget": "api:build:development"
        },
        "production": {
          "buildTarget": "api:build:production"
        }
      }
    }

I can at least build it but I can not serve the app:

  • throw new Error(`Could not find ${fileToRun}. Make sure your build succeeded.
  • Error: write EPIPE
  • node_modules/fork-ts-checker-webpack-plugin/lib/rpc/expose-rpc.js:5:58)

@samchon
Copy link
Owner

samchon commented Dec 19, 2023

As I'm not using NX at all, have no insight about it.

If there's some NX user, then please help him.

@samchon samchon added the help wanted Extra attention is needed label Dec 19, 2023
@loucass003
Copy link

@renesass Did you ever got this to work?

@renesass
Copy link
Author

Unfortunately not :( I tried for too long and then give up switching to typebox. Maybe you have better luck?

@loucass003
Copy link

Unfortunately not :( I tried for too long and then give up switching to typebox. Maybe you have better luck?

no I had to give up on NX too. I ended up using turborepo and it required no changes in the build setup, maybe you would be interested into that?

@samchon
Copy link
Owner

samchon commented Jan 22, 2024

This way is not working? @loucass003 @renesass

https://nestia.io/docs/setup/#nx

@loucass003
Copy link

loucass003 commented Jan 22, 2024

@samchon Sadly no, according to samchon/nestia#319 (comment)
it looks like the transformers are not compatible with Nx anymore

I tried myself for few hours and never got it to work 😭

@kakasoo
Copy link
Contributor

kakasoo commented Apr 11, 2024

I also tried to write a test code for type using packages managed by my company, but it failed. It seems to be an nx problem.
Unfortunately, I also have no understanding of nx.

@honguyenhaituan
Copy link

I make the newest version nx (18.2.4) run with nestia in this example repo. See commit: honguyenhaituan/nx-typia@c1df98c

@kakasoo
Copy link
Contributor

kakasoo commented Apr 15, 2024

I'm going to install it with this version and try it again. However, if the problem is reproduced every time the nx version is updated, I can consider removing it in the future.
I will share the results later.

@KyleBastien
Copy link
Contributor

KyleBastien commented Nov 22, 2024

Okay here's what I found which works with Nx v20 and Typia v6.

  1. In package.json you need to have a prepare script which is something like: ts-patch install && typia patch. This ensures that TypeScript is patched with ts-patch when Nx uses it later.
  2. Update your tsconfig.lib.json file in your @nx/js package to include the compilerOption so it looks something like the following:
{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "../../dist/out-tsc",
    "declaration": true,
    "types": [],
    "plugins": [{ "transform": "typia/lib/transform" }]
  },
  "include": ["**/*.ts"],
  "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"]
}
  1. When you run the standard nx <package-name>:build it should now output with the Typia transforms applied. But if Typia fails for any reasons (for example it considers some type you have to be invalid), this error is not reported back via Nx. Nx will silent swallow these errors from ts-patch/typia, and you will just not get the output you expect. I figured this out by creating a new task in my project.json file similar to:
"build:validate:typia": {
  "executor": "nx:run-commands",
  "options": {
    "commands": [
      "tsc --project packages/<package-name>/tsconfig.lib.json --outDir dist/packages/typiaTest"
    ],
    "parallel": false
  }
},

Running this task, showed me the errors from Typia, and once I corrected them, everything worked using the standard build task the way I expected.

Note: While Nx has a transformers feature on the @nx/js plugin, that won't work with Typia. The reason there is because Nx is expecting a transformer to export a before hook, which Nx then plugs directly into TypeScript via the compiler API. Typia doesn't export that kind of hook, because Typia (per my understanding) only works with ts-patch, which abstracts the need for creating a specific before hook in the way Nx wants.

I hope this helps someone!

@samchon
Copy link
Owner

samchon commented Nov 22, 2024

@KyleBastien Can you send a PR of documentation in typia and nestia?

If you're busy, I'll to it by myself after two weeks later.

@KyleBastien
Copy link
Contributor

@samchon Made the two PR's here:

#1373
samchon/nestia#1110

@samchon samchon closed this as completed Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants