2

I just joined an existing project that uses vanilla JS and I am trying to minify our source code. Unfortunately, there is no reference to the javascript files within the index.js, (All variables are global due to external scripts declared in the index.html file..), so solutions such as WebPack do not work.

Is there a solution out there that can parse through all of the external script tags in the index.html to minify it into a single separate js file?

4
  • Do you only have external libraries or do you also have inline JS? How many <script> tags are we talking about? Because for maybe 10 you are better off by hand. I'm not sure if such tool exists but it will be cool to make. Commented Jan 6, 2020 at 21:49
  • Why exactly can't you use Webpack to do that? You could define each external script as a separate entry. And if not, you could still bundle all the script into one and then have your own strategy of Code Splitting Commented Jan 6, 2020 at 21:59
  • 1
    There are hundreds of separate external and inline entries. I was hoping there would be a tool available to simply parse through all of them, bundle & minify. Commented Jan 6, 2020 at 22:59
  • Hundreds is not a simple task indeed. Is it across multiple files with the same references for example a jQuery CDN? Commented Jan 6, 2020 at 23:21

1 Answer 1

4

If it is a single page application with every scripts linked synchronously in the HTML file (no modules and stuff like that), it should be quite simple

Adding this at the end of the html file would help you retrieve the content in the good execution order:

(async () => {

  // get the scripts contents
  var scripts = await Promise.all(
    Array.from( document.querySelectorAll("script") )
    .map( x => x.src )
    .map( x => fetch(x).then( x => x.text()))
  )

  // package them into one Blob
  var blob = new Blob(
    [scripts.join("\n\n")], 
    {type: "text/plain"}
  );

  // send it to a server script
  let xhr = new XMLHttpRequest();
  xhr.open("POST", "upload.php", true);
  xhr.onload = () => console.log("sent");
  xhr.send(blob);

})();

Then you would minify/uglify with any tool of your choosing.

Sign up to request clarification or add additional context in comments.

3 Comments

Pretty clever but what about inline scripts?
A naive approach would be to use x.childNodes[0].textContent instead of x.src but I can't guarantee that it is going to work well with, for example, comments. I don't know if treating the content of a script tag as a text node is part of any specification. You should run some tests on dummy scripts first and have some way to test the compliance of the application after the minification.
I just tested the textContent thing. It seems to work just fine with multiline scripts in Firefox. with a little refactoring you can fallback to the textContent hack when the script tag does not have a src attribute. One way to make sure the hack is safe to use would be to copy and paste a library into script tags (with comments and all), run the snippet and then compare the output with the original library file by the means of your IDE, git or some other tool. if the two files are identical you are good to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.