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

The built library does not include assets correctly in nx project? #27351

Closed
1 of 4 tasks
luojinghui opened this issue Aug 9, 2024 · 4 comments · Fixed by #27593
Closed
1 of 4 tasks

The built library does not include assets correctly in nx project? #27351

luojinghui opened this issue Aug 9, 2024 · 4 comments · Fixed by #27593

Comments

@luojinghui
Copy link

luojinghui commented Aug 9, 2024

Current Behavior

I used nx + nx/js + vite to build a js library and configured the assets resources, but the build package did not include the resources in the assets, such as the README.md file.

image

Expected Behavior

When using nx build or nx release, resources with assets can be correctly output, such as README.md files, which is very important for npm packages

GitHub Repo

https://github.com/luojinghui/nx-project

Steps to Reproduce

  1. clone github repo
  2. install dep: pnpm install
  3. build packages lib: nx run rtc-sdk:build
  4. check the dist directory to see if the README.md file exists?

Nx Report

NX   Report complete - copy this into the issue template

Node   : 20.14.0
OS     : darwin-x64
pnpm   : 8.15.9

nx (global)        : 19.5.7
nx                 : 19.5.3
@nx/js             : 19.5.4
@nx/jest           : 19.5.3
@nx/eslint         : 19.5.3
@nx/workspace      : 19.5.3
@nx/eslint-plugin  : 19.5.3
@nx/react          : 19.5.3
@nx/vite           : 19.5.4
@nx/vue            : 19.5.4
@nx/web            : 19.5.4
@nx/webpack        : 19.5.3
typescript         : 5.5.4
---------------------------------------
Registered Plugins:
@nx/webpack/plugin
@nx/eslint/plugin
@nx/jest/plugin
@nx/vite/plugin
---------------------------------------
The following packages should match the installed version of nx
  - @nx/js@19.5.4
  - @nx/vite@19.5.4
  - @nx/vue@19.5.4
  - @nx/web@19.5.4

To fix this, run `nx migrate nx@19.5.4`

Failure Logs

No response

Package Manager Version

No response

Operating System

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

Additional Information

if use @nx/js:tsc executor to build, it's no problem!

No response

@luojinghui
Copy link
Author

hi, Is there any progress on the issue? @jaysoo

@jaysoo
Copy link
Member

jaysoo commented Aug 22, 2024

Hi @luojinghui, the @nx/vite:build doesn't support assets. Our philosophy is to rely on the underlying tool as much as possible, so in this case I would configure assets in vite.config.ts instead. Maybe something like this would help: https://www.npmjs.com/package/vite-plugin-static-copy

I think we can add a plugin for this and configure vite.config.ts automatically for JS libs. That would be a new feature.

@jaysoo
Copy link
Member

jaysoo commented Aug 22, 2024

I opened a PR for this. You can configure the plugin like:

// Copies all markdown files to dist
nxCopyAssetsPlugin(['*.md'])

In the meantime I think vite-plugin-static-copy will still work.

jaysoo added a commit that referenced this issue Aug 22, 2024
…7593)

This PR adds a `nxCopyAssetsPlugin` for Vite to brings it to parity with
the other compilers/bundlers (tsc, swc, esbuild, rollup, and webpack).

When generate a lib with Vite (e.g.`nx g @nx/js:lib --bundler=vite` or
`nx g @nx/react:lib --bundler=vite`), we expect it to at least copy
`README.md` as an asset.

Note: Vite has support for copying assets from `public/` but that is
less flexible and more intended for apps, not libs.

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #27351
@luojinghui
Copy link
Author

@jaysoo
Thank you. Actually, I tried to solve the problem with the two plugins rollup-plugin-copy and vite-plugin-static-copy before. Unfortunately, they both had problems. It seemed that the problem was caused by the timing difference between nx build and plugin execution. In the end, the specified *.md resource was not successfully copied to the destination.

So later I tried to execute a custom nodejs script after the nx build was completed to solve the problem, but it was not so elegant.

Thank you for providing another idea. I tried to upgrade the @nx/vite version to: @nx/vite@19.6.3, but did not find the corresponding plugin: @nx/vite/plugins/nx-copy-assets.plugin. Which version do I need to install?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants