Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added processScriptsFunction option #1052

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from

Conversation

SergeyMosin
Copy link

Add ability to "processScripts" via a custom function. Useful for WebGL shaders and json-ld data.
For example, these options...

const minifyOpt={
	"processScripts": [
		"application/ld+json",
		"x-shader/x-vertex",
		"x-shader/x-fragment"
	],
	"processScriptsFunction": function(text,script){
		if(script==="x-shader/x-vertex" || script==="x-shader/x-fragment"){
			// Strip comments + normalize whitespace (including line breaks)...
			return text.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g,'').replace(/\s+/g,' ').trim();
		}else if(script==="application/ld+json"){
			// Strip whitespace
			return JSON.stringify(JSON.parse(text))
		}else return text;
	},
	"removeComments": true,
	"collapseWhitespace": true,
	"continueOnParseError": true
}

... could be used for this file...

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Minifier Test</title>
	<script id="vertexShader" type="x-shader/x-vertex">
		// Vertex shader with comments
		void main() {
			/**
 			 * Multi-line comments
 			 */
			gl_Position = projectionMatrix * modelViewMatrix * vec4(position.x+10.0, position.y, position.z+5.0, 1.0);
		}
	</script>
	<script id="fragmentShader" type="x-shader/x-fragment">
		// Fragment shader
		void main() {
			gl_FragColor = vec4(0.0, 0.58, 0.86, 1.0); // some more comments
		}
	</script>
	<script type="application/ld+json">
		{
			"@context": "http://schema.org",
			"@type": "WebPage",
			"name": "Lecture 12: Graphs, networks, incidence matrices",
			"description": "These video lectures of Professor Gilbert Strang teaching 18.06 were recorded in Fall 1999 and do not correspond precisely to the current  edition of the textbook.",
			"publisher": {
				"@type": "CollegeOrUniversity",
				"name": "MIT OpenCourseWare"
			},
			"license": "http://creativecommons.org/licenses/by-nc-sa/3.0/us/deed.en_US"
		}
	</script>
</head>
<body>

</body>
</html>

Currently the minifier breaks WebGL shaders if they have comments and does not remove all the white-space from json-ld scripts....

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Minifier Test</title><script id="vertexShader" type="x-shader/x-vertex">// Vertex shader with comments void main() { /** * Multi-line comments */ gl_Position = projectionMatrix * modelViewMatrix * vec4(position.x+10.0, position.y, position.z+5.0, 1.0); }</script><script id="fragmentShader" type="x-shader/x-fragment">// Fragment shader void main() { gl_FragColor = vec4(0.0, 0.58, 0.86, 1.0); // some more comments }</script><script type="application/ld+json">{ "@context": "http://schema.org", "@type": "WebPage", "name": "Lecture 12: Graphs, networks, incidence matrices", "description": "These video lectures of Professor Gilbert Strang teaching 18.06 were recorded in Fall 1999 and do not correspond precisely to the current edition of the textbook.", "publisher": { "@type": "CollegeOrUniversity", "name": "MIT OpenCourseWare" }, "license": "http://creativecommons.org/licenses/by-nc-sa/3.0/us/deed.en_US" }</script></head><body></body></html>

... expected output (using the 'processScriptsFunction') ...

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Minifier Test</title><script id="vertexShader" type="x-shader/x-vertex">void main() { gl_Position = projectionMatrix * modelViewMatrix * vec4(position.x+10.0, position.y, position.z+5.0, 1.0); }</script><script id="fragmentShader" type="x-shader/x-fragment">void main() { gl_FragColor = vec4(0.0, 0.58, 0.86, 1.0); }</script><script type="application/ld+json">{"@context":"http://schema.org","@type":"WebPage","name":"Lecture 12: Graphs, networks, incidence matrices","description":"These video lectures of Professor Gilbert Strang teaching 18.06 were recorded in Fall 1999 and do not correspond precisely to the current  edition of the textbook.","publisher":{"@type":"CollegeOrUniversity","name":"MIT OpenCourseWare"},"license":"http://creativecommons.org/licenses/by-nc-sa/3.0/us/deed.en_US"}</script></head><body></body></html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant