-
Notifications
You must be signed in to change notification settings - Fork 571
Make module imports relative. #938
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
Conversation
|
Seems like a good solution to me, I'm not sure why we didn't do this in the first place! 👍 |
|
I'm not sure I see the benefit of this, sorry. To be honest, I don't fully understand the issue yet :) The way I understand it, CommonJS defines what the module loader should do, which is why I like that we do the minimum possible work when generating imports. Maybe the relative path could be a command line option, but I'm not sure I like making |
|
No need to apologize! I thought it was better to make the change and submit a pull request rather than open an issue for this one. I think the benefit is that people can use It took me a while to find the linked issue and make things work otherwise and both workarounds aren't ideal, it seems. |
|
Hmm, I see your point @paf31, but yeah I think it would be nice to have as a command line option. Avoiding the copy etc. stage during compliation would be handy, as it seems to have tripped up a few people. |
|
Might want to consider making the ES6 module format an option too. |
|
I like the ES6 module idea, maybe as a separate option for now. I guess what I don't understand is: why not just set |
|
My concern was that a package in |
|
Could you try it and see if it solves your problem? It might not, but if it does, we could just change the default output directory to that. |
|
That works, although obviously I now need to do: var Main = require("./output/node_modules/Main");instead of: var Main = require("Main");which seems reasonable to me. |
|
I'm happy to submit a pull request which changes the default output directory, if that's what people want. |
|
I'm trying to think if it will mess up any tools, but honestly I'm not sure. Maybe |
|
Pulp has a hard-coded build path instead of deferring to https://github.com/bodil/pulp/blob/e73b9c29ac7e02e6f487037d8c2c6898915f70a7/index.js#L163 I just confirmed locally that even with the change to |
|
To clarify, I'm not pushing for this either way. Manually setting |
|
It feels like a bit of a hack to abuse the |
|
How is it a hack? Doesn't CommonJS define the resolution algorithm? My concern is that we end up tying the import code to the particular way in which we lay out things on disk, which means we might not be able to play well with other modules later. |
|
No, node specifies their module resolution algorithm here: https://nodejs.org/docs/latest/api/modules.html#modules_all_together I'm sure other module resolution algorithms for CommonJS systems would implement relative modules, but would be very unlikely to include |
|
What about this one? I'm still on the side of slightly preferring relative imports to relying on |
|
I don't really fully understand the benefit of this. If the goal is just to be able to run the The current approach isn't a hack at all - we create a valid set of CommonJS modules, each in their own directory. How they get used, and how they reference each other depends on the CommonJS implementation and its module resolution algorithm. The approach is simple (a module name is a On the other hand, using |
|
The other consideration is that by doing it this way we're trampling on the module name space root - without using |
|
To offer an alternative, I would merge a PR which made this a command line option. |
|
That would work for me, it could be the |
|
True. A better example of a different algorithm would be a situation in which you wanted one set of modules to be placed in |
|
One more thought: I don't think it's the job of I think |
|
The way I currently use my PureScript modules is with @ethul's brilliant https://github.com/ethul/purs-loader and webpack. I can then import functions from that module with whatever import syntax I prefer.
https://github.com/kRITZCREEK/squirrelzon/blob/master/src/js/components/Shoppingcart.js So I think if people want to improve their imports they should use a packaging tool that does that. The compilers/make-tool's job is rather to enable packaging tools to use the output. PS: A nice sideeffect of this is, that I can now use ES6 syntax in my FFI declarations. |
|
I've found one example where it could be very useful. Let's say you have some js library MyLib downloaded using npm and you wrap it using FFI like this: module MyLib where
foreign import foo """
function foo (a) {
return require('mylib').foo(a);
} """ :: a -> aThen you compile it using |
This pull request addresses the issue described in #352 (comment).
This means that modules can be
required without settingprocess.env.NODE_PATHor renaming the output directory tonode_modules.