Require.js required module with index.js

Therefore, I am trying to configure Typescript and Chutzpah for testing purposes. Typescript is configured to output in this format:

define(['require', 'exports', './someModule'], function(require, exports, someModule) {
    //examplecode
});

What works well, the problem occurs when someModule is actually a directory with index.js.

/app
  app.js
  /someModule
    index.js

require.js cannot solve someModule this way and the test fails.

Is there any way to tell require.js that this is a module?

+4
source share
2 answers

RequireJS index.js . RequireJS, , someModule, someModule/index. map require.config:

require.config({
  [ ... ]
  map: {
    '*': {
        someModule: 'someModule/index',
    }
  },
});

, , baseUrl. , , , .

( packages, , , , , , - packages " ", , , , , , .)

+2

map. , , - .

mod, mod!module/someModule, index index!module/someModule, .

define(function(require, exports, module) {
   // loading module/someModule/index.js with `mod!`      
   var someModule = require('mod!module/someModule');

   // whatever this is about ..
   module.exports = { .. };
});

, , paths, :

- app
  - modules
    - someModule/index.js        // the index we want to load
    - someModule/..
    - someModule/..
    - etc
  - plugins
    - mod.js                     // plugin to load a module with index.js

config:

require.config({
   paths: {
      'module': 'app/modules',

      // the plugin we're going to use so 
      // require knows what mod! stands for
      'mod': 'app/plugins/mod.js'
   }
});

, requirejs.org. name "", , load.

//mod.js

(function() {
    define(function () {
        function parse(name, req) {
            return req.toUrl(name + '/index.js');
        }

        return {
            normalize: function(name, normalize) {
                return normalize(name);
            },
            load:function (name, req, load) {
                req([parse(name, req)], function(o) {
                    load(o);
                });
            }
        };
    });
})();

, , config .

+1

Source: https://habr.com/ru/post/1667589/


All Articles