nx-extend icon indicating copy to clipboard operation
nx-extend copied to clipboard

Wrong import when adding component. - Shadcn Plugin

Open RestfuI opened this issue 11 months ago • 6 comments

When running the command nx add-component shadcn-ui button, no errors are displayed; however, the generated import for the cn function in the component is incorrect.

Generated Button.tsx:

import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "@myproject-mono/lib/utils";

...

Expected Import:

import { cn } from "@myproject-mono/shadcn-ui-utils";

Note: The library names provided during plugin usage were shadcn-ui and shadcn-ui-utils.

RestfuI avatar Dec 29 '24 05:12 RestfuI

Those are based off what is defined inside the components.json, could you share that? When the CLI changed that file needed to be updated.

TriPSs avatar Dec 29 '24 08:12 TriPSs

Hi,

Thanks for the reply!

Here is my components.json file.

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "default",
  "rsc": false,
  "tailwind": {
    "config": "libs\\shadcn-ui-utils\\src\\tailwind.config.ts",
    "css": "libs\\shadcn-ui-utils\\src\\global.css",
    "baseColor": "neutral",
    "cssVariables": true
  },
  "aliases": {
    "ui": "@myproject-mono/shadcn-ui",
    "components": "@myproject-mono/shadcn-ui/components",
    "utils": "@myproject-mono/shadcn-ui-utils",
    "hooks": "@myproject-mono/shadcn-ui/hooks"
  }
}

RestfuI avatar Dec 29 '24 21:12 RestfuI

That looks good, the tsconfig in the root also has the paths correctly defined?

TriPSs avatar Dec 30 '24 17:12 TriPSs

All looks to be correctly configured on my end.

tsconfig.base.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "rootDir": ".",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "importHelpers": true,
    "target": "es2015",
    "module": "esnext",
    "lib": [
      "es2020",
      "dom"
    ],
    "skipLibCheck": true,
    "skipDefaultLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "@myproject-mono/common": [
        "libs/common/src/index.ts"
      ],
      "@myproject-mono/common/api": [
        "libs/common/src/lib/api/index.ts"
      ],
      "@myproject-mono/common/entities": [
        "libs/common/src/lib/entities/index.ts"
      ],
      "@myproject-mono/common/modules": [
        "libs/common/src/lib/modules/index.ts"
      ],
      "@myproject-mono/common/services": [
        "libs/common/src/lib/services/index.ts"
      ],
      "@myproject-mono/shadcn-ui": [
        "libs/shadcn-ui/src"
      ],
      "@myproject-mono/shadcn-ui-utils": [
        "libs/shadcn-ui-utils/src"
      ],
      "@myproject-mono/shadcn-ui/*": [
        "libs/shadcn-ui/src/*"
      ],
      "@myproject-mono/shadcn-ui-utils/*": [
        "libs/shadcn-ui-utils/src/*"
      ]
    }
  },
  "exclude": [
    "node_modules",
    "tmp"
  ]
}

RestfuI avatar Dec 30 '24 17:12 RestfuI

I have the exact same problem. Tried on a fresh install. It always imports from @projectname/lib/utils.

Even when I change the aliases in components.json, it has no effect (and yes, I delete the .nx cache after making manual changes)

mj-meyer avatar Feb 17 '25 11:02 mj-meyer

Same issue here.

petenilson avatar May 08 '25 15:05 petenilson