This repository reproduces a bug in the @nx/angular-rspack plugin where component stylesheet assets (images, SVGs, etc.) are not properly extracted and copied to the output directory during build.
When Angular components reference assets in their stylesheets using url(), the Angular build system correctly processes these assets and generates outputFiles, but the Nx Angular Rspack plugin doesn't extract and emit these files. This results in:
- CSS gets rewritten to reference assets in the output directory (e.g.,
url('./media/test-pattern.svg')) - The actual asset files are never copied to the output directory
- Browser shows 404 errors for missing asset files
This repository was created using the following sequence of commands:
# Create Nx workspace with Angular Rspack
npx create-nx-workspace@latest repro --preset=angular-standalone --bundler=rspack --style=css --unitTestRunner=jest --e2eTestRunner=playwright --ssr=false --interactive=false && cd repro
# Create assets directory
mkdir -p src/assets
# Add test SVG image (created manually)
# Created: src/assets/test-pattern.svg
# Modified app component files to demonstrate the bug:
# - src/app/app.html - Updated template with test case
# - src/app/app.css - Added background-image URL pointing to the assetnpm installnpx nx buildAfter building, check the output directory structure:
# List the built assets
ls -la dist/repro/browser/
# Check if media directory exists and contains our asset
ls -la dist/repro/browser/media/Expected: The test-pattern.svg file should be copied to dist/repro/browser/media/ directory.
Actual: The media directory doesn't exist or is empty, missing the referenced asset.
npx nx serveOpen browser to http://localhost:4200 and:
- You should see a test section with a div that should have a checkered background pattern
- Open browser developer tools and check the Console tab
- You'll see 404 errors for the missing
test-pattern.svgfile - The background image doesn't load because the asset wasn't copied
The bug occurs in the interaction between these components:
- Angular's ComponentStylesheetBundler: Correctly processes stylesheet URLs and generates
outputFilescontaining the assets - Nx Angular Rspack Plugin: Doesn't handle the
outputFilesfrom the stylesheet bundler - Missing Asset Emission: Assets are never emitted using
compilation.emitAsset()
src/app/app.css- Containsbackground-image: url('./assets/test-pattern.svg')src/assets/test-pattern.svg- Test asset that should be copied but isn'tsrc/app/app.html- Test case demonstrating the issue
- Asset files referenced in component stylesheets should be copied to the output directory
- CSS URLs should be rewritten to point to the copied assets
- No 404 errors for asset files
- CSS URLs are rewritten correctly
- Asset files are NOT copied to the output directory
- Browser shows 404 errors for missing assets
The solution involves modifying the Nx Angular Rspack compilation process to:
- Collect
outputFilesfromComponentStylesheetBundlerresults - Pass these collected assets to the Rspack plugin
- Emit the assets using
compilation.emitAsset()during the build process
The fix requires changes to:
@nx/angular-rspack-compiler/dist/compilation/setup-compilation.js@nx/angular-rspack-compiler/dist/compilation/setup-with-angular-compilation.js@nx/angular-rspack/dist/lib/plugins/angular-rspack-plugin.js
# Install dependencies
npm install
# Build and check for missing assets
npx nx build && ls -la dist/static/media/
# Serve and test in browser
npx nx serveAfter running these commands, you should see the bug in action with missing asset files and 404 errors in the browser console.