webpack + javascript

0

Situation: When trying to make a new of a javascript function, it gives an error if the file was compiled with webpack.

Use case where it works (does not pass through webpack) SiteController.js file:

var SiteController=function() {
};

Execution inside the body of my page:

<script type="text/javascript">
$( document ).ready(function() {
                new SiteController();
});
</script>

Use case where it does not work, passing the javascript files by webpack:

The javascript file after being compiled by webpack, bundle_js_app.js:

eval("\r\nvar SiteController=function() {\r\n\t\r\n};\r\n\n\n//# sourceURL=webpack:///./resources/assets/frontend/js/bin/siteController.js?");

The execution on the page remains the same:

<script type="text/javascript">
$( document ).ready(function() {
                new SiteController();
});
</script>

The error:

  

(index): 205 Uncaught ReferenceError: SiteController is not defined       at (index): 205

I do not know if I should do the new one in a different way or where the solution may be

    
asked by Pablo 24.04.2018 в 18:39
source

1 answer

0

It is possible that the order in which you have put the scripts on your page is the wrong one, because here you can see that there is no problem in using the generated code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
eval("\r\nvar SiteController=function() {\r\n\tconsole.log('Hola Mundo');\r\n};\r\n\n\n//# sourceURL=webpack:///./resources/assets/frontend/js/bin/siteController.js?");
</script>
<script type="text/javascript">
$( document ).ready(function() {
            new SiteController();
});
</script>
    
answered by 24.04.2018 в 18:56