analogjs / analog

The fullstack meta-framework for Angular. Powered by Vite and Nitro
https://analogjs.org
MIT License
2.48k stars 234 forks source link

Server API can't import from Nx libs #1207

Closed dalenguyen closed 1 month ago

dalenguyen commented 1 month ago

Please provide the environment you discovered this bug in.

Here is the repo: https://github.com/dalenguyen/pdfun

I was trying to use Nx lib in server > routes, but it doesn't allow me to.

image

The Nx lib import: import { openai } from '@pdfun/ai'

Which area/package is the issue in?

vite-plugin-nitro

Description

I was trying to import a service from Nx lib to server route, however, it looks like that the server doesn't recognize the path from Nx lib. It works with Angular component though.

Please provide the exception or error you saw

Here is the error 

[worker reload] Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@pdfun/ai' imported from /Users/dalenguyen/projects/github/pdfun/dist/pdf/.nitro/dev/index.mjs
    at packageResolve (node:internal/modules/esm/resolve:853:9)
    at moduleResolve (node:internal/modules/esm/resolve:910:20)
    at defaultResolve (node:internal/modules/esm/resolve:1130:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36)

Other information

It looks like the index.mjs thinks that the Nx lib (@pdfun/ai) is an NPM package instead of a file location.

image

This is my vite.config.ts:

/// <reference types="vitest" />

import analog from '@analogjs/platform'
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'
import { visualizer } from 'rollup-plugin-visualizer'
import { Plugin, defineConfig, splitVendorChunkPlugin } from 'vite'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  return {
    root: __dirname,
    publicDir: 'src/public',
    cacheDir: `../node_modules/.vite`,

    build: {
      outDir: '../dist/./pdf/client',
      reportCompressedSize: true,
      target: ['es2022'],
    },
    server: {
      fs: {
        // '..' is for primeicons
        allow: ['.', '../libs', '..'],
      },
    },
    ssr: {
      noExternal: [
        'shelljs',
        'firebase/**',
        'firebase-admin/**',
        'ngx-cookie-service/**',
        'ngx-cookie-service-ssr/**',
      ],
    },
    plugins: [
      analog({
        nitro: {
          preset: 'node-server',
        },
        vite: {
          inlineStylesExtension: 'scss',
        },
        ssr: false,
      }),

      nxViteTsPaths(),
      visualizer() as Plugin,
      splitVendorChunkPlugin(),
    ],
    test: {
      globals: true,
      environment: 'jsdom',
      setupFiles: ['src/test-setup.ts'],
      include: ['**/*.spec.ts'],
      reporters: ['default'],
      server: {
        deps: {
          inline: ['@angular/fire'],
        },
      },
    },
    define: {
      'import.meta.vitest': mode !== 'production',
    },
  }
})

I would be willing to submit a PR to fix this issue

brandonroberts commented 1 month ago

https://github.com/analogjs/analog/issues/977#issuecomment-2025120729

goetzrobin commented 1 month ago

This might also be helpful: https://github.com/nrwl/nx/issues/22410

Nitro and Nx don’t play together perfectly yet

dalenguyen commented 1 month ago

Thanks, @brandonroberts & @goetzrobin. Brandon suggestion works!

brandonroberts commented 1 month ago

Thanks @goetzrobin. Maybe we could make a small nitro module that does what's suggested there if a tsconfig.base.json exists. People have ran into this a few times