How to load JS as the first item in head tag? We have a custom module that has a JS library that we want to load in the &lt;head&gt; tag as the very first item even before all the normal Drupal JS files. All other libraries are loaded through mytheme.libraries.yml.

How can I load the library (mymodule/myscript) so that it loads as the first &lt;script&gt; tag in the &lt;head&gt;?

What we tried:

mymodule.module

function  mymodule_page_attachments(array &amp;$page) {
  $attachments['#attached']['library'][] = 'mymodule/myscript';
}


mymodule.libraries.yml

myscript:
  version: 1.x
  header: true
  js:
    /path/to/file: { external: true, weight: -200 }


Current problem:

&lt;head&gt;
  &lt;script&gt;script1&lt;/script&gt;
  &lt;script&gt;script2&lt;/script&gt;
  &lt;script&gt;myscript is loaded here&lt;/script&gt;
&lt;/head&gt;


Goal:

&lt;head&gt;
  &lt;script&gt;myscript is loaded here&lt;/script&gt;
  &lt;script&gt;script1&lt;/script&gt;
  &lt;script&gt;script2&lt;/script&gt;
&lt;/head&gt;
Using @Manav's link, i was able to get to this solution that worked. 

function mymodule_page_attachments(&amp;$attachments) {
  $custom_script = [
    '#type' =&gt; 'html_tag',
    '#tag' =&gt; 'script',
    '#attributes' =&gt; [
      'src' =&gt; 'path/to/file.js',
    ],
    '#weight' =&gt; -200, // this allowed me to do what i needed
  ];

  $attachments['#attached']['html_head'][] = [$custom_script, 'custom_script_key'];
}