Opened 9 years ago

Closed 8 years ago

Last modified 7 years ago

#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)

dojo_node_tests.patch (2.9 KB) - added by Kitson Kelly 8 years ago.
Test case and associated files for testing node plugin

Download all attachments as: .zip

Change History (15)

comment:1 Changed 9 years ago by bill

Owner: set to Rawld Gill

comment:2 Changed 9 years ago by bill

Component: GeneralLoader

comment:3 Changed 9 years ago by Rawld Gill

Milestone: tbd1.7

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!.

comment:4 Changed 9 years ago by Rawld Gill

Status: newassigned

comment:5 Changed 8 years ago by Rawld Gill

Milestone: 1.71.8

comment:6 Changed 8 years ago by ben hockey

Priority: highlow

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 8 years ago by James Burke

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 8 years ago by cjolif

Cc: cjolif added

Changed 8 years ago by Kitson Kelly

Attachment: dojo_node_tests.patch added

Test case and associated files for testing node plugin

comment:9 Changed 8 years ago by Kitson Kelly

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 8 years ago by Kitson Kelly

Cc: Kitson Kelly added

comment:11 Changed 8 years ago by Colin Snover

Resolution: fixed
Status: assignedclosed

In [28707]:

Add dojo/node AMD plugin for loading Node.js modules. Fixes #12892. Thanks to kitsonk for the tests.

comment:12 Changed 7 years ago by 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 ?

comment:13 in reply to:  12 Changed 7 years ago by Kitson Kelly

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 7 years ago by cjolif

Cc: cjolif removed
Note: See TracTickets for help on using tickets.