Questions about Best Practices with JS in AEM - Webpack, jQuery, Third-Party JS
We have both classic and Touch UI implementations of AEM, so I have some experience dealing with these issues in the classic environment. Right now, we are trying to find the best approach with Touch UI and Webpack. We have a frontend that is a custom framework along with custom components (that need their own JS and maybe CSS). I'd appreciate any help for the following:
- For third-party jQuery-dependent vendor JavaScript (not installed through Node), we have tried adding them in Webpack, but then we encounter the standard problem of making them available in the global namespace for custom component JS to access. For example, vendor JS like DataTables and Bootstrap-Select have globally available methods $().DataTable() or $().selectpicker() that are not available when added in Webpack. This appears to be by design with Webpack.
- How should vendor JS be loaded, especially JS that builds on jQuery?
- Currently, clientlib-site JS gets loaded at the bottom of the page. Custom component clientlibs for JS gets added within the page where the component is added. How do I solve issues with global functions not existing for the custom component JS because they have not been loaded yet (because that JS is at the bottom of the page)?
- How could we specify vendor JS (e.g., DataTables) to be loaded only when a specific custom component is used on a page?
- For custom components with their own JS,
- How do we architect the JS file structure? Should the component JS be housed in the component's folder within apps as it has historically been? Or should all JS now be housed under Webpack's folder structure to enable modular JavaScript features?
- If the JS is housed in Webpack and built out inside the site.js file, how can we change the structure in a way that allows the component JS to be loaded only on the pages where the component is used rather than be loaded for all pages?