简体   繁体   English

配置Require js以在JavaScript中加载AMD模块

[英]Configuring Require js to load AMD modules in javascript

I am new with writing test cases for testing my javascript files using gulp, mocha, chai. 我刚开始编写测试用例,以使用gulp,mocha,chai测试我的javascript文件。

My file directory 我的档案目录

Project
|-- _src
|   `-- scripts
|       `-- ui
|           |-- ChangePos.js
|           |-- PosRenderer
|           `-- utils
|               |-- Element
|               `-- Config
`-- test
    `-- index.js

utils/ ChangePos.js utils / ChangePos.js

 define(['Config',
            'Element',
            'PosRenderer'

] {
      function() {
      //mycode
      }
    });

It has dependency on Config and Element files. 它依赖于Config和Element文件。

test/index.js 测试/ index.js

var requirejs = require("requirejs");
var chai = require('chai');
var expect = chai.expect;

requirejs.config({
    baseUrl: './../_src/scripts/ui',
    paths: {
        utils: 'utils'
    },

    nodeRequire: require,
    map: {
        '*': {
            'NIT.Cookie': 'utils/Element'
        }
    }
});

    describe('almeObjectConfig', function() {
        var ele, conf, obj;
        before(function (done) {
            requirejs(['utils/Config', 'utils/Element'],
                function (config, element) {
                    console.log('Configuration: ' + config);
                    console.log('Element: ' + element);
                ele = element;
                conf = config;
                done();
            });
        });

I am trying to load ChangePos which has dependency on Element and Config by specifying path 'utils'. 我正在尝试通过指定路径“ utils”来加载对ElementConfig有依赖性的ChangePos

utils: 'utils'

I can load Element explicitly by specifying baseURL as baseUrl:'./../_src/scripts/ui/utils' but here it is checking only inside ui folder and giving an undefined object. 我可以通过将baseURL指定为baseUrl来显式加载Element baseUrl:'./../_src/scripts/ui/utils'但此处仅检查ui文件夹中的内容并提供未定义的对象。

It works if I put all files in ui folder as it is considering the base URL and looking only inside the base folder. 如果我将所有文件都放在ui文件夹中就可以了,因为它正在考虑基本URL并仅在基本文件夹中查找。

I am not sure what am I missing here. 我不确定我在这里想念什么。 Thanks for the help inadvance. 我在这里先向您的帮助表示感谢。

Looking at your configuration I see nothing that would allow ChangePos to load utils/Element as Element and utils/Config as Config . 查看您的配置,我发现没有什么能让ChangePosutils/Element加载为Element并将utils/Config加载为Config In particular, the paths configuration 'utils': 'utils' does not do this, and does nothing at all with the specific configuration you have. 特别是, paths配置'utils': 'utils'不会执行此操作,并且对于您拥有的特定配置完全不执行任何操作。 To get the code you have to work, you'd have to have paths be: 为了获得您必须工作的代码,必须具有以下paths

paths: {
    'Config': 'utils/Config',
    'Element': 'utils/Element'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM