nrwl / nx

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

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

Closed luojinghui closed 2 months ago

luojinghui commented 2 months ago

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

Additional Information

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

No response

luojinghui commented 2 months ago

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

jaysoo commented 2 months ago

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 commented 2 months ago

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.

luojinghui commented 1 month ago

@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?

github-actions[bot] commented 3 weeks ago

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.