aboutsummaryrefslogtreecommitdiffstats
path: root/build/tasks/dist.js
blob: f15689e3dca2cd817625575f320372d17318c37c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"use strict";

// Process files for distribution.
module.exports = function processForDist( text, filename ) {
	if ( !text ) {
		throw new Error( "text required for processForDist" );
	}

	if ( !filename ) {
		throw new Error( "filename required for processForDist" );
	}

	// Ensure files use only \n for line endings, not \r\n
	if ( /\x0d\x0a/.test( text ) ) {
		throw new Error( filename + ": Incorrect line endings (\\r\\n)" );
	}

	// Ensure only ASCII chars so script tags don't need a charset attribute
	if ( text.length !== Buffer.byteLength( text, "utf8" ) ) {
		let message = filename + ": Non-ASCII characters detected:\n";
		for ( let i = 0; i < text.length; i++ ) {
			const c = text.charCodeAt( i );
			if ( c > 127 ) {
				message += "- position " + i + ": " + c + "\n";
				message += "==> " + text.substring( i - 20, i + 20 );
				break;
			}
		}
		throw new Error( message );
	}
};