Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require a class with dependencies from a generator #710

Closed
gama410 opened this issue Aug 21, 2017 · 7 comments
Closed

Require a class with dependencies from a generator #710

gama410 opened this issue Aug 21, 2017 · 7 comments
Labels

Comments

@gama410
Copy link

gama410 commented Aug 21, 2017

This is not a bug, just a question I can't get the answer from Google, SO nor Gitter and I've been racking my brains on it for 2 days now...

I have a generator that receives a model class in argument and uses this class to create a basic CRUD based on its properties and decorators.
I have to require this file from the generator using an absolute path (as I understand it, it is because require considers absolute path to be either core modules or node_modules).

This works fine as long as my model file doesn't import dependencies with an absolute path (in which case it fails because it cannot find the file). All of my imports in the code base are absolute path as typescript allows me to specify the baseUrl and path compiler options (and it's much easier to maintain).

So my question is : is there a way to handle these absolute dependencies in a generator? I was thinking maybe to use my app-bundle which contains all these files precompiled but I don't see any way to use it from the generator...

Thanks for your help!

@zewa666
Copy link
Member

zewa666 commented Aug 21, 2017

There is a section paths in the file aurelia.json which can be configured to understand tsconfig style paths. Take a look at this example https://github.com/zewa666/aurelia-cli-shared-folder which makes use of that approach to import shared code. Is that what you're looking for or did I misunderstand the question?

@gama410
Copy link
Author

gama410 commented Aug 21, 2017

Thanks for your answer!

Unfortunately, I don't really see how I could apply this to my problem. Maybe my SO question will be clearer.

My problem is related to the import of code coming from my src/ folder by a generator. It works well if the code is imported with a relative path (which I don't mind) AND has no dependencies with an absolute path (which is a big problem for me as all the imports in the code base are absolute).

@zewa666
Copy link
Member

zewa666 commented Aug 21, 2017

Could you create a minimal example depicting the issue so we can take a better look at it? If the generator is used as a gulp task, which is run as a node application the issue could obviously be as you mentioned that you can't tell cjs how to load that properly though there might be some tricks to try out. So if your example can also show the expected outcome that would really help

@gama410
Copy link
Author

gama410 commented Aug 22, 2017

Ok, I'll try to do that and put it on github. I'll notify you when it's done. Thanks!

@gama410
Copy link
Author

gama410 commented Aug 22, 2017

I made a minimal repro that I set up in this repository. Using the generator called "test", you can see that it makes a require() of a file to retrieve some of its properties and generate some code. If you pass 'a' as the sole argument, it will require a class that has an absolute dependency (which is my use case) and fail with a 'file not found' error...

If the problem comes from the fact that the generator is run in a node context that is not able to recognize absolute path (as it seems to say here ) would you have an alternative idea? Maybe in a way to use the bundles?
Thanks a lot!

@zewa666
Copy link
Member

zewa666 commented Aug 23, 2017

Sorry I didn't get a chance to look at your example yet, too busy with tons of stuff. What I would have tried is to patch the require method as mentioned here near the end microsoft/TypeScript#9259

@gama410
Copy link
Author

gama410 commented Sep 1, 2017

Sorry for taking so much time to answer : I had other urgent tasks to take care of before coming back to this one...
I took the option you proposed here and it worked well, thanks a lot for pointing me in that direction! I think this ticket can be closed now!

@gama410 gama410 closed this as completed Sep 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants