2023-09-18 16:39:00 +00:00
|
|
|
// Process files for distribution.
|
2023-07-27 15:24:49 +00:00
|
|
|
export default function processForDist( text, filename ) {
|
2023-09-18 16:39:00 +00:00
|
|
|
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;
|
2013-08-16 13:48:00 +00:00
|
|
|
}
|
2023-09-18 16:39:00 +00:00
|
|
|
}
|
|
|
|
throw new Error( message );
|
|
|
|
}
|
2023-07-27 15:24:49 +00:00
|
|
|
}
|