Nest: Ability to reinitialize components of module (or replace controller/components in runtime)

Created on 19 Feb 2018  路  8Comments  路  Source: nestjs/nest

Ability to reinitialize components of module
or
Ability to replace/merge controller/components in modules in runtime
or
Ability to create modules and add to application in runtime

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Unable to (reinitialize components of module / replace/merge controller/components in modules in runtime / create modules and add to application in runtime)

Expected behavior

Possibility to do it.

What is the motivation / use case for changing the behavior?

Tools like nodemon or node_dev restarts application in cold mode. It means that behavior the same as manual ctrl+c -> node server.ts
The goal is to adapt nestjs to hot module replacement API of webpack or similar tools.
Because cold can take a lot of time up to 10 seconds which is bad developer experience. Most of the time config (connection string, etc.), node_modules, etc. are not changing during development.
Hot module replacement is much faster.
It would be nice if modules can 'reinitialize' feature (see other options in title or in 'Current behavior')

Example code with hmr:

const app = await NestFactory.create(AppModule);
await app.listen(config.get('port'));

// Example 1
if (module.hot) {
    module.hot.accept('./cat.module', () => {
        const { CatModule } = require('./cat.module'); // Required to get new reference.
        app.replaceModule(CatModuleOld, CatModule);
    });
}

// Example 2
if (module.hot) {
    module.hot.accept('./cat.module', () => {
        const { CatModule } = require('./cat.module'); // Required to get new reference.
        app.addModule(CatModule); // Acts like merge and overwrites old controllers paths, etc.
    });
}
discussion 馃敟 type

Most helpful comment

was this ever made?

All 8 comments

Cool idea 馃檪 I'll see what we can do.

I think some parts of TestingModule are already doing it something similar.

Nest forces to have a very strict modules API definitions. It means that each module has to be imported at least once, and thus the whole relationship will eventually 'touch' a root module which is used in main.ts (bootstrap) file. In this case, even small change inside nested module could affect root module, and that's why the basic idea of HMR doesn't fit here. BUT (馃) your proposal, in fact, brought another idea. HMR helps a lot with partial compilation. Thanks to that, we should reduce rebuilds even 300%, meaning really fast app reloads. It's a part of 5.0.0 milestone 馃檪

@kamilmysliwiec
I think we could make something like this
NestFactory.reload()
or something similar to the front-end Development like using webpack

...
if(module.hot) {
    module.hot.accept(m => NestFactory.reload(m))
}
...

this should re-init the effected module , with some help from wepback
actually, 2 days ago i was experimenting Wepback 4 with back-end and HMR with Nest. but until now, it's seems to be hard to replace some Components in Run-time, without breacking some nest private api 馃榿.
but soon i will find a good solution.

It doesn't make too much sense @shekohex, HMR + webpack 4 + Nest 5.0.0 reinitializes the whole app in 1-2 seconds 馃敟 Building API that allows replacing particular module would still affect with reinitialization of the entire path (from root module to module X). Also, Nest 5.0.0 brings some small features that make it impossible.

can't wait for officially releasing it

I already playing with the v5 branche 馃槀馃敟

was this ever made?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

marshall007 picture marshall007  路  3Comments

breitsmiley picture breitsmiley  路  3Comments

rlesniak picture rlesniak  路  3Comments

2233322 picture 2233322  路  3Comments

VRspace4 picture VRspace4  路  3Comments