Thanks for this @bobbybouwmann. I am actually already compiling my JS like this.
I think my question maybe needs to be re-phrased.
This is more to do with the architecture of page scripts that are just using simple vanilla JS (ones that I have generally put near to the footer of each page they run on).
i.e. - for the print function below
<body>
<h1>Hello, world!</h1>
<button class="btn" onclick="print('all')">Print All Rows</button>
<table id="bootstrap-table"><table>
</body>
<script src="{{ mix('/js/app.js') }}"></script>
<script>
var $table = $('#bootstrap-table');
function print(type){
$table.getAllRows();
// do other stuff with ES6 syntax
}
</script>
in the above example, I am getting data from a table which is on the page and then doing some other stuff with ES6 to pass this to make a request to a print server.
Moving the print function off into another file I see the following issues:
- The
$table variable is specific to this page only. In another file, It just appears from nowhere and does not have any context (but still works as intended!).
- print is a very generic function. Once compiled this could easily conflict.
I have not worked with basic JS outside either Vue JS or putting it straight into the page it is being run on, so apologise if this is a very generic question!