• 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
    });
  • 相关阅读:
    Django学习笔记(进阶篇)
    Django学习笔记(基础篇)
    vim的使用
    PyCharm默认文件头部的设置
    Django的内置登录、退出、修改密码方法
    vbox manjaro 无法挂载共享文件 解决办法
    vbox虚拟机 无法启动 新任务
    Ubuntu安装输入法
    安装 protobuf
    Manjaro.常用命令
  • 原文地址:https://www.cnblogs.com/fanyegong/p/5526379.html
Copyright © 2020-2023  润新知