Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # amdefine
  2. A module that can be used to implement AMD's define() in Node. This allows you
  3. to code to the AMD API and have the module work in node programs without
  4. requiring those other programs to use AMD.
  5. ## Usage
  6. **1)** Update your package.json to indicate amdefine as a dependency:
  7. ```javascript
  8. "dependencies": {
  9. "amdefine": ">=0.1.0"
  10. }
  11. ```
  12. Then run `npm install` to get amdefine into your project.
  13. **2)** At the top of each module that uses define(), place this code:
  14. ```javascript
  15. if (typeof define !== 'function') { var define = require('amdefine')(module) }
  16. ```
  17. **Only use these snippets** when loading amdefine. If you preserve the basic structure,
  18. with the braces, it will be stripped out when using the [RequireJS optimizer](#optimizer).
  19. You can add spaces, line breaks and even require amdefine with a local path, but
  20. keep the rest of the structure to get the stripping behavior.
  21. As you may know, because `if` statements in JavaScript don't have their own scope, the var
  22. declaration in the above snippet is made whether the `if` expression is truthy or not. If
  23. RequireJS is loaded then the declaration is superfluous because `define` is already already
  24. declared in the same scope in RequireJS. Fortunately JavaScript handles multiple `var`
  25. declarations of the same variable in the same scope gracefully.
  26. If you want to deliver amdefine.js with your code rather than specifying it as a dependency
  27. with npm, then just download the latest release and refer to it using a relative path:
  28. [Latest Version](https://github.com/jrburke/amdefine/raw/latest/amdefine.js)
  29. ### amdefine/intercept
  30. Consider this very experimental.
  31. Instead of pasting the piece of text for the amdefine setup of a `define`
  32. variable in each module you create or consume, you can use `amdefine/intercept`
  33. instead. It will automatically insert the above snippet in each .js file loaded
  34. by Node.
  35. **Warning**: you should only use this if you are creating an application that
  36. is consuming AMD style defined()'d modules that are distributed via npm and want
  37. to run that code in Node.
  38. For library code where you are not sure if it will be used by others in Node or
  39. in the browser, then explicitly depending on amdefine and placing the code
  40. snippet above is suggested path, instead of using `amdefine/intercept`. The
  41. intercept module affects all .js files loaded in the Node app, and it is
  42. inconsiderate to modify global state like that unless you are also controlling
  43. the top level app.
  44. #### Why distribute AMD-style modules via npm?
  45. npm has a lot of weaknesses for front-end use (installed layout is not great,
  46. should have better support for the `baseUrl + moduleID + '.js' style of loading,
  47. single file JS installs), but some people want a JS package manager and are
  48. willing to live with those constraints. If that is you, but still want to author
  49. in AMD style modules to get dynamic require([]), better direct source usage and
  50. powerful loader plugin support in the browser, then this tool can help.
  51. #### amdefine/intercept usage
  52. Just require it in your top level app module (for example index.js, server.js):
  53. ```javascript
  54. require('amdefine/intercept');
  55. ```
  56. The module does not return a value, so no need to assign the result to a local
  57. variable.
  58. Then just require() code as you normally would with Node's require(). Any .js
  59. loaded after the intercept require will have the amdefine check injected in
  60. the .js source as it is loaded. It does not modify the source on disk, just
  61. prepends some content to the text of the module as it is loaded by Node.
  62. #### How amdefine/intercept works
  63. It overrides the `Module._extensions['.js']` in Node to automatically prepend
  64. the amdefine snippet above. So, it will affect any .js file loaded by your
  65. app.
  66. ## define() usage
  67. It is best if you use the anonymous forms of define() in your module:
  68. ```javascript
  69. define(function (require) {
  70. var dependency = require('dependency');
  71. });
  72. ```
  73. or
  74. ```javascript
  75. define(['dependency'], function (dependency) {
  76. });
  77. ```
  78. ## RequireJS optimizer integration. <a name="optimizer"></name>
  79. Version 1.0.3 of the [RequireJS optimizer](http://requirejs.org/docs/optimization.html)
  80. will have support for stripping the `if (typeof define !== 'function')` check
  81. mentioned above, so you can include this snippet for code that runs in the
  82. browser, but avoid taking the cost of the if() statement once the code is
  83. optimized for deployment.
  84. ## Node 0.4 Support
  85. If you want to support Node 0.4, then add `require` as the second parameter to amdefine:
  86. ```javascript
  87. //Only if you want Node 0.4. If using 0.5 or later, use the above snippet.
  88. if (typeof define !== 'function') { var define = require('amdefine')(module, require) }
  89. ```
  90. ## Limitations
  91. ### Synchronous vs Asynchronous
  92. amdefine creates a define() function that is callable by your code. It will
  93. execute and trace dependencies and call the factory function *synchronously*,
  94. to keep the behavior in line with Node's synchronous dependency tracing.
  95. The exception: calling AMD's callback-style require() from inside a factory
  96. function. The require callback is called on process.nextTick():
  97. ```javascript
  98. define(function (require) {
  99. require(['a'], function(a) {
  100. //'a' is loaded synchronously, but
  101. //this callback is called on process.nextTick().
  102. });
  103. });
  104. ```
  105. ### Loader Plugins
  106. Loader plugins are supported as long as they call their load() callbacks
  107. synchronously. So ones that do network requests will not work. However plugins
  108. like [text](http://requirejs.org/docs/api.html#text) can load text files locally.
  109. The plugin API's `load.fromText()` is **not supported** in amdefine, so this means
  110. transpiler plugins like the [CoffeeScript loader plugin](https://github.com/jrburke/require-cs)
  111. will not work. This may be fixable, but it is a bit complex, and I do not have
  112. enough node-fu to figure it out yet. See the source for amdefine.js if you want
  113. to get an idea of the issues involved.
  114. ## Tests
  115. To run the tests, cd to **tests** and run:
  116. ```
  117. node all.js
  118. node all-intercept.js
  119. ```
  120. ## License
  121. New BSD and MIT. Check the LICENSE file for all the details.