Laravel 9 vite - internal script is executed before JS bundled via vite

Viewed 24

I've created 'test.js' file in 'resources/js/' and added console.log(first) to it

In the welcome.blade.php I have something like that:

<body>
  @vite(['resources/js/test.js'])
  <script type='text/javascript'>
    console.log(second)
  </script>
</body>

What I expect is to execute those scipts in order from top to bottom, however it returns output:

second
first

I guess @vite(['resources/js/test.js']) is executed asynchronously by default, but I want to change it.

Is there any better way to do this than to put all the internal JS in DOMContentLoaded?

1 Answers

Changing type from 'text/javascript' to 'module' helped.

<script type='module'>
  console.log(second)
</script>
Related