0

In a project I'm working on, I want to produce stand-alone .html files. For convenience and modularity, I want to use separate JavaScript files during the development. I'm looking for a 'build' solution that will allow me to produce the target .html files with the relevant JavaScript files included directly as embedded <script> tags (no src attribute). For example:

input.html:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<script id="custom" src="test.js"></script>
</body>
</html>

test.js:

alert("hello");

output.html:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<script id="custom">
alert("hello");    
</script>
</body>
</html>

Bonus: A solution that can easily adapt to multiple js dependencies (output.html should include all the js files concatenated inside a single script tag). This can possibly be done by introducing a main.js file that depends on all of the other .js files, then bundling it with some tool, then reference it from input.html. I'd like a streamlined solution for this entire flow.

-- Update --

After considering webpack (from here , as was suggested in the comments) and being unable to get rid of all of the wrapping code (the minimum wrapping was achieved by setting iife: false, but that still had some code remaining, which was unacceptable for my usecase), and trying out grunt from the same question, which stripped the id attribute from the <script> tag, I finally came across m4 which easily fit the bill (things seemed more complicated with sed and awk).

Using it was as simple as having:

input.html.preprocessed:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
</head>
<body>
<script id="custom">
  changequote(, )
  include(test.js)
</script>
</body>
</html>

Then running m4 input.html.preprocessed in the command line. If one wanted to have the original html also functioning, I'm sure some sed magic, piped before the m4 command, would have done the trick, but for me that was not the case.

Uri Barenholz
  • 693
  • 3
  • 13
  • 1
    "[webpack](https://webpack.js.org/)" is probably the most commonly used bundler, and there are some existing question covering this sort of problem using it: [Inline JavaScript and CSS with webpack](https://stackoverflow.com/questions/39555511/inline-javascript-and-css-with-webpack) – DBS May 15 '23 at 08:33
  • Thanks! on first look it seems like there have been some issues with the accepted answer breaking on current webpack versions, but also answers mitigating that. I'll give it a go and update here. – Uri Barenholz May 15 '23 at 10:23
  • The referred question and answers do not solve my issue (albeit close). For webpack, it injects wrapping code that I do not need, and cannot avoid (best option was to set "iife: false" but some extra code remains). With grunt - the id attribute of the – Uri Barenholz May 17 '23 at 14:00

1 Answers1

-2

TLDR: Please tell what environment you have available for it to run, so we know what tools are acceptable

I am not sure, what environment are You going for, since this is just a parsing and file I/O exercise. You can do it with a Python script or node.js script, doing it with JS would be quite ugly, but possible too.

Such a script would just look for a line containing a script tag, and took the file names, in order to open each of the required files to write them down to the output file, with other elements already written in. Also if the line in an opened JS file contains some dependency or file import, it is written before everything else, so first You are going file by file and storing the imports else ware to write it first to the file, and then the regular code.

EDIT: Found this answer, should be perfect for You: https://stackoverflow.com/a/301784/18229317

You would need to get the files with simple script (like python loop) the filenames should be then passed to the includes in the xml. The output file is the file you can just put in your output html file (also with anything capable of reading and writing files)

  • I'm quite flexible, but prefer usage of standard tools/bundlers, and not custom-coding that may end up with unexpected bugs on edge cases. I guess node / python are the easiest environments for me to consider – Uri Barenholz May 15 '23 at 09:05
  • found something that might be useful, edited the answer to include that – k_roszczak May 15 '23 at 09:31
  • Thanks! that specific answer is quite antique, but other answers in that thread can solve the first part of the bonus section (concatenating all the js files into one, by using some 'master' js file). Still getting an automated, standard build / bundle process out of this requires some custom coding, which i prefer to avoid. – Uri Barenholz May 15 '23 at 10:19