3 module.exports = function( grunt ) {
4 const fs = require( "fs" );
5 const filename = grunt.option( "filename" );
6 const distFolder = grunt.option( "dist-folder" );
8 `${ distFolder }/${ filename }`,
9 `${ distFolder }/${ filename.replace( ".js", ".min.js" ) }`,
10 `${ distFolder }/${ filename.replace( ".js", ".min.map" ) }`
13 // Process files for distribution
14 grunt.registerTask( "dist", function() {
15 let stored, flags, paths, nonascii;
17 // Check for stored destination paths
18 // ( set in dist/.destination.json )
19 stored = Object.keys( grunt.config( "dst" ) );
21 // Allow command line input as well
22 flags = Object.keys( this.flags );
24 // Combine all output target paths
25 paths = [].concat( stored, flags ).filter( function( path ) {
29 // Ensure the dist files are pure ASCII
32 distPaths.forEach( function( filename ) {
34 const text = fs.readFileSync( filename, "utf8" );
36 // Ensure files use only \n for line endings, not \r\n
37 if ( /\x0d\x0a/.test( text ) ) {
38 grunt.log.writeln( filename + ": Incorrect line endings (\\r\\n)" );
42 // Ensure only ASCII chars so script tags don't need a charset attribute
43 if ( text.length !== Buffer.byteLength( text, "utf8" ) ) {
44 grunt.log.writeln( filename + ": Non-ASCII characters detected:" );
45 for ( i = 0; i < text.length; i++ ) {
46 c = text.charCodeAt( i );
48 grunt.log.writeln( "- position " + i + ": " + c );
49 grunt.log.writeln( "-- " + text.substring( i - 20, i + 20 ) );
56 // Optionally copy dist files to other locations
57 paths.forEach( function( path ) {
60 if ( !/\/$/.test( path ) ) {
64 created = path + filename.replace( "dist/", "" );
65 grunt.file.write( created, text );
66 grunt.log.writeln( "File '" + created + "' created." );