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

Any idea on how to use nestjs cli plugin with nx? #4135

Closed
JeetChaudhari opened this issue Nov 23, 2020 · 13 comments
Closed

Any idea on how to use nestjs cli plugin with nx? #4135

JeetChaudhari opened this issue Nov 23, 2020 · 13 comments

Comments

@JeetChaudhari
Copy link

I want to use nestjs cli plugin as mentioned at https://docs.nestjs.com/graphql/cli-plugin#using-the-cli-plugin. As my nx workspace does not contain nest-cli.json, I am not sure how to use this.

Also If I manually create nest-cli.json file it seems to have no effect. At https://nx.dev/latest/node/workspace/configuration#schematics it is mentioned that we can configure options from here but I am not sure how to do it.

Can anyone help me with this please? One more thing, I could not add question here as slack link is broken on add new issue page.

@JeetChaudhari
Copy link
Author

I was able to solve this using #2147 (comment), Just replaced swagger with graphql as
from return { before: [require('@nestjs/swagger/plugin').before({}, program)] };
to return { before: [require('@nestjs/graphql/plugin').before({}, program)] };
saved file as "webpack.config.js" and added it to "workspace.json" as

/workspace.json

"projects": {
  "api": {
    "architect": {
      "build": {
        "options": {
          "webpackConfig": "apps/api/webpack.config.js"
        }
      }
    }
  }
}

/apps/api/webpack.config.js

module.exports = (config, _context) => {
  const tsLoader = config.module.rules.find((r) =>
    r.loader.includes("ts-loader")
  );

  if (tsLoader) {
    tsLoader.options.transpileOnly = false;
    tsLoader.options.getCustomTransformers = (program) => {
      return {
        before: [
          require("@nestjs/graphql/plugin").before(
            {
              // Options: typeFileNameSuffix, introspectComments
            },
            program
          ),
        ],
      };
    };
  }

  return config;
};

@crohit92
Copy link

I created my workspace with nx and was looking for the same issue.
I got it solved by adding

"tsPlugins": [
    {
      "name": "@nestjs/swagger/plugin",
      "options": {
        "classValidatorShim": false,
        "introspectComments": true
      }
    }
 ]

in my angular.json (Since my workspace was NestJS + Angular)
under projects->api->architect->build->options

@kirylvarykau
Copy link

I created my workspace with nx and was looking for the same issue. I got it solved by adding

"tsPlugins": [
    {
      "name": "@nestjs/swagger/plugin",
      "options": {
        "classValidatorShim": false,
        "introspectComments": true
      }
    }
 ]

in my angular.json (Since my workspace was NestJS + Angular) under projects->api->architect->build->options

Hi!
I don't understand where to put this piece of code?
Could you explain it, please?
Should I put it into my nx.json ? Or it should be something else ? Thanks

@aviggiano
Copy link

hi @kirillvarikow and others with this issue

You should put it under the nx project.json file

Like this, entire file:

{
  "root": "packages/backend",
  "sourceRoot": "packages/backend/src",
  "projectType": "application",
  "targets": {
    "build": {
      "executor": "@nrwl/node:webpack",
      "outputs": ["{options.outputPath}"],
      "options": {
        "outputPath": "dist/packages/backend",
        "main": "packages/backend/src/main.ts",
        "tsConfig": "packages/backend/tsconfig.app.json",
        "tsPlugins": [
          {
            "name": "@nestjs/swagger/plugin",
            "options": {
              "classValidatorShim": false,
              "introspectComments": true
            }
          }
        ],
        "assets": ["packages/backend/src/common/assets"]
      },
      "configurations": {
        "production": {
          "optimization": true,
          "extractLicenses": true,
          "inspect": false
        }
      }
    },
    "serve": {
      "executor": "@nrwl/node:node",
      "options": {
        "buildTarget": "backend:build"
      }
    },
    "lint": {
      "executor": "@nrwl/linter:eslint",
      "outputs": ["{options.outputFile}"],
      "options": {
        "lintFilePatterns": ["packages/backend/**/*.ts"]
      }
    },
    "test": {
      "executor": "@nrwl/jest:jest",
      "outputs": ["coverage/packages/backend"],
      "options": {
        "jestConfig": "packages/backend/jest.config.js",
        "passWithNoTests": true,
        "codeCoverage": true,
        "coverageDirectory": "coverage/packages/backend"
      }
    }
  },
  "tags": []
}

@KevinOsorioCodes
Copy link

HI! the @nrwl/nest plugin has the nest cli built in, you just have to use the commands like this:
nx g @nrwl/nest:<commandName> <finalName> --project=<projectName>

ex: nx g @nrwl/nest:resource client --project=api

Here the docs: https://nx.dev/packages/nest#nest-generators

I hope it works for you!

@eliotis
Copy link

eliotis commented Oct 9, 2022

Is this solution for @nestjs/graphql/plugin still working for you because for me after nx ver 14 it stopped.

Any help?

@tagplus5
Copy link
Contributor

tagplus5 commented Oct 26, 2022

Example project.json with @nestjs/swagger plugin

{
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/api/src",
  "projectType": "application",
  "targets": {
    "build": {
      "executor": "@nrwl/node:webpack",
      "outputs": ["{options.outputPath}"],
      "options": {
        "outputPath": "dist/apps/api",
        "main": "apps/api/src/main.ts",
        "tsConfig": "apps/api/tsconfig.app.json",
        "tsPlugins": [
          "@nestjs/swagger/plugin"
        ],
        "assets": [
          "apps/api/src/assets"
        ],
        "generatePackageJson": true
      },
      "configurations": {
        "production": {
          "optimization": true,
          "extractLicenses": true,
          "inspect": false,
          "fileReplacements": [
            {
              "replace": "apps/api/src/environments/environment.ts",
              "with": "apps/api/src/environments/environment.prod.ts"
            }
          ]
        }
      }
    },
    "serve": {
      "executor": "@nrwl/node:node",
      "options": {
        "buildTarget": "api:build"
      }
    },
    "lint": {
      "executor": "@nrwl/linter:eslint",
      "outputs": ["{options.outputFile}"],
      "options": {
        "lintFilePatterns": ["apps/api/**/*.ts"]
      }
    },
    "test": {
      "executor": "@nrwl/jest:jest",
      "outputs": ["coverage/apps/api"],
      "options": {
        "jestConfig": "apps/api/jest.config.ts",
        "passWithNoTests": true
      }
    }
  },
  "tags": []
}

@dan-cooke
Copy link
Contributor

TS plugins has been removed in the latest version of NX without any notification, so this is now broken.

#12966

The only option is to use a custom webpack config, which is messy

@mattvgm
Copy link

mattvgm commented Nov 14, 2022

@dan-cooke Replace tsPlugins to transformers now. It looks like it works the same way, just different name

@dan-cooke
Copy link
Contributor

dan-cooke commented Nov 15, 2022 via email

@eso32
Copy link

eso32 commented Jan 12, 2023

TLDR

your configuration in project.json should look like this:

{
    "name": "project-with-swagger-plugin",
    "targets": {
        "build": {
            "executor": "@nrwl/webpack:webpack",
            "options": {
                // other options 
                "transformers": [
                    // register plugins here
                    "@nestjs/swagger/plugin"
                ]
            },
        }
   }
}

Thanks @mattvgm

@MartinDevillers
Copy link

MartinDevillers commented Mar 18, 2023

Thank you @eso32
One addition for those looking to change any of the plugins options:

{
    "name": "project-with-swagger-plugin",
    "targets": {
        "build": {
            "executor": "@nrwl/webpack:webpack",
            "options": {
                // other options 
                "transformers": [
                    // register plugins here
                    {
                        "name": "@nestjs/swagger/plugin",
                        "options": {
                            "introspectComments": true
                        }
                    }
                ]
            },
        }
   }
}

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests