nrwl / nx

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

NX 20: Vite custom plugin config's hook is called twice when a custom target is defined #28917

Open abarghoud opened 1 week ago

abarghoud commented 1 week ago

Current Behavior

When a target is defined in project.json using @nx/vite:build

"options": {
  "emptyOutDir": false,
  "outputPath": "dist/test",
  "configFile": "test/vite.config.ts"
},

And a custom vite plugin is defined like this

{
  name: 'my-plugin',
  config: (config) => {
    console.log('hello from vite.config.ts');

    return config;
  }
},

Running this target will execute my custom plugin config's hook twice

This problem popped after I migrated from Nx 19 to Nx 20

Expected Behavior

The vite plugin config's hook should be called only once.

GitHub Repo

https://github.com/abarghoud/nx-vite-config-called-twice-bug

Steps to Reproduce

  1. use the repo
  2. npm install
  3. npm run build-from-custon-target (bugs occurs)

Nx Report

Node           : 20.11.1
OS             : darwin-arm64
Native Target  : aarch64-macos
npm            : 10.2.4

nx                 : 20.1.0
@nx/js             : 20.1.0
@nx/eslint         : 20.1.0
@nx/workspace      : 20.1.0
@nx/devkit         : 20.1.0
@nx/eslint-plugin  : 20.1.0
@nx/react          : 20.1.0
@nx/vite           : 20.1.0
@nx/web            : 20.1.0
typescript         : 5.5.4
---------------------------------------
Registered Plugins:
@nx/vite/plugin
@nx/eslint/plugin

Failure Logs

Package Manager Version

1.22.22

Operating System

Additional Information

This behavior causes a problem to me because in my vite plugin I create a websocket server to listen to some external changes, so the double loading behavior causing the WebSocketServer to be initialized twice during the build process which leads the build to fail.

I've already reported a smiliar issue here #22158, but it was due to a misuse on my side.