admin管理员组文章数量:1415119
How can I make the node.js require
function in pure JavaScript? I have searched the web for any solutions but I have found none.
Something along the lines of this:
function require(path){
//Code
return data;
}
Some of the posts I have already seen:
- How does require() in node.js work?
- Node.js "require" function and parameters
How can I make the node.js require
function in pure JavaScript? I have searched the web for any solutions but I have found none.
Something along the lines of this:
function require(path){
//Code
return data;
}
Some of the posts I have already seen:
- How does require() in node.js work?
- Node.js "require" function and parameters
- This looks interesting. Why is it necessary to implement the require function in pure JavaScript? – Anderson Green Commented Jan 27, 2014 at 2:17
- It entirely depends on how you're able to locate & load other scripts. – SLaks Commented Jan 27, 2014 at 2:36
3 Answers
Reset to default 1CommonJS functionality is implemented in the Curl Library As pointed out in other answers RequireJS
follows the AMD spec which is different than Node's CommonJS var foo = require('foo');
spec.
Curl Features at a glance:
- Loads AMD-formatted javascript modules in parallel
- Loads CommonJS/node modules (v1.1 when wrapped in a define())
- Loads CommonJS/node modules (unwrapped when using the cjsm11 loader)
- Loads non-AMD javascript files in parallel, too.
- Loads CSS files and text files in parallel
- Waits for dependencies (js, css, text, etc) before executing javascript
- Waits for domReady, if desired
- Allows for virtually limitless binations of files and dependencies
- Tested with Safari 5+, IE6+, and recent Chrome, FF, Opera
Browserify does not implement CommonJS straight out but rather transpiles your CommonJS style code into a single bundle.js
file which is included in the html.
Webmake seems to offer similar functionality to Browserify while also allowing for other file types.
In a nutshell CommonJS works like this:
- Maintain a map of identifiers that correspond to loaded modules.
- When a
require('lib')
is encountered, check the list to see if it has been loaded. If the library has not been loaded (it's key is not in the library), load the script via XHRRequest. If the library has been loaded, return the value that corresponds to thekey
.
This is why required modules are always singletons.
RequireJS uses AMD loader, whereas node.js uses CommonJS pattern loading.
Browserify allows you to wrap up all of your code in a build step so you can have require()
statements in a browser.
To roll your own (which I do not suggest, as Browserify is already a battle-tested, popular library), you would have to map all of your modules on your own and load it upfront, or load them synchronously (as that's necessary for CommonJS spec), in a blocking (and undesirable) way.
code
in your function should contain full module resolution logic, which may not be that light and simple. You can check source code of module
module which is responsible for that in node.js -> https://github./joyent/node/blob/master/lib/module.js
There are various tools which help you port CJS modules outside of node.js (they provide require
function for you): Most popular is Browserify, but you can also check Webmake (I'm the author).
本文标签: How to make the nodejs require function in pure JavaScriptStack Overflow
版权声明:本文标题:How to make the node.js require function in pure JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745220145a2648356.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论