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
define() & paths relativity & resolution with requirejs on nodejs #450
Comments
I've trouble here too, I'm trying to use mocha to test in the command line. But than that means i need to remove it after testing. |
What's even more confusing is that if you want to define your dependencies with the module-relative path (eg. So you can't easily test or execute you scripts in the browser and node, unless you use ONLY the file-relative path resolution (starting with Alternativelly you use umdjs, pick a template and use module-relative on the browser and calculate the path resolution manually for each dependency on the node side. The downside is too much boilerplate, and optimization with r.js wont work. I am actually thinking of drafting a utility that will do the 'conversion' automatically :-) Hope its not an overkill! |
Some background: If you load requirejs via `require('requirejs') as done on the requirejs-node doc page, then you would not need amdefine in that script. amdefine is an option to using a UMD pattern in a particularly library module. Ideally you would just use the An option could be to figure out how to load mocha via a It is all a bit tricky because node does not understand define() and getting the right pathing behavior is tricky too. There is an |
@jrburke, thank you for your response. I believe having to tweak mocha or any other framework just to make it work with AMD, completely breaks the reusability and modularity concerns that modules come to solve. Using modules should be as unobstrusive as possible, like using an import in other languages. In current state, this is not the case. For this reason, after having tried all possible valid solutions (I hope), I have drafted out a simple source converter that I believe will be useful and I would like to hear your thoughts. The main aim is to write once (with AMD only syntax) and run on both browser & node, using some UMD template, without any other hassle. The core usage pattern will be:
This way, your code is guaranteed(!) to run everywhere, but written consistently with the exact same authoring semantics (provided async requires are also used on the node side), and testers like mocha wouldn't even know the difference. Think of it as the oposite of browserify: it takes AMD-browser modules and wraps them around UMD so they can run on node. Its a trivial thought really, but can help many people quickly use AMD over UMD without even bothering with it. Ultimatelly I hope it wil raise AMD adoption :-) My first tests seem to work and I should have a first alpha-preview version hopefully by the end of this week for you to reflect on, but I'd appreciate you first thoughts on it. |
@anodynos see this comment in the amdefine issue: I think a converter that taps into node's require.extensions system may be the way to go. But it will be difficult, just on the path resolution front to bridge both node and AMD conventions. |
I haven't looked at What I have in the works is just a proof of concept of a generic AMD-to-UMD converter that allows AMD written code (using both Thus, it can be incorporated in your usual chain of build (eg. grunt) and then be run, deployed and tested on both browser and node (as long as its not using any dom or node's specific stuff). |
Converters are difficult, in that not all the same concepts are in both envs, in particular the |
If you write against the |
As promised, I have released an early version of uRequire - https://github.com/anodynos/uRequire uRequire converts AMD/commonjs modules to the UMD format. It allows you to write against a strict AMD or a more 'relaxed' notation. Any feedback is welcome. |
I am eager to write reusable modules, spanned in multiple files, that run & test on both the browser and nodejs.
According to the docs, I can use requirejs on node like this
but then I can't understand how I can define modules that run on node.js, since
define()
is not available.I tried to do a
but then with the call
nothing is happening, 'my code' never gets executed. Is this a bug ? Or is
define()
not available on node?I also tried to use requirejs along with
amdefine
which seems to work, but is a bit of a frankestein: the
amdefine
version ofdefine()
doesn't respect the baseUrl set on requirejs. Specifically theamddefine define()
delegates to node's require() and its resolution of paths that are relative to the file.js and not some 'baseUrl'. On top, node's require() requires./
for files on the same directory, and there is no 'base', so even this doesn't work smoothly. How can we run code (eg mocha tests) with a reference to a base directory with requirejs based modules, on node ?So far, my reusable modules work on the browser but not on node, unless I do all kinds of non-resuable tricks & tests with the path names.
Is umdjs format the only way to achieve true cross-executability ? But even then, the path relativity semantics are not the same....
The text was updated successfully, but these errors were encountered: