#12892 closed enhancement (fixed)
[patch] Add support to load native node modules with dojo loader
Reported by: | Colin Snover | Owned by: | Rawld Gill |
---|---|---|---|
Priority: | low | Milestone: | 1.8 |
Component: | Loader | Version: | 1.6.1 |
Keywords: | Cc: | Kitson Kelly | |
Blocked By: | Blocking: |
Description
RequireJS has an extremely useful feature that allows a user to transparently require a native node module the same as an AMD-defined module. e.g. require(['fs'], function (fs) { … })
will load the filesystem module to fs
as long as there isn’t already a fs.js
that would be otherwise resolved.
I feel like this would be a very worthwhile addition to the dojo loader, but I’m not sure if it wouldn’t make more sense as a separate module that one would instead load as require(['dojo/native!fs'])
or something. In any case, here is such a module if that is determined to be the best way to do it:
define([ "require", "dojo/has" ], function (require, has) { // module: // native // summary: // This module implements the !native plugin. // description: // Loads a native node.js module. if (!has("host-node")) { console.error("native plugin failed to load because environment is not node"); } return { load: function (id, requireParent, onLoad) { onLoad(require.nodeRequire(id)); } }; });
It would be great to get this into 1.7, though not necessarily a deal-breaker since adding a module like this is trivial.
Regards,
Attachments (1)
Change History (15)
comment:1 Changed 11 years ago by
Owner: | set to Rawld Gill |
---|
comment:2 Changed 11 years ago by
Component: | General → Loader |
---|
comment:3 Changed 11 years ago by
Milestone: | tbd → 1.7 |
---|
comment:4 Changed 11 years ago by
Status: | new → assigned |
---|
comment:5 Changed 11 years ago by
Milestone: | 1.7 → 1.8 |
---|
comment:6 Changed 10 years ago by
Priority: | high → low |
---|
isn there some way we can determine that fs doesn't exist within our AMD module id space and then try node's require as a last option before declaring that we can't find a module? a plugin seems kind of annoying and so does define("fs", require.nodeRequire("fs"));
comment:7 Changed 10 years ago by
requirejs does this by: if the module is found via the requirejs baseUrl/paths/packages config, treat it as an AMD module. If the module is not found via that config, then Node's require is used to load the module.
So projects are encouraged to not put node_modules in their amd config, leave that for node/npm-installed modules loaded via node's require. So no plugins or special syntax, just a paths fallback.
comment:8 Changed 10 years ago by
Cc: | cjolif added |
---|
Changed 10 years ago by
Attachment: | dojo_node_tests.patch added |
---|
Test case and associated files for testing node plugin
comment:9 Changed 10 years ago by
Adding test cases for plugin which snover wants to land.
Applying the patch, plus adding the plugin and running the following is coming back with no errors:
node dojo/dojo.js load=doh test=dojo/tests/node
Don't know if there are any more relevant test cases other than:
- Loading internal module
- Not loading a module (failing properly)
- Loading external module
- Loading external module that requires another module
- Loading a module identified with package.json to identify main module
comment:10 Changed 10 years ago by
Cc: | Kitson Kelly added |
---|
comment:12 follow-up: 13 Changed 10 years ago by
It seems that all paths passed to plugin behave as relative to dojo/dojo.js and not to the module using the plugin.
Any ideas ?
comment:13 Changed 10 years ago by
Replying to mm:
It seems that all paths passed to plugin behave as relative to dojo/dojo.js and not to the module using the plugin.
Any ideas ?
If you think you are dealing with a new issue, it would be better to open a new ticket or post to the Dojo interest mailing list than reply to a closed ticket.
But the modules are resolved in the way there are invoked relative to initial script loaded by node. So if you are bootstrapping dojo/dojo.js directly, then that is where the modules will be relative to. If you are using a bootstrap script that loads dojo/dojo.js, then the modules will be relative to that script. That would be "works as designed".
comment:14 Changed 10 years ago by
Cc: | cjolif removed |
---|
That's a really good idea csnover. I want to talk to James since I don't understand how RequireJS could know that a valid (e.g.) fs does not exist. I think we should name the plugin dojo/node!.