requirejs in node
2016-05-25 11:34
495 查看
翻译: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')
4.如何在用AMD或RequireJS建立node模块
5.如何在模块中使用AMD,但输出为一个CommonJS模块
6.在node模块中使用requirejs的optimizer(代替命令行运行r.js的方式)
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 });
相关文章推荐
- "same tree" and "Delete Node in a Linked List"
- Promise.all解决异步条件的收敛
- 删除node_modules文件下依赖层次过多的解决方法
- 整理node中的变量
- leetcode 24. Swap Nodes in Pairs
- 《深入浅出nodeJS》学习笔记-第一章Node简介
- node.js简单服务器创建代码
- 51NOde+大数相乘+注意找规律
- Mac下配置Nodejs开发环境
- Windows部署LeanCloud本地命令行工具
- node.js实现微博系统在express3.0+版本下的实现——所遇问题
- nodejs遇到的问题
- Leetcode 237. Delete Node in a Linked List
- NotFoundError: Node was not found
- nodejs案例-HelloWorld
- nodejs 命令篇
- node实现rar格式压缩
- nodejs redirect
- NodeJS、NPM安装配置步骤(windows版本)
- 珠峰 - 郭永峰react课程 node es6 babel学习笔记