nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
22.7k stars 2.27k forks source link

Vite plugin does not create serve, serve-static and preview targets anymore #26800

Open simedia-fabianamhof opened 2 weeks ago

simedia-fabianamhof commented 2 weeks ago

Current Behavior

Since v19.3.1 the @nx/vite plugin does not create serve, serve-static and preview targets if vite is configured in library mode. This is wrong since these commands do work properly in vite if you have set up a demo index.html file (https://vitejs.dev/guide/build#library-mode).

Error: NX Cannot find configuration for task my-lib:serve

Expected Behavior

The targets should execute the corresponding vite commands like in the previous NX versions.

GitHub Repo

No response

Steps to Reproduce

  1. Create an NX Workspace (npx create-nx-workspace@latest --preset=react-standalone --bundler=vite)
  2. Generate a React Libary (nx g @nx/react:lib my-lib --bundler=vite)
  3. Try start the dev-server with (nx serve my-lib)

Nx Report

NX   Report complete - copy this into the issue template

Node   : 20.12.2
OS     : linux-x64
npm    : 10.5.0

nx                 : 19.4.0
@nx/js             : 19.4.0
@nx/linter         : 19.4.0
@nx/eslint         : 19.4.0
@nx/workspace      : 19.4.0
@nx/devkit         : 19.4.0
@nx/eslint-plugin  : 19.4.0
@nx/react          : 19.4.0
@nrwl/tao          : 19.4.0
@nx/vite           : 19.4.0
@nx/web            : 19.4.0
typescript         : 5.4.5
---------------------------------------
Registered Plugins:
@nx/vite/plugin
@nx/eslint/plugin

Failure Logs

NX   Cannot find configuration for task my-lib:serve

Error: Cannot find configuration for task my-lib:serve
    at ProcessTasks.createTask (/home/fabian/tmp/org/node_modules/nx/src/tasks-runner/create-task-graph.js:155:19)
    at ProcessTasks.processTasks (/home/fabian/tmp/org/node_modules/nx/src/tasks-runner/create-task-graph.js:23:39)
    at createTaskGraph (/home/fabian/tmp/org/node_modules/nx/src/tasks-runner/create-task-graph.js:193:21)
    at createTaskGraphAndValidateCycles (/home/fabian/tmp/org/node_modules/nx/src/tasks-runner/run-command.js:65:63)
    at /home/fabian/tmp/org/node_modules/nx/src/tasks-runner/run-command.js:88:27
    at handleErrors (/home/fabian/tmp/org/node_modules/nx/src/utils/params.js:9:30)
    at runCommand (/home/fabian/tmp/org/node_modules/nx/src/tasks-runner/run-command.js:86:52)
    at Object.runOne (/home/fabian/tmp/org/node_modules/nx/src/command-line/run/run-one.js:53:59)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async handleErrors (/home/fabian/tmp/org/node_modules/nx/src/utils/params.js:9:24)

Package Manager Version

No response

Operating System

Additional Information

No response

simedia-fabianamhof commented 2 weeks ago

Removing the if condition in packages/vite/src/plugins/plugin.ts:194 would solve this bug.

// If running in library mode, then there is nothing to serve.
    if (!viteConfig.build?.lib) {
      targets[options.serveTargetName] = serveTarget(projectRoot);
      targets[options.previewTargetName] = previewTarget(projectRoot);
      targets[options.serveStaticTargetName] = serveStaticTarget(options) as {};
    }`