nuxt / nuxt/bridge

Nuxt Bridge doesn't work well with InversifyJS

Open
#338 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending triage
Dominant language
TypeScript
Stars
285
Forks
33
Avg merge
22h 38m
Merged PRs (30d)
3

Description

Environment
RootDir: /home/joe/Temp/nuxt-bridge-inversify-repro                   18:46:31
Nuxt project info:                                                    18:46:31

------------------------------
- Operating System: `Linux`
- Node Version:     `v14.19.1`
- Nuxt Version:     `2.16.0-27358576.777a4b7f`
- Package Manager:  `npm@7.24.0`
- Builder:          `webpack`
- User Config:      `ssr`, `build`, `bridge`, `buildModules`
- Runtime Modules:  `-`
- Build Modules:    `@nuxt/bridge@3.0.0-27530716.c4f8671`
------------------------------

(the same issue happens with Node v16 too)

Reproduction

https://github.com/jozsefsallai/nuxt-bridge-inversify-repro

Describe the bug

We are trying to migrate our Nuxt 2 web app to Nuxt Bridge (mainly to benefit from the Nitro engine and incrementally adopt Vue 3). However, our app also uses TypeScript and InversifyJS to manage our various services and dependency injection. We have our services defined in the services directory and we have a special file that contains unique identifiers/symbols for our services (which exports an object called TYPES). This is common practice when using Inversify (or similar libraries).

Since Inversify uses decorators and decorator metadata, it's not going to work with Nuxt Bridge out of the box (see https://github.com/unjs/jiti/issues/27#issuecomment-849132971).

Installing @babel/plugin-proposal-decorators and babel-plugin-transform-typescript-metadata, then defining the Babel plugins in nuxt.config.ts solves this, however, when we try to use something like:

import { injectable, inject } from "inversify";
import { TYPES } from "~/services/inversify-types";

@injectable()
class DemoService {
  constructor(@inject(TYPES.OtherService) private otherService: IOtherService) {}
}

...the page doesn't build at all, saying that TYPES is not defined. I assume this happens because the way Babel transforms the decorator metadata doesn't take into consideration that whatever's in the decorator parameters might be an imported module and instead it's compiling it as a literal. (probably a plugin ordering issue?)

I should mention that a similar setup works just fine without installing any Babel plugins in Nuxt 2, or writing extra configuration.

There might also be a bit of a conflict between Inversify's functions and the Nuxt 3 globals (but I can't confirm this because it might be related to Babel plugin ordering again). I tried aliasing the members I needed from TYPES as constants at the beginning of the file, then passing those to @inject instead of TYPES.Whatever. However, doing so resulted in an inject() can only be used inside setup() or functional components error (which makes me assume that there's either a conflict or the Inversify imports aren't aliased properly in Webpack).

Not sure if there's something wrong with our setup and configuration or whether it's a bug in the new build tools (I also wasn't sure if I was supposed to open this issue in this repo or jiti's), but I would really appreciate any input on this matter. Nuxt 3 is an awesome step forward in the Vue ecosystem and we would love to fully migrate to it as soon as we can 🙏

Additional context

No response

Logs

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked nuxt-bridge-inversify-repro and its Nuxt configuration, including the Babel plugins and nuxt.config.ts setup. Reproduce the build failure with the services/inversify-types import and decorator usage, then determine whether the Babel/Webpack path handles the metadata import correctly; done means the repro builds without the TYPES or inject() errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
nuxtjs, typescript, webpack
Domain
build-system, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.