-
Notifications
You must be signed in to change notification settings - Fork 12
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
Circular dependencies in AMD #74
Comments
👍 |
Do you have a reference for this? |
If you do:
You will get an error that And id you do:
It is a pretty simple mechanism. |
Thanks for the clarification. I'm trying to understand the benefit of the following:
This one sounds irrelevant. The rule should be about always passing dependencies to |
If you do:
There will be no errors because the code will not be analysed. Also if you do:
Everything will work fine because
|
Why? Do we have any realistic problem with it? A "good practice" would be instead not naming functions "require", which seems to be a pretty obvious thing in a AMD environment. Forcing us to always pass dependencies even if we don't have them sounds like one of the of those over-engineering wtfs, IMHO. |
I think that the problem is in the fact that AMD tries to be compatible with CJS and omitting passing dependencies in |
Most likely not. I guess that they tried to at least cover frequent cases like using |
Well, that should tell us what to do :P
Until someone go ask you "why the hell we are forced to set empty dependencies if AMD says it's optional?" (the wtfs I was talking about... after all the code will just look weird) |
I knew from the moment I wrote the first message, that I will have to agree with that :P. |
Anyway, the first proposal is officially rejected. If there are no objections about the second one I'll extend the doc about AMD. |
Well, the second one is the only way to make circular references to work, so it's a must have and it's good to have it documented. |
I found such case:
It will work as long as someone will not add modules he wants to load. For example:
This code will stop working because now 'bar' is not loaded. It means the error is in the totally different place that the one was modified. If we decide to use It fact the situation is even worst. If any other plugin will load |
As we adopted ES6 modules this is a much smaller issue now, although Babel makes things harder: https://phabricator.babeljs.io/T6880. For now we're using a patched version of Require.JS which works around this problem, but we'll need to figure our some other way. Anyway, this topic can be closed. |
TODO (For me to remember) Update https://github.com/ckeditor/ckeditor5-design/wiki/AMD after closing this issue.
In some specific cases we need to deal with circular deps between our modules. We may consider switching to ES6 modules in the future (see #73), but for now we use AMD. Obviously, this is not going to work:
Fortunately, Require.JS allows you to require modules on demand using the
require()
function.This will work, but how does Require.JS know that it needs to preload files
foo.js
andbar.js
(the function, as you can see, is synchronous)? Turns out it statically analyses the code. The analysis is quite simple and fails whenrequire()
is used in some places like JS strings, in unreachable places, etc.However, if you specify the modules that need to be loaded in the first argument of
CKEDITOR.define()
, then Require.JS will not analyse the code. So, if we added[]
to the example above, it would fail as Require.JS would not preparefoo.js
andbar.js
.Solution
We've got two proposals based on our research:
CKEDITOR.define()
, even if the module has zero dependencies. That will turn off the static analysis.require()
calls to get circular dependencies. Where to getrequire()
from? There are other ways in Require.JS (like defining it as one of the deps), but in our case the simplest is to use globalCKEDITOR.require()
which is always accessible. So the above case should be rewritten to:The text was updated successfully, but these errors were encountered: