Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gruntfile: Optimize verify:source-maps #9

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 98 additions & 20 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ var installChanged = require( 'install-changed' );
var json2php = require( 'json2php' );

module.exports = function(grunt) {
var path = require('path'),
var Buffer = require( 'buffer' ).Buffer,
path = require('path'),
fs = require( 'fs' ),
glob = require( 'glob' ),
assert = require( 'assert' ).strict,
Expand Down Expand Up @@ -1612,38 +1613,115 @@ module.exports = function(grunt) {
* @ticket 24994
* @ticket 46218
*/
grunt.registerTask( 'verify:source-maps', function() {
grunt.registerTask( 'verify:source-maps', async function() {
const done = this.async();

const ignoredFiles = [
'build/wp-includes/js/dist/components.js'
];
const files = buildFiles.reduce( ( acc, path ) => {

/** @var {string[]} File paths for files to scan for source maps. */
const files = [];

/** @var {Promise[]} Tracks the progress of finding files to scan. */
const fileSearch = [];

for ( const globPattern of buildFiles ) {
// Skip excluded paths and any path that isn't a file.
if ( '!' === path[0] || '**' !== path.substr( -2 ) ) {
return acc;
if ( '!' === globPattern[0] || ! globPattern.endsWith( '**' ) ) {
continue;
}
acc.push( ...glob.sync( `${ BUILD_DIR }/${ path }/*.js` ) );
return acc;
}, [] );

fileSearch.push( new Promise( ( resolve, reject ) => {
glob( `${ BUILD_DIR }/${ globPattern }/*.js`, ( error, matches ) => {
if ( null !== error ) {
reject();
return;
}

if ( matches.length > 0 ) {
files.push.apply( files, matches );
}
resolve();
} );
} ) );
}

await Promise.all( fileSearch );

assert(
files.length > 0,
'No JavaScript files found in the build directory.'
);

files
.filter(file => ! ignoredFiles.includes( file) )
.forEach( function( file ) {
const contents = fs.readFileSync( file, {
encoding: 'utf8',
const sourceMapSearch = [];

/** @var {Buffer} Contains source map indicator. */
const searchToken = Buffer.from( 'sourceMappingURL=', 'utf8' );
const dataUriToken = Buffer.from( 'sourceMappingURL=data:', 'utf8' );

for ( const filePath of files ) {
if ( ignoredFiles.includes( filePath ) ) {
continue;
}

sourceMapSearch.push( new Promise( ( resolve, reject ) => {
fs.open( filePath, ( error, fd ) => {
if ( null !== error ) {
reject( error );
return;
}

const BUFFER_SIZE_BYTES = 64 * 1024;
const START_OF_BUFFER = 0;
const buffer = Buffer.alloc( BUFFER_SIZE_BYTES + dataUriToken.byteLength );

/** @param {number} position Byte offset at which to start reading next chunk. */
const scanNextChunk = ( position ) => {
fs.read(
fd,
buffer,
START_OF_BUFFER,
BUFFER_SIZE_BYTES,
// Ensure we capture the entire search pattern in this chunk.
position,
/** @param {Buffer} chunk Next read chunk from file. */
( error, bytesRead, chunk ) => {
if ( null !== error ) {
reject();
return;
}

if ( bytesRead <= searchToken.byteLength ) {
resolve();
return;
}

const searchTokenAt = chunk.indexOf( searchToken );
if ( -1 === searchTokenAt ) {
return scanNextChunk( position + bytesRead - searchToken.byteLength );
}

if ( searchTokenAt + dataUriToken.byteLength + 1 > chunk.byteLength ) {
return scanNextChunk( searchTokenAt );
}

assert(
chunk.includes( dataUriToken ),
`The ${ filePath } file must not contain a sourceMappingURL.`
);
resolve();
}
);
};

scanNextChunk( 0 );
} );
// `data:` URLs are allowed:
const match = contents.match( /sourceMappingURL=((?!data:).)/ );
} ) );
}

assert(
match === null,
`The ${ file } file must not contain a sourceMappingURL.`
);
} );
await Promise.all( sourceMapSearch );
done();
} );

grunt.registerTask( 'build', function() {
Expand Down