Use javascript to remove the comments and blank lines of the require.js file

Recently, I was looking at the source code of require.js, and I almost wanted to remove the comments and blank lines on the source code to get a "pure version", and then add comments myself.

First, use javascript to read the file. Some people on the Internet recommend using ActiveXObject objects, but this is ie (using Chrome). Just use the file box to read the file and then use the fileReader object to read, the file content

code show as below

<input type="file"id="files"name="files[]"multiple/>
		<script>
			
			var commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg
			var blinkRegExp = / \ n \ s * \ r / g;
			function handleFileSelect (possibly) {
				var files = evt.target.files;
				for(var i =0, f; f = files[i]; i++){
					var reader = new FileReader();
					reader.readAsText(f);
					reader.onload = function(e){
							var kk = e.target.result.replace(commentRegExp,'')
							alert(typeof(kk) === 'string')
							console.log (kk.replace (blinkRegExp, ''));
						};
				}
			}

			document.getElementById('files').addEventListener('change', handleFileSelect,false);
		</script>

 The key is in the second regex, the first regex that matches the comment itself in require.js.

Then I looked for those who matched the blank line and found that there are many strange sayings on the Internet, so I wanted to write it myself, and then various attempts failed, and finally I thought of a partial trick, the newline is \r\nIf there are two blank lines, there are two\r\ n is connected together, that is, \r\n\r\n, just remove one \n\r.

Anyway, all kinds of trials will not work. Finally, copy the code of require.js to another file, and then process it. The result is OK. . . my day

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327040559&siteId=291194637