Skip to content

Instantly share code, notes, and snippets.

@terryupton
Last active February 22, 2020 14:37
Show Gist options
  • Save terryupton/b9f71aa89a3c131a438c1dcc4a1cccf6 to your computer and use it in GitHub Desktop.
Save terryupton/b9f71aa89a3c131a438c1dcc4a1cccf6 to your computer and use it in GitHub Desktop.
My idea and approach to Inline JS functions for use with ES6 modules with Laravel Mix and Craft CMS

So I am looking to inline certain JS functions and pieces of code for some third parties (sliders, light-boxes, galleries etc). The reason for this is so I can keep specific and relevant javascript together with the twig code for that specific module/component. Rather than splitting it over a twig file and a separate js file(s).

A good example and use-case of this is Swiper (https://swiperjs.com/).

Below is a basic setup on how this is structured and my ideal approach. Files have been numbered for clarity and order over explanation.

Concerns and Qustions:

  1. Is window.Swiper = Swiper; the best way to do this or is there another/better method?
  2. Do you forsee any issues or pitfalls with this approach?
  3. Is this likely to have any negative effect in terms of performance and inlining JS, if there were potentially a few modules on a page, like a swiper, a gallery and a an-other.
  4. Any general thoughts, considerations or concerns on this method and approach?
//Abridged version of my mix code foucussing on the JS aspect only.
// JS to Concatenate and Minify JS files using Webpack bundler.
mix.js(settings.paths.src.js + 'index.js', settings.paths.build.js)
// Extract splits the JS into Vendor, Manfiest and Index
mix.extract()
//index.js is located in src/js/ folder and is the primary js for the webesite.
//Abridged version of my primary js file foucussing on the above use case.
import Swiper from 'swiper';
//Add the follwiing for each third part include to allow this to be accessible in the global scope.
window.Swiper = Swiper;
//Layout showing the JS code included after the body tag.
//Abridged version of my layout twig file foucussing on the javscaript includes.
//Currently using Twigpack plugin, but the commented code below uses traditional methods.
{{ craft.twigpack.includeJsModule("/js/manifest.js") }}
{{ craft.twigpack.includeJsModule("/js/vendor.js") }}
{{ craft.twigpack.includeJsModule("/js/index.js") }}
{#<script src="/assets/js/manifest.js" type="module"></script>#}
{#<script src="/assets/js/vendor.js" type="module"></script>#}
{#<script src="/assets/js/index.js" type="module"></script>#}
//A shortened version of a twig compenent for easier clarity.
<div class="swiper-container js-swiper-projects w-full h-full relative">
<div class="swiper-wrapper">
SWIPER HTML/TWIG CODFE HERE FOR EACH SLIDE OR LOOP
<div class="swiper-slide">
Slide Code
</div>
</div>
</div>
{% js %}
var swiperProjects = new Swiper('.js-swiper-projects', {
// Optional parameters
loop: true,
autoplay: {
delay: 5000,
stopOnLastSlide: true,
disableOnInteraction: true
},
centeredSlides: true,
speed: 600,
effect: 'fade',
// If we need pagination
pagination: {
el: '.swiper-pagination',
// bulletElement: 'li',
clickable: true,
},
// Navigation arrows
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
{% endjs %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment