adonisjs / rfcs

💬 Sharing big changes with community to add them to the AdonisJs eco-system
52 stars 6 forks source link

Provider autodiscovery #19

Closed MZanggl closed 4 years ago

MZanggl commented 5 years ago

What problem does it solve?

Adding a package involves an seemingly unnecessary step of adding it to the providers / aceProviders array inside start/app.js. This is currently part of the documentation of every single package. It doesn't serve any real purpose since it can not even be used to verify if a package is actually being used or not (because it is usually aliased).

Proposal

It should be sufficient to install a package, without having to add it to the providers arrays.

How will providers be identified?

The module (provider package) needs to have the following section in "package.json"

{
    "name": "@adonisjs/vow",
    "adonisjs": {
        "providers": ["./providers/AProvider"],
        "aceProviders": ["./providers/BProvider"]
    }
}

What is the process of autodiscovery?

There are two approaches

The performant way

  1. Adonis-slim, adonis-api and adonis-fullstack receive a new script in package.json "postinstall": "node ace autodiscovery". This makes use of npm's hooks and will automatically run after npm install. Alternatively, It can be added to adonis install to avoid adding the scripts, but I think there are many people who are used to doing npm install instead.
  2. node ace autodiscovery will look into each package found in package.json and find all the identified providers (those that have an "adonisjs" key)
  3. It will save the result in a gitignored file ./start/discovered-providers.json
  4. when starting the server, adonis-framework (src/Ignitor/Bootstrapper.ts) will search in the json file from step 3 and register the providers automatically

The straight forward way

  1. Identify the packages dynamically when starting the server. This takes approximately 100 ms on my windows machine on a project with 50 dependencies. The change requires low effort but will definitey slow down server start up time, which might be annoying during development.

Questions

What about nested packages?

Including nested packages has more drawbacks than benefits

Relevant links

Are you willing to work on it?

Yes

MZanggl commented 4 years ago

Will close this in favor of https://github.com/adonisjs/rfcs/issues/21