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

buildLibsFromSource: false doesn't work with Vite executors when package.json has no exports field #18459

Closed
3 of 4 tasks
ms-tng opened this issue Aug 3, 2023 · 4 comments · Fixed by #18477
Closed
3 of 4 tasks
Labels
outdated scope: bundlers Issues related to webpack, rollup type: bug

Comments

@ms-tng
Copy link
Contributor

ms-tng commented Aug 3, 2023

Current Behavior

Suppose an app my-app uses the @nx/vite:dev-server executor and depends on a lib my-lib. Then when the buildLibsFromSource option of the executor is set to false, my-lib is still built from source within my-app.

First of all, this fails because of #18458, but even after patching that, it still doesn't work.

The reason is that when the new vite-tsconfig-paths plugin tries to find the bundled module in the build artifacts of the lib, it first checks for an exports field in package.json, then for module and main, see here:

['exports', '.', 'import'],

When there is no exports field (which is currently the default for a Rollup build, for example), the check for exports throws and the plugin falls back to using the default tsconfig, not the tsconfig.generated.json pointing at the build artifacts.

Expected Behavior

my-lib is not built from source, but its build artifact is used instead.

GitHub Repo

No response

Steps to Reproduce

  1. Patch @nx/vite to fix buildLibsFromSource: false doesn't work with Vite dev-server executor #18458 by replacing context.targetName with 'build' in executor-utils
  2. Create a Vite app and a lib, where the app depends on the lib
  3. Set "buildLibsFromSource": false for the serve target of the app
  4. Build the lib and serve the app
  5. Open the app in a browser and observe that the lib is built from its sources

Nx Report

Node   : 20.2.0
OS     : win32-x64

@nx/express        : 16.6.0
@nx/node           : 16.6.0
@nx/plugin         : 16.6.0
@nx/react          : 16.6.0
@nx/rollup         : 16.6.0
@nx/storybook      : 16.6.0
@nrwl/tao          : 16.6.0
@nx/vite           : 16.6.0
@nx/web            : 16.6.0
typescript         : 5.1.6

Failure Logs

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@AgentEnder AgentEnder added the scope: bundlers Issues related to webpack, rollup label Aug 11, 2023
@barbados-clemens
Copy link
Contributor

this should be resolved with #18477
i still need to do more testing, but basically will allow projects without package.json or export fields to work.

@ms-tng
Copy link
Contributor Author

ms-tng commented Sep 6, 2023

@barbados-clemens Thanks for the fix!

Unfortunately, the issue persists in Nx 16.8.0. I created another reproducing example at https://github.com/ms-tng/nx-buildlibsfromsource-repro2.

When the lib has an exports field, everything works as expected: Setting "buildLibsFromSource": false in the app causes the app to use the build artifact of the lib. When there is no exports field, however, the lib is built/served from source.

Could you take another look?

@ms-tng
Copy link
Contributor Author

ms-tng commented Sep 25, 2023

Ok, I figured it out: You can set the mainFields option of the nxViteTsPaths Vite plugin to something not containing exports, e.g.:

nxViteTsPaths({
  mainFields: ['module', 'main'],
}),

The default setting is [['exports', '.', 'import'], 'module', 'main'], which causes the issue when there is no exports field.

@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 Oct 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: bundlers Issues related to webpack, rollup type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants