node.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. define(["./_base/kernel", "./has", "require"], function(kernel, has, require){
  2. var nodeRequire = kernel.global.require && kernel.global.require.nodeRequire;
  3. if (!nodeRequire) {
  4. throw new Error("Cannot find the Node.js require");
  5. }
  6. var module = nodeRequire("module");
  7. return {
  8. // summary:
  9. // This AMD plugin module allows native Node.js modules to be loaded by AMD modules using the Dojo
  10. // loader. This plugin will not work with AMD loaders that do not expose the Node.js require function
  11. // at `require.nodeRequire`.
  12. //
  13. // example:
  14. // | require(["dojo/node!fs"], function(fs){
  15. // | var fileData = fs.readFileSync("foo.txt", "utf-8");
  16. // | });
  17. load: function(/*string*/ id, /*Function*/ contextRequire, /*Function*/ load){
  18. /*global define:true */
  19. // The `nodeRequire` function comes from the Node.js module of the AMD loader, so module ID resolution is
  20. // relative to the loader's path, not the calling AMD module's path. This means that loading Node.js
  21. // modules that exist in a higher level or sibling path to the loader will cause those modules to fail to
  22. // resolve.
  23. //
  24. // Node.js does not expose a public API for performing module filename resolution relative to an arbitrary
  25. // directory root, so we are forced to dig into the internal functions of the Node.js `module` module to
  26. // use Node.js's own path resolution code instead of having to duplicate its rules ourselves.
  27. //
  28. // Sooner or later, probably around the time that Node.js internal code is reworked to use ES6, these
  29. // methods will no longer be exposed and we will have to find another workaround if they have not exposed
  30. // an API for doing this by then.
  31. if(module._findPath && module._nodeModulePaths){
  32. var localModulePath = module._findPath(id, module._nodeModulePaths(contextRequire.toUrl(".")));
  33. if (localModulePath !== false) {
  34. id = localModulePath;
  35. }
  36. }
  37. var oldDefine = define,
  38. result;
  39. // Some modules attempt to detect an AMD loader by looking for global AMD `define`. This causes issues
  40. // when other CommonJS modules attempt to load them via the standard Node.js `require`, so hide it
  41. // during the load
  42. define = undefined;
  43. try {
  44. result = nodeRequire(id);
  45. }
  46. finally {
  47. define = oldDefine;
  48. }
  49. load(result);
  50. },
  51. normalize: function (/**string*/ id, /*Function*/ normalize){
  52. // summary:
  53. // Produces a normalized CommonJS module ID to be used by Node.js `require`. Relative IDs
  54. // are resolved relative to the requesting module's location in the filesystem and will
  55. // return an ID with path separators appropriate for the local filesystem
  56. if (id.charAt(0) === ".") {
  57. // absolute module IDs need to be generated based on the AMD loader's knowledge of the parent module,
  58. // since Node.js will try to use the directory containing `dojo.js` as the relative root if a
  59. // relative module ID is provided
  60. id = require.toUrl(normalize("./" + id));
  61. }
  62. return id;
  63. }
  64. };
  65. });