Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

Controller loading doesn't work in compiled apps #2

Closed
ben-ryder opened this issue Jan 18, 2022 · 4 comments
Closed

Controller loading doesn't work in compiled apps #2

ben-ryder opened this issue Jan 18, 2022 · 4 comments
Assignees
Labels
bug Something isn't working discovery more information is needed package/core issues related to @kangojs/core

Comments

@ben-ryder
Copy link
Owner

Package Information: kangojs, all versions

Describe the bug
When passing a controllerFilesGlob to KangoJS it will be a glob of typescript files, for example src/modules/**/*.controller.ts.
When the app is compiled to JS this reference stays the same and so the compiled app fails to import the controllers as Typescript files can't be imported into the compiled JS.

To Reproduce
Steps to reproduce the behaviour:

  1. Setup KangoJS with a controller files glob that points to typescript files (such as src/modules/**/*.controller.ts).
  2. When running via ts-node this will work fine.
  3. When attempting to run the compiled JS app an error will occur.

In my application the error is as follows:

err: {
  "type": "SyntaxError",
  "message": "Cannot use import statement outside a module",
  "stack":
      /home/<path>/api/src/modules/albums/album.controller.ts:1
      import { NextFunction, Response } from 'express';
      ^^^^^^
      
      SyntaxError: Cannot use import statement outside a module
          at wrapSafe (internal/modules/cjs/loader.js:984:16)
          at Module._compile (internal/modules/cjs/loader.js:1032:27)
          at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
          at Module.load (internal/modules/cjs/loader.js:933:32)
          at Function.Module._load (internal/modules/cjs/loader.js:774:14)
          at Module.require (internal/modules/cjs/loader.js:957:19)
          at require (internal/modules/cjs/helpers.js:88:18)
}
             

Expected behaviour
I'm unsure at the moment. Ideally the solution should not require any changes from a user perspective.
Can KangoJS somehow hook into the typescript build to change that string?
When loading could KangoJS use tsconfig.json to transform the path if it detects it's running with JS not TS?

Additional context
This could just be a fundamental problem with typescript & builds. It will probably be a good idea to see if this is an
issue other frameworks or libraries have needed to solve.

@ben-ryder ben-ryder added bug Something isn't working discovery more information is needed package/core issues related to @kangojs/core labels Jan 18, 2022
@ben-ryder ben-ryder self-assigned this Jan 18, 2022
@ben-ryder ben-ryder pinned this issue Jan 18, 2022
@ben-ryder
Copy link
Owner Author

ben-ryder commented Jan 18, 2022

Temporary Workaround (NOW OLD, SEE BELOW)

A temporary workaround is to check if the current file being executed is a .js or .ts file and then pass a different controllerFilesGlob to KangoJS depending on the answer.

For example:

const TYPESCRIPT_ACTIVE = __filename.endsWith('ts');
const controllerFilesGlob = TYPESCRIPT_ACTIVE 
  ? 'src/modules/**/*.controller.ts' 
  : 'dist/src/modules/**/*.controller.js'; // set glob to compiled files if running JS

const kangoJS = new KangoJS({
    globalPrefix: "/api/v2",
    controllerFilesGlob,
});
await kangoJS.boostrap(app);

I'm calling this a "temporary" workaround as I'm hoping there will be a solution that doesn't involve extra code from a user's perspective

@ben-ryder ben-ryder changed the title Controller file loading doesn't work in compiled applications Controller loading doesn't work in compiled applications Jan 18, 2022
@ben-ryder ben-ryder changed the title Controller loading doesn't work in compiled applications Controller loading doesn't work in compiled apps Jan 18, 2022
@ben-ryder
Copy link
Owner Author

ben-ryder commented Jan 18, 2022

A possible solution could be something like https://stackoverflow.com/a/59607836:

join(__dirname, '**', '*.entity.{ts,js}')

Using __dirname it would be possible to automatically get either the src or build directory, then {ts,js} will cause the correct files to load.
While this doesn't completely hide this gotcha from a user, it helps reduce the code required to overcome it.

An example could look something like this:

const kangoJS = new KangoJS({
    globalPrefix: "/api/v2",
    controllerFilesGlob: join(__dirname, 'src/modules/**/*.controller.{ts,js}'),
});

Initial testing suggest this approach works fine.

@ben-ryder
Copy link
Owner Author

If a solution like one of the two above is always going to be required to overcome this issue, I would rather keep this as something the user can choose to manage how they wish.
I fear that any implementation I build into KangoJS will be so dependent on certain tsconfig and Typescript build settings that attempting to support a one-size-fits-all solution would cause even more issues.

@ben-ryder
Copy link
Owner Author

ben-ryder commented Jan 19, 2022

TODO

  • update code examples to include the second solution
  • update docs to explicitly highlight this issue

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working discovery more information is needed package/core issues related to @kangojs/core
Projects
None yet
Development

No branches or pull requests

1 participant