zoukankan      html  css  js  c++  java
  • 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
    });
  • 相关阅读:
    .NET中对资源文件的使用简介
    jQuery框架学习
    asp.net 性能优化(转)
    走向ASP.NET架构设计(转)
    memcached全面剖析–5. memcached的应用和兼容程序
    ASP.NET MVC
    memcached全面剖析–3.memcached的删除机制和发展方向
    memcached完全剖析–1. memcached的基础
    memcached全面剖析–4. memcached的分布式算法
    memcached全面剖析–2.理解memcached的内存存储
  • 原文地址:https://www.cnblogs.com/fanyegong/p/5526379.html
Copyright © 2011-2022 走看看