nitedani / vite-plugin-angular

Vite plugin for Angular, using esbuild and SWC
72 stars 5 forks source link

Routing not finding local library (nx) #10

Closed cskiwi closed 11 months ago

cskiwi commented 11 months ago

Hi,

I've got the project up and running, but when I want to load a component from a library, the building process breaks:

[vite] Error when evaluating SSR module /src/app/app.component.ts: failed to import "@gull/welcome"

I tried some configurations, but the error persists. you got any suggestion? full repo can be found on: https://github.com/cskiwi/herring-gull-angular

UPDATE: On some further investigation I found that if I put the following in my vite.config.ts it works:

export default defineConfig({
  plugins: [
    // plugins
  ],
  resolve: {
    alias: {
      '@gull/welcome': path.resolve(
        __dirname,
        '../../libs/frontend/pages/welcome/src/index.ts'
      ),
    },
  },
});

so it's because the nx libraries aren't resolved

nitedani commented 11 months ago

I'm not familiar with nx. Should this work out of the box?

cskiwi commented 11 months ago

I though it would, but I had to configure it in vite to use the nxViteTsPaths, and that solved this issue. so finally this would become:

import { angular } from '@nitedani/vite-plugin-angular/plugin';
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
import { vavite } from 'vavite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [
    angular(),
    vavite({
      serverEntry: '/src/server/main.ts',
      serveClientAssetsInDev: true,
    }),
    nxViteTsPaths(), // this did the trick!
  ],
});

Thanks to @brandonroberts with AnalogJs for having this already solved!