1 | define(["./has"], function(has){ |
---|
2 | if(!has("host-node")){ |
---|
3 | throw new Error("node plugin failed to load because environment is not Node.js"); |
---|
4 | } |
---|
5 | |
---|
6 | var pathUtil; |
---|
7 | if(require.nodeRequire){ |
---|
8 | pathUtil = require.nodeRequire("path"); |
---|
9 | }else{ |
---|
10 | throw new Error("node plugin failed to load because it cannot find the original Node.js require"); |
---|
11 | } |
---|
12 | |
---|
13 | return { |
---|
14 | // summary: |
---|
15 | // This AMD plugin module allows native Node.js modules to be loaded by AMD modules using the Dojo |
---|
16 | // loader. Note that this plugin will not work with AMD loaders other than the Dojo loader. |
---|
17 | // example: |
---|
18 | // | require(["dojo/node!fs"], function(fs){ |
---|
19 | // | var fileData = fs.readFileSync("foo.txt", "utf-8"); |
---|
20 | // | }); |
---|
21 | |
---|
22 | load: function(/*string*/ id, /*Function*/ require, /*Function*/ load){ |
---|
23 | // summary: |
---|
24 | // Standard AMD plugin interface. See https://github.com/amdjs/amdjs-api/wiki/Loader-Plugins |
---|
25 | // for information. |
---|
26 | |
---|
27 | if(!require.nodeRequire){ |
---|
28 | throw new Error("Cannot find native require function"); |
---|
29 | } |
---|
30 | |
---|
31 | load((function(id, require){ |
---|
32 | var oldDefine = define, |
---|
33 | result; |
---|
34 | |
---|
35 | // Some modules may attempt to detect an AMD loader via define and define.amd. This can cause issues |
---|
36 | // when other CommonJS modules attempt to load them via the standard node require(). If define is |
---|
37 | // temporarily moved into another variable, it will prevent modules from detecting AMD in this fashion. |
---|
38 | define = undefined; |
---|
39 | |
---|
40 | try{ |
---|
41 | result = require(id); |
---|
42 | }finally{ |
---|
43 | define = oldDefine; |
---|
44 | } |
---|
45 | return result; |
---|
46 | })(id, require.nodeRequire)); |
---|
47 | }, |
---|
48 | |
---|
49 | normalize: function (/**string*/ id, /*Function*/ normalize){ |
---|
50 | // summary: |
---|
51 | // Produces a normalized id to be used by node. Relative ids are resolved relative to the requesting |
---|
52 | // module's location in the file system and will return an id with path separators appropriate for the |
---|
53 | // local file system. |
---|
54 | |
---|
55 | if(id.charAt(0) === "."){ |
---|
56 | // dirname of the reference module - normalized to match the local file system |
---|
57 | var referenceModuleDirname = require.toUrl(normalize(".")).replace("/", pathUtil.sep), |
---|
58 | segments = id.split("/"); |
---|
59 | segments.unshift(referenceModuleDirname); |
---|
60 | // this will produce an absolute path normalized to the semantics of the underlying file system. |
---|
61 | id = pathUtil.join.apply(pathUtil, segments); |
---|
62 | } |
---|
63 | |
---|
64 | return id; |
---|
65 | } |
---|
66 | }; |
---|
67 | }); |
---|