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

How to improve nested requires? #9323

Closed
kirrg001 opened this issue Dec 12, 2017 · 12 comments
Closed

How to improve nested requires? #9323

kirrg001 opened this issue Dec 12, 2017 · 12 comments
Labels
server / core Issues relating to the server or core of Ghost stale [triage] Issues that were closed to to lack of traction

Comments

@kirrg001
Copy link
Contributor

kirrg001 commented Dec 12, 2017

We would like to figure out how to improve requiring modules.

e.g. take a look at this - we have to go back in the folder tree to be able to require the logging module.

The same problem exists in our test env, see.

global require function

One option could be to add a global.requireSomething fn, which is able to read modules from a defined root path.

e.g. requireRoot('services') - would require a module from the core/server folder.
e.g. requireLib('errors') - would require a module inside core/server/lib

other options?

Wait that Node supports import/export and a similar module like ember-resolve. So you can do:

import * from 'ghost/services' - that would be so cool 🙊

@kirrg001 kirrg001 added refactoring/cleanup server / core Issues relating to the server or core of Ghost labels Dec 12, 2017
@kirrg001
Copy link
Contributor Author

kirrg001 commented Dec 12, 2017

This and this looks interesting.
Here is a whole list of ideas.

@kirrg001
Copy link
Contributor Author

Furthermore, it would be great to raise an issue to design a solution for our dirty requires.

@davisfreimanis
Copy link

I could look at this task.

We are a group of 5 students from KTH in Stockholm and we have a task to look at refactoring issues in open source projects.

@kirrg001
Copy link
Contributor Author

@davisfreimanis Sure 👍

Just writing down the steps for you how to tackle this task:

  • come up with an idea
    • look for references (how do others solve this problem?)
    • please post your full idea here as comment (+ share an example usage - a branch with a workable usage)
  • the Ghost team will review and approve or request changes (PR's are not accepted without approval)
  • after approval, you can finish the refactoring

If you have any questions, ping me in slack.

@ernstwi
Copy link

ernstwi commented Feb 14, 2018

Hello!

I'm one of the other students from @KTH mentioned by @davisfreimanis above (the others are @dsalathe, @steinnp, and @hannahfloeter). We've looked at the options a bit and think the simplest solution is to use a global function requireRoot as you suggested (also suggested here).

An example usage is here. One thing we aren't sure about is where we could define the global function to have it be visible from tests as well? And do you think this seems like a good solution?

@kirrg001
Copy link
Contributor Author

@ernstwi Hey, thanks for coming back. I'll reply tomorrow 👋

@kirrg001
Copy link
Contributor Author

kirrg001 commented Feb 15, 2018

Just sharing some options i went through. I'll look into this tomorrow again. If you find anything else, please share. Before we can make a decision we have to get a clear overview of currently available options and upcoming options.

@ernstwi
Copy link

ernstwi commented Feb 15, 2018

Thank you. I don't think I have enough node experience to offer a good opinion on which of these solutions is the best, but we did try the NODE_PATH approach with success on this branch.

We replaced all the relative paths in core/server with absolute paths (with core/server as root) and set the NODE_PATH environment variable manually, then tested with grunt test-all. So what remains to do if this is the option we go with is to set NODE_PATH programatically.

@kirrg001
Copy link
Contributor Author

@ernstwi Thanks for coming back :)

Could you pls swing by our slack channel and ping me in the help channel. Thanks! 👋

@ernstwi
Copy link

ernstwi commented Feb 16, 2018

In case it could be useful in future, here's a script to rewrite the require paths using the current directory as root.

@allouis
Copy link
Contributor

allouis commented Aug 27, 2018

What do you think about patching global.require? I'm thinking we could have some kind of registerRequirePrefix function that will allow you to set a prefix for requiring modules. The prefix would be linked to a directory and all requires containing the prefix would be resolved from the directory linked to the prefix. We could ensure than prefixes would have to be invalid packages names, so we can ensue there won't be conflict with installed packages. Below i've used : in the prefix name which isn't allowed in npm package names.

example usage

// index.js (entrypoint)
const registerRequirePrefix = require('register-require-prefix')
registerRequirePrefix('models:', __dirname + '/models');
registerRequirePrefix('ghost:', __dirname);
// some other file
const SomeModel = require('models:thing'); // equivalent of requiring __dirname/models/thing
// OR
const SomeModel = require('ghost:models/thing'); // same as above

@kirrg001
Copy link
Contributor Author

@allouis Thanks for getting involved. It's a nice idea, but it also forces us to override the require module.

@ErisDS ErisDS added the stale [triage] Issues that were closed to to lack of traction label Jan 23, 2019
@ErisDS ErisDS closed this as completed Jan 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
server / core Issues relating to the server or core of Ghost stale [triage] Issues that were closed to to lack of traction
Projects
None yet
Development

No branches or pull requests

5 participants