samchon / typia

Super-fast/easy runtime validators and serializers via transformation
https://typia.io/
MIT License
4.61k stars 158 forks source link

Nx and typia setup docs #900

Open renesass opened 11 months ago

renesass commented 11 months ago

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:

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 commented 11 months ago

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:

samchon commented 10 months ago

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

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

loucass003 commented 9 months ago

@renesass Did you ever got this to work?

renesass commented 9 months ago

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

loucass003 commented 9 months ago

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 commented 9 months ago

This way is not working? @loucass003 @renesass

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

loucass003 commented 9 months ago

@samchon Sadly no, according to https://github.com/samchon/nestia/issues/319#issuecomment-1828180666 it looks like the transformers are not compatible with Nx anymore

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

kakasoo commented 7 months ago

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 commented 7 months ago

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

kakasoo commented 7 months ago

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.