jQuery with WordPress and Webpack












1















I've just setup Webpack in a WordPress-theme.



The theme uses Bootstrap, and in that regard, the bootstrap.min.js has previously been loaded like this:



wp_enqueue_script( 'bootstrapjs', get_stylesheet_directory_uri() . '/assets/js/bootstrap.min.js', array( 'jquery' ) );


WordPress has jQuery baked into it, so by specifying jquery as a dependency, this works and everybody is happy.



However, now I'm trying to use Webpack. I'm still getting used to it, - and it was my impression that it's good style to compile as many scripts together into one, to limit the request the server has to make to get all the resources.



So I'm torn... Do I



SOLUTION 1) Not compile Bootstrap.min.js using Webpack.
... And just put that file straight in the assets-folder and enqueue it (bypassing Webpack completely).

Upside: I can just use WordPress' jQuery, so that doesn't need to be loaded twice (which could also cause problems).

Downside: Cluttered file-structure, since some files are loaded through Webpack and some aren't. This will also result in a higher number of files (more server-requests).



SOLUTION 2) Compile Bootstrap.min.js using Webpack. This requires me to import jquery and popper, so I can require the Bootstrap.min.js-file without getting an error.

Upside: Better file structure and fewer files that the server needs to request.

Downside: Jquery needs to be loaded twice (which can be a mess). And that can also result in larger file-sizes.










share|improve this question





























    1















    I've just setup Webpack in a WordPress-theme.



    The theme uses Bootstrap, and in that regard, the bootstrap.min.js has previously been loaded like this:



    wp_enqueue_script( 'bootstrapjs', get_stylesheet_directory_uri() . '/assets/js/bootstrap.min.js', array( 'jquery' ) );


    WordPress has jQuery baked into it, so by specifying jquery as a dependency, this works and everybody is happy.



    However, now I'm trying to use Webpack. I'm still getting used to it, - and it was my impression that it's good style to compile as many scripts together into one, to limit the request the server has to make to get all the resources.



    So I'm torn... Do I



    SOLUTION 1) Not compile Bootstrap.min.js using Webpack.
    ... And just put that file straight in the assets-folder and enqueue it (bypassing Webpack completely).

    Upside: I can just use WordPress' jQuery, so that doesn't need to be loaded twice (which could also cause problems).

    Downside: Cluttered file-structure, since some files are loaded through Webpack and some aren't. This will also result in a higher number of files (more server-requests).



    SOLUTION 2) Compile Bootstrap.min.js using Webpack. This requires me to import jquery and popper, so I can require the Bootstrap.min.js-file without getting an error.

    Upside: Better file structure and fewer files that the server needs to request.

    Downside: Jquery needs to be loaded twice (which can be a mess). And that can also result in larger file-sizes.










    share|improve this question



























      1












      1








      1








      I've just setup Webpack in a WordPress-theme.



      The theme uses Bootstrap, and in that regard, the bootstrap.min.js has previously been loaded like this:



      wp_enqueue_script( 'bootstrapjs', get_stylesheet_directory_uri() . '/assets/js/bootstrap.min.js', array( 'jquery' ) );


      WordPress has jQuery baked into it, so by specifying jquery as a dependency, this works and everybody is happy.



      However, now I'm trying to use Webpack. I'm still getting used to it, - and it was my impression that it's good style to compile as many scripts together into one, to limit the request the server has to make to get all the resources.



      So I'm torn... Do I



      SOLUTION 1) Not compile Bootstrap.min.js using Webpack.
      ... And just put that file straight in the assets-folder and enqueue it (bypassing Webpack completely).

      Upside: I can just use WordPress' jQuery, so that doesn't need to be loaded twice (which could also cause problems).

      Downside: Cluttered file-structure, since some files are loaded through Webpack and some aren't. This will also result in a higher number of files (more server-requests).



      SOLUTION 2) Compile Bootstrap.min.js using Webpack. This requires me to import jquery and popper, so I can require the Bootstrap.min.js-file without getting an error.

      Upside: Better file structure and fewer files that the server needs to request.

      Downside: Jquery needs to be loaded twice (which can be a mess). And that can also result in larger file-sizes.










      share|improve this question
















      I've just setup Webpack in a WordPress-theme.



      The theme uses Bootstrap, and in that regard, the bootstrap.min.js has previously been loaded like this:



      wp_enqueue_script( 'bootstrapjs', get_stylesheet_directory_uri() . '/assets/js/bootstrap.min.js', array( 'jquery' ) );


      WordPress has jQuery baked into it, so by specifying jquery as a dependency, this works and everybody is happy.



      However, now I'm trying to use Webpack. I'm still getting used to it, - and it was my impression that it's good style to compile as many scripts together into one, to limit the request the server has to make to get all the resources.



      So I'm torn... Do I



      SOLUTION 1) Not compile Bootstrap.min.js using Webpack.
      ... And just put that file straight in the assets-folder and enqueue it (bypassing Webpack completely).

      Upside: I can just use WordPress' jQuery, so that doesn't need to be loaded twice (which could also cause problems).

      Downside: Cluttered file-structure, since some files are loaded through Webpack and some aren't. This will also result in a higher number of files (more server-requests).



      SOLUTION 2) Compile Bootstrap.min.js using Webpack. This requires me to import jquery and popper, so I can require the Bootstrap.min.js-file without getting an error.

      Upside: Better file structure and fewer files that the server needs to request.

      Downside: Jquery needs to be loaded twice (which can be a mess). And that can also result in larger file-sizes.







      jquery wordpress webpack






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 14 at 13:08







      Zeth

















      asked Jan 2 at 19:16









      ZethZeth

      72511532




      72511532
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I just realized that I can just deregister jQuery using this:



          function custom_head_cleanup(){
          if( ! is_admin() ){
          wp_deregister_script( 'jquery' );
          }
          }
          add_action( 'init', 'custom_head_cleanup' );


          I assume that I then use solution 2.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011957%2fjquery-with-wordpress-and-webpack%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I just realized that I can just deregister jQuery using this:



            function custom_head_cleanup(){
            if( ! is_admin() ){
            wp_deregister_script( 'jquery' );
            }
            }
            add_action( 'init', 'custom_head_cleanup' );


            I assume that I then use solution 2.






            share|improve this answer




























              0














              I just realized that I can just deregister jQuery using this:



              function custom_head_cleanup(){
              if( ! is_admin() ){
              wp_deregister_script( 'jquery' );
              }
              }
              add_action( 'init', 'custom_head_cleanup' );


              I assume that I then use solution 2.






              share|improve this answer


























                0












                0








                0







                I just realized that I can just deregister jQuery using this:



                function custom_head_cleanup(){
                if( ! is_admin() ){
                wp_deregister_script( 'jquery' );
                }
                }
                add_action( 'init', 'custom_head_cleanup' );


                I assume that I then use solution 2.






                share|improve this answer













                I just realized that I can just deregister jQuery using this:



                function custom_head_cleanup(){
                if( ! is_admin() ){
                wp_deregister_script( 'jquery' );
                }
                }
                add_action( 'init', 'custom_head_cleanup' );


                I assume that I then use solution 2.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 14 at 13:07









                ZethZeth

                72511532




                72511532
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011957%2fjquery-with-wordpress-and-webpack%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas