How to use webpack modules on inline javascriptHow to validate an email address in JavaScript?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Get consecutive integer number ranges from list of int

How do I check if a string is entirely made of the same substring?

Is Diceware more secure than a long passphrase?

Why do games have consumables?

555 timer FM transmitter

Why does nature favour the Laplacian?

Is it idiomatic to construct against `this`

As an international instructor, should I openly talk about my accent?

How exactly does Hawking radiation decrease the mass of black holes?

bldc motor, esc and battery draw, nominal vs peak

Can someone publish a story that happened to you?

Elements other than carbon that can form many different compounds by bonding to themselves?

Can an Area of Effect spell cast outside a Prismatic Wall extend inside it?

Dynamic SOQL query relationship with field visibility for Users

Should the Death Curse affect an undead PC in the Tomb of Annihilation adventure?

Apply MapThread to all but one variable

Extension of 2-adic valuation to the real numbers

What does ゆーか mean?

Pre-plastic human skin alternative

How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?

How much cash can I safely carry into the USA and avoid civil forfeiture?

Don’t seats that recline flat defeat the purpose of having seatbelts?

Rivers without rain

How do I reattach a shelf to the wall when it ripped out of the wall?



How to use webpack modules on inline javascript


How to validate an email address in JavaScript?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I'm an inexperienced web dev, and especially so when it comes to JavaScript. Lately I've been trying to fix that, and since Laravel's my framework of choice I'm trying to change from the "old" style of:



<script src="script1.js"></script>
<script src="script2.js"></script>



to something better. And using Laravel, the logical step is using Mix, which relies on Webpack.



After a lot of trial and error, I've managed to get the basic setup working. I have a settings.js file for changing the modules' default settings when necessary, and an app.js file for custom functions I'd like to make available on every page. I'm also separating the vendor modules to a vendors.js file.



So the bootstrap.js file has:



window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
[...]
require('bootstrap-datepicker');
require('bootstrap-year-calendar');


The webpack.mix.js file has:



mix.autoload( jquery: ['$', 'jQuery', 'window.jQuery']);
mix.webpackConfig( resolve: alias: jquery: "jquery/src/jquery" );
mix.js([
'resources/js/app.js',
'resources/js/settings.js'
], 'public/js')
.extract([
'lodash',
'jquery',
[…]
'bootstrap-datepicker',
'bootstrap-year-calendar'
]);


And the master layout has the following scripts right before `:



<script src=" mix('/js/manifest.js') "></script>
<script src=" mix('/js/vendor.js') "></script>
<script src=" mix('/js/app.js') "></script>


This setup seems to be working, but there's one last issue I just can't seem to figure out. I also add some javascript on specific pages, for functionality limited to those pages (initialising pickers with non-default settings, custom form behaviour, etc). So some pages have an extra <script type="text/javascript"> [custom code] </script> section before </body>.



However, code there doesn't have access to modules: $('.input-daterange').datepicker(); should initialise an input as a bootstrap-datepicker instance, but it doesn't. Of course, using the "old style" it worked. Now, I'm getting a TypeError: undefined is not a function (near '...$('.input-daterange').datepicker...') error.



So my issue is the following: how can I import the necessary modules to be used in the script embedded in the HTML file?



P.S.: when I was pretty much finished writing this question, I happened to stumble on the solution. I decided to post it nevertheless because a) it might help someone else, and b) if there's a better way to approach this, I'd love to hear about it.










share|improve this question




























    1















    I'm an inexperienced web dev, and especially so when it comes to JavaScript. Lately I've been trying to fix that, and since Laravel's my framework of choice I'm trying to change from the "old" style of:



    <script src="script1.js"></script>
    <script src="script2.js"></script>



    to something better. And using Laravel, the logical step is using Mix, which relies on Webpack.



    After a lot of trial and error, I've managed to get the basic setup working. I have a settings.js file for changing the modules' default settings when necessary, and an app.js file for custom functions I'd like to make available on every page. I'm also separating the vendor modules to a vendors.js file.



    So the bootstrap.js file has:



    window._ = require('lodash');
    window.$ = window.jQuery = require('jquery');
    [...]
    require('bootstrap-datepicker');
    require('bootstrap-year-calendar');


    The webpack.mix.js file has:



    mix.autoload( jquery: ['$', 'jQuery', 'window.jQuery']);
    mix.webpackConfig( resolve: alias: jquery: "jquery/src/jquery" );
    mix.js([
    'resources/js/app.js',
    'resources/js/settings.js'
    ], 'public/js')
    .extract([
    'lodash',
    'jquery',
    […]
    'bootstrap-datepicker',
    'bootstrap-year-calendar'
    ]);


    And the master layout has the following scripts right before `:



    <script src=" mix('/js/manifest.js') "></script>
    <script src=" mix('/js/vendor.js') "></script>
    <script src=" mix('/js/app.js') "></script>


    This setup seems to be working, but there's one last issue I just can't seem to figure out. I also add some javascript on specific pages, for functionality limited to those pages (initialising pickers with non-default settings, custom form behaviour, etc). So some pages have an extra <script type="text/javascript"> [custom code] </script> section before </body>.



    However, code there doesn't have access to modules: $('.input-daterange').datepicker(); should initialise an input as a bootstrap-datepicker instance, but it doesn't. Of course, using the "old style" it worked. Now, I'm getting a TypeError: undefined is not a function (near '...$('.input-daterange').datepicker...') error.



    So my issue is the following: how can I import the necessary modules to be used in the script embedded in the HTML file?



    P.S.: when I was pretty much finished writing this question, I happened to stumble on the solution. I decided to post it nevertheless because a) it might help someone else, and b) if there's a better way to approach this, I'd love to hear about it.










    share|improve this question
























      1












      1








      1








      I'm an inexperienced web dev, and especially so when it comes to JavaScript. Lately I've been trying to fix that, and since Laravel's my framework of choice I'm trying to change from the "old" style of:



      <script src="script1.js"></script>
      <script src="script2.js"></script>



      to something better. And using Laravel, the logical step is using Mix, which relies on Webpack.



      After a lot of trial and error, I've managed to get the basic setup working. I have a settings.js file for changing the modules' default settings when necessary, and an app.js file for custom functions I'd like to make available on every page. I'm also separating the vendor modules to a vendors.js file.



      So the bootstrap.js file has:



      window._ = require('lodash');
      window.$ = window.jQuery = require('jquery');
      [...]
      require('bootstrap-datepicker');
      require('bootstrap-year-calendar');


      The webpack.mix.js file has:



      mix.autoload( jquery: ['$', 'jQuery', 'window.jQuery']);
      mix.webpackConfig( resolve: alias: jquery: "jquery/src/jquery" );
      mix.js([
      'resources/js/app.js',
      'resources/js/settings.js'
      ], 'public/js')
      .extract([
      'lodash',
      'jquery',
      […]
      'bootstrap-datepicker',
      'bootstrap-year-calendar'
      ]);


      And the master layout has the following scripts right before `:



      <script src=" mix('/js/manifest.js') "></script>
      <script src=" mix('/js/vendor.js') "></script>
      <script src=" mix('/js/app.js') "></script>


      This setup seems to be working, but there's one last issue I just can't seem to figure out. I also add some javascript on specific pages, for functionality limited to those pages (initialising pickers with non-default settings, custom form behaviour, etc). So some pages have an extra <script type="text/javascript"> [custom code] </script> section before </body>.



      However, code there doesn't have access to modules: $('.input-daterange').datepicker(); should initialise an input as a bootstrap-datepicker instance, but it doesn't. Of course, using the "old style" it worked. Now, I'm getting a TypeError: undefined is not a function (near '...$('.input-daterange').datepicker...') error.



      So my issue is the following: how can I import the necessary modules to be used in the script embedded in the HTML file?



      P.S.: when I was pretty much finished writing this question, I happened to stumble on the solution. I decided to post it nevertheless because a) it might help someone else, and b) if there's a better way to approach this, I'd love to hear about it.










      share|improve this question














      I'm an inexperienced web dev, and especially so when it comes to JavaScript. Lately I've been trying to fix that, and since Laravel's my framework of choice I'm trying to change from the "old" style of:



      <script src="script1.js"></script>
      <script src="script2.js"></script>



      to something better. And using Laravel, the logical step is using Mix, which relies on Webpack.



      After a lot of trial and error, I've managed to get the basic setup working. I have a settings.js file for changing the modules' default settings when necessary, and an app.js file for custom functions I'd like to make available on every page. I'm also separating the vendor modules to a vendors.js file.



      So the bootstrap.js file has:



      window._ = require('lodash');
      window.$ = window.jQuery = require('jquery');
      [...]
      require('bootstrap-datepicker');
      require('bootstrap-year-calendar');


      The webpack.mix.js file has:



      mix.autoload( jquery: ['$', 'jQuery', 'window.jQuery']);
      mix.webpackConfig( resolve: alias: jquery: "jquery/src/jquery" );
      mix.js([
      'resources/js/app.js',
      'resources/js/settings.js'
      ], 'public/js')
      .extract([
      'lodash',
      'jquery',
      […]
      'bootstrap-datepicker',
      'bootstrap-year-calendar'
      ]);


      And the master layout has the following scripts right before `:



      <script src=" mix('/js/manifest.js') "></script>
      <script src=" mix('/js/vendor.js') "></script>
      <script src=" mix('/js/app.js') "></script>


      This setup seems to be working, but there's one last issue I just can't seem to figure out. I also add some javascript on specific pages, for functionality limited to those pages (initialising pickers with non-default settings, custom form behaviour, etc). So some pages have an extra <script type="text/javascript"> [custom code] </script> section before </body>.



      However, code there doesn't have access to modules: $('.input-daterange').datepicker(); should initialise an input as a bootstrap-datepicker instance, but it doesn't. Of course, using the "old style" it worked. Now, I'm getting a TypeError: undefined is not a function (near '...$('.input-daterange').datepicker...') error.



      So my issue is the following: how can I import the necessary modules to be used in the script embedded in the HTML file?



      P.S.: when I was pretty much finished writing this question, I happened to stumble on the solution. I decided to post it nevertheless because a) it might help someone else, and b) if there's a better way to approach this, I'd love to hear about it.







      javascript webpack laravel-mix






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 9 at 9:20









      user7401727user7401727

      257




      257






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Doing the import not on the embedded javascript, but on either my app.js or settings.js files, made it work:



          import 'bootstrap-datepicker';
          import 'bootstrap-year-calendar';


          However, making the imports on settings.js rather than app.js seems to cause some issues, as it broke some separate functionality. I'm not sure if it's because app.js is listed first in the webpack.mix.js file (as shown below), but nevertheless the file where you make the imports does matter.



          mix.js([
          'resources/js/app.js',
          'resources/js/settings.js'
          ], 'public/js')





          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%2f55075810%2fhow-to-use-webpack-modules-on-inline-javascript%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














            Doing the import not on the embedded javascript, but on either my app.js or settings.js files, made it work:



            import 'bootstrap-datepicker';
            import 'bootstrap-year-calendar';


            However, making the imports on settings.js rather than app.js seems to cause some issues, as it broke some separate functionality. I'm not sure if it's because app.js is listed first in the webpack.mix.js file (as shown below), but nevertheless the file where you make the imports does matter.



            mix.js([
            'resources/js/app.js',
            'resources/js/settings.js'
            ], 'public/js')





            share|improve this answer



























              0














              Doing the import not on the embedded javascript, but on either my app.js or settings.js files, made it work:



              import 'bootstrap-datepicker';
              import 'bootstrap-year-calendar';


              However, making the imports on settings.js rather than app.js seems to cause some issues, as it broke some separate functionality. I'm not sure if it's because app.js is listed first in the webpack.mix.js file (as shown below), but nevertheless the file where you make the imports does matter.



              mix.js([
              'resources/js/app.js',
              'resources/js/settings.js'
              ], 'public/js')





              share|improve this answer

























                0












                0








                0







                Doing the import not on the embedded javascript, but on either my app.js or settings.js files, made it work:



                import 'bootstrap-datepicker';
                import 'bootstrap-year-calendar';


                However, making the imports on settings.js rather than app.js seems to cause some issues, as it broke some separate functionality. I'm not sure if it's because app.js is listed first in the webpack.mix.js file (as shown below), but nevertheless the file where you make the imports does matter.



                mix.js([
                'resources/js/app.js',
                'resources/js/settings.js'
                ], 'public/js')





                share|improve this answer













                Doing the import not on the embedded javascript, but on either my app.js or settings.js files, made it work:



                import 'bootstrap-datepicker';
                import 'bootstrap-year-calendar';


                However, making the imports on settings.js rather than app.js seems to cause some issues, as it broke some separate functionality. I'm not sure if it's because app.js is listed first in the webpack.mix.js file (as shown below), but nevertheless the file where you make the imports does matter.



                mix.js([
                'resources/js/app.js',
                'resources/js/settings.js'
                ], 'public/js')






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 9 at 9:20









                user7401727user7401727

                257




                257





























                    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%2f55075810%2fhow-to-use-webpack-modules-on-inline-javascript%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

                    Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                    Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                    Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved