Magento2 Theme Javascript Requirejs – Magento 2: requirejs-config.js in Themes

javascriptmagento2requirejstheme

Is it possible to include a requirejs-config.js file (or otherwise configure RequireJS) via a Magento theme? Or is the capability reserved for Magento modules? The dev docs information on theme structure is ambiguous to this point.

Dev docs doesn't mention anything about RequireJS — however, themes do include a web which means javascript can be bundled with them. If javascript can be bundled with a theme, that means a RequireJS module can be bundled with a theme, and if a RequireJS module can be bundled with a theme that module may requires a specific RequireJS configuration.

My naive assumption would be themes would have this capability, but I haven't been able to find any documentation on this one way or the other, and haven't an afternoon free to spend doing the required code spelunking on Magento's requirejs-config.js file including.

Best Answer

you can actually include a require-config.js in your themes Module dirs.

The problem is (actually for our frontend team) that there seems to be no possibility, to override the config, but to extend the config.

So, to take the Magento_Theme module for an example here, if you add a require-config.js under <theme_base_dir>/Magento_Theme dir, the config will be added to the generated require-config.js file and also the config from the Magento_Theme module will be added.

To answer your question I also tried to add a require-config.js under the themes <theme_base_dir>/web dir and also under the themes root dir. Both did not work. update: actually according to the answer below, it is possible by placing it into the theme base dir

So the answer would be basically yes, since you could add any js requirement under any module (theme related js files may be best placed under the <theme_base_dir>/Magento_Theme dir)

Although I would say, there should be the possibility to add a theme related require-config.js outside of any Module (maybe you deactivate the given module) and also it should be possible to override a modules require-config.js.

Both seem not possible atm.

===UPDATE===

actually it seems possible to have a theme specific require-config.js. See @Gareth Daine's Answer below

Related Topic