Search code examples
javascriptrequirejskarma-jasmineconflict

requirejs confilcts with define/require functions


so long story short - I have a problem trying to integrate requireJS in the old project with whole bunch of legacy code that has a definition for global define function which conflicts with requireJS. Problem happens in karma/jasmine test when I load those two scripts.

karma.conf.js

files: [
  <lib that has define global function insight>
  ...
  webappJs + '/vendor/require.js',
  'src/test/js/test-main.js',
  ... 
]

test-main.js

var allTestFiles = [];
var TEST_REGEXP = /.*ng.*test\.js/i;

Object.keys(window.__karma__.files).forEach(function(file) {
    if (TEST_REGEXP.test(file)) {
        // Normalize paths to RequireJS module names.
        allTestFiles.push(file);
    }
});

var config = {
    appDir : '',
    baseUrl : '/base/src/main/webapp',
    dir: "lib",
    deps: allTestFiles,
    callback: window.__karma__.start,
    paths : {
        // Configure alias to full paths
        'angular' : 'lib/angular.min',
        'angularMock' : '/base/src/test/lib/angular-mocks',
        'requireLib': 'lib/require'
    },
    namespace: "appRequire",
    shim : {
        'angular' : {
            deps : [],
            exports : 'angular'
        },
        'angularMock' : {
            deps : ['angular']
        }
    },
    modules: [
        {
            name: "appRequire",
            include: ["requireLib"],
            create: true
        }
    ]
};

require.config (config);

so supposedly now I have to include the appRequire.js file ??? but here is the thing I don't understand how the module will be created, who will create it? Should I not include require.js into list of files

The document doesn't say much about it...it just says that I'll be able to use define/require function as appRequire.define and appRequire.require after that...but what is the mechanism of that? What do I miss here? Cause currently requirejs is still complaining about code that uses the 'old' define function.

Thanks in advance to all of you who can shed some light on that!


Solution

  • fixed this problem manually resolving the conflict in main.js

    if( typeof define !== 'undefined' ) {
        myApp = {
           define : define
        };
        define = void 0;
    }