OK, I think I may have solved it and I believe that the documentation is ambiguous and needs updating to clarify the process.
I moved my requirejs-config.js
from within the web/js
and web
directories respectively for both Magento_Theme
and the root of my theme at <Vendor>/<theme>
and now my RequireJS config is merged into the main requirejs-config.js
with all other includes.
So, it appears that you have to include the requirejs-config.js
file in the following locations based on the theme/module requirements.
Theme Level
app/design/frontend/<Vendor>/<theme>/requirejs-config.js
Module Level
app/design/frontend/<Vendor>/<theme>/<Module_Name>/requirejs-config.js
So, in your requirejs-config.js
for your theme you should map your component to a path and then use shim
to declare any dependencies:
var config = {
map: {
'component': 'js/component'
},
shim: {
'component': {
deps: ['jquery']
}
}
};
Then you'll need to create a template to hold the initialisation of the components via a <script>
tag (unless you attach it directly to an element within a .phtml file) if this is the route you want to go down, include the following content:
<script type="text/x-magento-init">
{
"*": {
"js/component": {} // Not entirely sure what {} is and what I'm passing here
}
}
</script>
Alternatively, bind it to an element:
<script type="text/x-magento-init">
{
"#element": {
"js/component": {} // Not entirely sure what {} is and what I'm passing here
}
}
</script>
Then simply include the .phtml template in your layout instructions, for example, I placed mine within the default.xml
located at app/design/frontend/<Vendor>/<theme>/Magento_Theme/layout
under the body node and referenced:
<block class="Magento\Framework\View\Element\Template" name="theme.js" template="Magento_Theme::html/js.phtml" />