PlayForm/Compress

HTML // Comments in JS Scripts Problematic

NeilCresswell opened this issue ยท 13 comments

Am finding that HTML .astro pages that have embedded JS in the body via <script> tags are very problematic, especially if there are comments inside starting with //, which can cause the script contents to get dropped. Workaround is to have html:false in the config file. Everything else seems good if all other settings set to true.

Thank you for reporting this.

Can you try processScripts: [ ] like so:

import compress from "astro-compress";
import { defineConfig } from "astro/config";

export default defineConfig({
	integrations: [
		compress({
			html: {
				processScripts: []
			},
		}),
	],
});

or maybe minifyJS: false

import compress from "astro-compress";
import { defineConfig } from "astro/config";

export default defineConfig({
	integrations: [
		compress({
			html: {
				minifyJS: false
			},
		}),
	],
});

@NeilCresswell Sorry, this is my alt, but yeah what he said.

Can you try processScripts: [ ] like so:

import compress from "astro-compress";
import { defineConfig } from "astro/config";

export default defineConfig({
	integrations: [
		compress({
			html: {
				processScripts: []
			},
		}),
	],
});

Only lightly tested by changing config in a small project that had the issue I originally reported. This worked and I wasn't missing JS due to // comments.

or maybe minifyJS: false

import compress from "astro-compress";
import { defineConfig } from "astro/config";

export default defineConfig({
	integrations: [
		compress({
			html: {
				minifyJS: false
			},
		}),
	],
});

Problem is still present trying this way.

It might be that the HTML minification is triggered before the inside script minification and that's why when it tries to compress the JavaScript inside the script tag it's just one long string with an // at the beginning. This is an html-minifier-terser thing, but I'll try to see if I can get it sorted.

Would the html: {processScripts: []} option be a better workaround choice for now than html: false if we want to maximize the compression?

Yes.

You could also do with a block comment to avoid minifying the whole line.

/**
 * Example
 */

If this issue is resolved I'll close it, feel free to re-open it whenever you have questions.

FWIW not resolved. Just have a work-around. Bug is still present.

I am sorry, this can't get fixed. This is standard behavior in html-minifier-terser and the default value of processScripts is [ ] there. However, astro-compress is a bit more opinionated and aggressive. This is why you should use the workaround.