Build: Add `exports` to package.json, export slim & esm builds
[jquery.git] / build / tasks / dist.js
blob36ce38b0095e8c9a14377f22ab4a077d065d7bca
1 "use strict";
3 module.exports = function( grunt ) {
4         const fs = require( "fs" );
5         const filename = grunt.option( "filename" );
6         const distFolder = grunt.option( "dist-folder" );
7         const distPaths = [
8                 `${ distFolder }/${ filename }`,
9                 `${ distFolder }/${ filename.replace( ".js", ".min.js" ) }`,
10                 `${ distFolder }/${ filename.replace( ".js", ".min.map" ) }`
11         ];
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 ) {
26                         return path !== "*";
27                 } );
29                 // Ensure the dist files are pure ASCII
30                 nonascii = false;
32                 distPaths.forEach( function( filename ) {
33                         let i, c;
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)" );
39                                 nonascii = true;
40                         }
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 );
47                                         if ( c > 127 ) {
48                                                 grunt.log.writeln( "- position " + i + ": " + c );
49                                                 grunt.log.writeln( "-- " + text.substring( i - 20, i + 20 ) );
50                                                 break;
51                                         }
52                                 }
53                                 nonascii = true;
54                         }
56                         // Optionally copy dist files to other locations
57                         paths.forEach( function( path ) {
58                                 let created;
60                                 if ( !/\/$/.test( path ) ) {
61                                         path += "/";
62                                 }
64                                 created = path + filename.replace( "dist/", "" );
65                                 grunt.file.write( created, text );
66                                 grunt.log.writeln( "File '" + created + "' created." );
67                         } );
68                 } );
70                 return !nonascii;
71         } );