Search code examples
javascriptgulpyoutube-javascript-apilaravel-elixir

How to use YouTube API with Gulp (minified file doesn't contain YouTube callback methods)


I can't use YouTube Player API methods because they are deleted by Laravel Elixir (Gulp) in my minified .js file.

Methods such as below are not included in my my minified .js file because they aren't called in .js files I created (In fact, they are called in YouTube iFrame API).

onYouTubeIframeAPIReady
onPlayerReady
onPlayerStateChange

Any solution for this problem ?


Solution

  • The only solution for this is to call onYouTubeIframeAPIReady().

    To do that, I added a condition :

    function onYouTubeIframeAPIReady(init) {
       if (init === "init") {
           return;
       }
       // Init your YouTube Player here, after the condition.
    }
    
    // First call here : The minifier notices the method is used, so it won't delete it
    onYouTubeIframeAPIReady("init");
    
    // Later, YouTube API will call this method
    // (YouTube doesn't pass any parameters, so the player will be initialized in this case)
    // YouTube API : onYouTubeIframeAPIReady();