requirejs in node

Posted 翻页工

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了requirejs in node相关的知识,希望对你有一定的参考价值。

翻译:http://requirejs.org/docs/node.html

1.为什么要在node环境下用requirejs,node本身就有模块加载器

node下使用的CommonJS模块规范,CommonJS对于浏览器不友好。在server端使用requirejs,可以使server和browser端的代码同构。

2.对于已经使用CommonJS的node模块,可以使用吗?

可以,Requirejs在node下是对应版本叫r.js,如果找不到RequireJS中配置的模块,会按node的方式去搜索模块。所以只需要配置使用AMD的模块,不要这样做:require("./node_modules/foo/foo")

另外,node下的RequireJS只支持本地模块,不支持http模块

3.如何使用?

(1)方法1:npm install requirejs

(2)方法2:下载r.js http://requirejs.org/docs/download.html#rjs

下面介绍用方法1的使用,方法2可以用 require(‘./path/to/r.js‘)替换require(‘requirejs‘) 

var requirejs = require(‘requirejs‘);

requirejs.config({
    //Pass the top-level main.js/index.js require
    //function to requirejs so that node modules
    //are loaded relative to the top-level JS file.
    nodeRequire: require
});

requirejs([‘foo‘, ‘bar‘],
function   (foo,   bar) {
    //foo and bar are loaded according to requirejs
    //config, but if not found, then node‘s require
    //is used to load the module.
});

4.如何在用AMD或RequireJS建立node模块

if (typeof define !== ‘function‘) {
    var define = require(‘amdefine‘)(module);
}

define(function(require) {
    var dep = require(‘dependency‘);

    //The value returned from the function is
    //used as the module export visible to Node.
    return function () {};
});

5.如何在模块中使用AMD,但输出为一个CommonJS模块

var requirejs = require(‘requirejs‘);

requirejs.config({
    //Use node‘s special variable __dirname to
    //get the directory containing this file.
    //Useful if building a library that will
    //be used in node but does not require the
    //use of node outside
    baseUrl: __dirname,

    //Pass the top-level main.js/index.js require
    //function to requirejs so that node modules
    //are loaded relative to the top-level JS file.
    nodeRequire: require
});

//foo and bar are loaded according to requirejs
//config, and if found, assumed to be an AMD module.
//If they are not found via the requirejs config,
//then node‘s require is used to load the module,
//and if found, the module is assumed to be a
//node-formatted module. Note: this synchronous
//style of loading a module only works in Node.
var foo = requirejs(‘foo‘);
var bar = requirejs(‘bar‘);

//Now export a value visible to Node.
module.exports = function () {};

6.在node模块中使用requirejs的optimizer(代替命令行运行r.js的方式)

var requirejs = require(‘requirejs‘);

var config = {
    baseUrl: ‘../appDir/scripts‘,
    name: ‘main‘,
    out: ‘../build/main-built.js‘
};

requirejs.optimize(config, function (buildResponse) {
    //buildResponse is just a text output of the modules
    //included. Load the built file for the contents.
    //Use config.out to get the optimized file contents.
    var contents = fs.readFileSync(config.out, ‘utf8‘);
}, function(err) {
    //optimization err callback
});

 

以上是关于requirejs in node的主要内容,如果未能解决你的问题,请参考以下文章

RequireJS 中本地需求和全局需求的区别

reactjs怎么和requirejs结合使用

如何使用 RequireJS 加载前端 NPM/Yarn 包

RequireJS 与 SeaJS 的异同

优化 RequireJS 项目(合并与压缩) 已翻译100%

如何配置Intern 4以使用RequireJS?