Skip to content

nxViteTsPaths overwrites generated package.json #30312

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

Open
JSMike opened this issue Mar 7, 2025 · 2 comments · May be fixed by #30705
Open

nxViteTsPaths overwrites generated package.json #30312

JSMike opened this issue Mar 7, 2025 · 2 comments · May be fixed by #30705
Assignees
Labels
scope: bundlers Issues related to webpack, rollup type: bug

Comments

@JSMike
Copy link

JSMike commented Mar 7, 2025

Current Behavior

When using nxViteTsPaths as a vite plugin for bundling a library and also using rollup-plugin-generate-pacakge-json (or any other plugin to create/modify/generate a package.json) the project directory's package.json gets copied to the outputDir during the writeBundle lifecycle hook of Rollup resulting in the generated package.json file getting overwritten.

Expected Behavior

Several things:

  1. nxViteTsPaths ignores the generatePackageJson configuration from @nx/vite:build.

  2. nxViteTsPaths has no options related to copying a package.json file. Earlier versions of nxViteTsPaths do not copy/modify files at all. The copying of package.json is undocumented and the side-effect of overwriting a package.json that's generated during build is unexpected.

The documentation:

https://nx.dev/nx-api/vite/documents/overview#installation

add nxViteTsPaths plugin, so your projects can use workspace libraries.

https://nx.dev/recipes/vite/configure-vite#typescript-paths

You need to use the nxViteTsPaths() plugin to make sure that your TypeScript paths are resolved correctly in your monorepo.

  1. nxViteTsPaths is a 'pre' enforced plugin, yet it uses the writeBundle hook, which is the last hook in rollup runs. Consider using generateBundle hook instead so other plugins can run after.

  2. IMO the functionality of copying the package.json file at the end of the build should be a completely separate plugin as it doesn't align with the documented use case for nxViteTsPaths. This functionality was added for the inference feature and was added to an existing plugin instead of creating a new one.

Steps to Reproduce

  1. Create an NX monorepo
  2. Add a library bundled with vite that modifies the package.json during build
  3. Use nxViteTsPaths plugin

See reproduction: https://stackblitz.com/edit/vitejs-vite-6smqcoqr?file=package.json

npm run build:with builds with nxViteTsPaths plugin
npm run build:without builds without nxViteTsPaths plugin

Notice difference in dist/libs/mylib/package.json#exports.

Nx Report

Node           : 18.20.3
OS             : linux-x64
Native Target  : wasm32
npm            : 10.2.3

nx             : 20.5.0
@nx/js         : 20.5.0
@nx/workspace  : 20.5.0
@nx/devkit     : 20.5.0
@nx/vite       : 20.5.0
@nx/web        : 20.5.0
typescript     : 5.7.3
---------------------------------------
Registered Plugins:
@nx/js/typescript
@nx/vite/plugin

Additional Information

The issue is at https://github.com/nrwl/nx/blob/20.5.0/packages/vite/plugins/nx-tsconfig-paths.plugin.ts#L196-L210

@Coly010 added the writeBundle function as part of support for the inference plugin.

This was introduced in @nx/[email protected].

@FrozenPandaz FrozenPandaz added the scope: bundlers Issues related to webpack, rollup label Mar 24, 2025
JSMike added a commit to JSMike/nx that referenced this issue Mar 24, 2025
The nxViteTsPaths plugin added a rollup writeBundle hook in 19.1.0 that copies over the package.json file from the projectRoot without a configuration option to not copy the file. This adds an option to prevent overwriting of a generated package.json

Closes nrwl#30312
@JSMike
Copy link
Author

JSMike commented Mar 24, 2025

FYI, I met some of you at NX Enterprise in NYC. I went through the legal loopholes at my company to get approval to contribute for this issue. I started work on adding an option to the plugin to disable copying of the package.json file. I'll submit a PR when I get it a bit further. Easiest path forward is to add a flag for the plugin. I'm not sure if there's a good way to capture the configuration options from project.json at the vite plugin level to read the "generatePackageJson" option from @nx/vite:build. If there is an example of something similar that would be helpful.

master...JSMike:nx:issue-30312-nxViteTsPaths

JSMike added a commit to JSMike/nx that referenced this issue Apr 13, 2025
The nxViteTsPaths plugin added a rollup writeBundle hook in 19.1.0 that copies over the package.json file from the projectRoot without a configuration option to not copy the file. This adds an option to prevent overwriting of a generated package.json

Closes nrwl#30312
JSMike added a commit to JSMike/nx that referenced this issue Apr 13, 2025
The nxViteTsPaths plugin added a rollup writeBundle hook in 19.1.0 that copies over the package.json file from the projectRoot without a configuration option to not copy the file. This adds an option to prevent overwriting of a generated package.json

Closes nrwl#30312
JSMike added a commit to JSMike/nx that referenced this issue Apr 13, 2025
The nxViteTsPaths plugin added a rollup writeBundle hook in 19.1.0 that copies over the package.json file from the projectRoot without a configuration option to not copy the file. This adds an option to prevent overwriting of a generated package.json

Closes nrwl#30312
JSMike added a commit to JSMike/nx that referenced this issue Apr 13, 2025
The nxViteTsPaths plugin added a rollup writeBundle hook in 19.1.0 that copies over the package.json file from the projectRoot without a configuration option to not copy the file. This adds an option to prevent overwriting of a generated package.json

Closes nrwl#30312
JSMike added a commit to JSMike/nx that referenced this issue Apr 13, 2025
…ckage.json file if it already exists in dest

The nxViteTsPaths plugin currently always copys the package.json file at the end of the build and does not check if the file was generated from the build process. This adds logic to check if the package.json file already exists in the dist path before copying to prevent overwriting generated files.

Closes nrwl#30312
JSMike added a commit to JSMike/nx that referenced this issue Apr 13, 2025
The nxViteTsPaths plugin currently always copys the package.json file at the end of the build and does not check if the file was generated from the build process. This adds logic to check if the package.json file already exists in the dist path before copying to prevent overwriting generated files.

Closes nrwl#30312
@JSMike
Copy link
Author

JSMike commented Apr 13, 2025

I decided to take another route, and do this more as a fix instead of a feature enhancement. This prevents overwriting of a generated package.json during build. Let me know your thoughts on direction @Coly010

JSMike added a commit to JSMike/nx that referenced this issue Apr 15, 2025
The nxViteTsPaths plugin currently always copys the package.json file at the end of the build and does not check if the file was generated from the build process. This adds logic to check if the package.json file already exists in the dist path before copying to prevent overwriting generated files.

Closes nrwl#30312
JSMike added a commit to JSMike/nx that referenced this issue Apr 16, 2025
The nxViteTsPaths plugin currently always copys the package.json file at the end of the build and does not check if the file was generated from the build process. This adds logic to check if the package.json file already exists in the dist path before copying to prevent overwriting generated files.

Closes nrwl#30312
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: bundlers Issues related to webpack, rollup type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants