2

It is possible to use RequireJS to inject some modules into a custom script, for example:

define([
    'jquery',
    'tinymce',
    'Magento_Ui/js/modal/modal'
], function($, modal, tinymce) {
    "use strict";
    ...
});

However, it looks like in Magento 2.3 the module tinymce is no longer supported, but it is possible to use tinymce4. However, that is not backwards-compatible.

The question is, is it possible to check whether a module exists before defining it? It have tried with require.defined and require.specified but they appear to check against what's defined with define(), not what exists.

Ideally I'd want something like this:

var tinymce_module = requirejs.moduleExists('tinymce4') ? 'tinymce4' : 'tinymce';
define([
    'jquery',
    tinymce_module,
    'Magento_Ui/js/modal/modal'
], function($, modal, tinymce) {
    "use strict";
    ...
});

P.S. Any help regarding loading TinyMCE across Magento 2 versions specifically would be appreciated in the comments as well.

1 Answer 1

1

Yes, it's available to require ext js into define.

You have a js file named abc.js stored at /Nspace/Module/view/frontend/web/js/abc.js

Solution 1 Using Nspace/Module/view/frontend/requirejs-config.js

First Step: create Nspace/Module/view/frontend/requirejs-config.js

map: {
        '*': {
            'abc': 'Nspace_Module::js/abc',
         }
}

If your js file required jquery using shim option for depending jquery.

var config = {    
    map: {
            '*': {
                'abc': 'Nspace_Module::js/abc',
             }
    },
    shim: {
        'abc': {
            deps: ['jquery']
    }
}

where you want to require abc js include 'abc' to your required list.

define([
   'abc',
], function(abc) {
    "use strict";
    ...
});

More info please read here: https://devdocs.magento.com/guides/v2.3/javascript-dev-guide/javascript/js-resources.html

good look!.

1
  • Thanks, but I'm not sure I understand from your answer how to require different JS files based on a condition that I set. The condition is not a dependency, it's arbitrary, e.g. a configuration, or a Magento version, or anything else.
    – Ynhockey
    Commented Mar 19, 2019 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.