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

Resolve original sources #75

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
61 changes: 35 additions & 26 deletions src/nodes/Merger.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ export default class Merger extends Node {
});
}

getFileFromChecksum ( checksum ) {
let i = this.inputs.length;
let file;

while ( i-- ) {
if ( file = this.inputs[i].getFileFromChecksum( checksum ) ) {
return file;
}
}
}

ready () {
let aborted;
let index;
Expand All @@ -102,37 +113,35 @@ export default class Merger extends Node {

this._ready = mkdir( outputdir ).then( () => {
let start;
let inputdirs = [];

return mapSeries( this.inputs, function ( input, i ) {
if ( aborted ) throw ABORTED;
return input.ready().then( inputdir => inputdirs[i] = inputdir );
}).then( () => {
start = Date.now();

this.emit( 'info', {
code: 'MERGE_START',
id: this.id,
progressIndicator: true
});

return mapSeries( inputdirs, inputdir => {
return mapSeries( this.inputs, x => x.ready() )
.then( inputdirs => {
start = Date.now();

this.emit( 'info', {
code: 'MERGE_START',
id: this.id,
progressIndicator: true
});

return mapSeries( inputdirs, inputdir => {
if ( aborted ) throw ABORTED;
return mergeDirectories( inputdir, outputdir );
});
})
.then( () => {
if ( aborted ) throw ABORTED;
return mergeDirectories( inputdir, outputdir );
});
}).then( () => {
if ( aborted ) throw ABORTED;

this._cleanup( index );
this._cleanup( index );

this.emit( 'info', {
code: 'MERGE_COMPLETE',
id: this.id,
duration: Date.now() - start
});
this.emit( 'info', {
code: 'MERGE_COMPLETE',
id: this.id,
duration: Date.now() - start
});

return outputdir;
});
return outputdir;
});
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/nodes/Observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export default class Observer extends Node {
this.input.on( 'info', this._oninfo );
}

getFileFromChecksum ( checksum ) {
return this.input.getFileFromChecksum( checksum );
}

ready () {
let observation;

Expand Down
90 changes: 57 additions & 33 deletions src/nodes/Source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { basename, relative, resolve } from 'path';
import { link, linkSync, mkdirSync, statSync, Promise } from 'sander';
import { lsr, link, linkSync, readFileSync, mkdirSync, statSync, unlinkSync, Promise } from 'sander';
import queue from '../queue/index.js';
import { crc32 } from 'crc';
import { watch } from 'chokidar';
import * as debounce from 'debounce';
import Node from './Node';
Expand All @@ -13,6 +15,8 @@ export default class Source extends Node {

this.id = options.id || 'source';
this.dir = dir;
this.checksumByFile = {};
this.fileByChecksum = {};
this.callbacks = [];
this._entries = {};

Expand All @@ -21,16 +25,14 @@ export default class Source extends Node {
const stats = statSync( this.dir );

if ( !stats.isDirectory() ) {
this.isFileSource = true;

this.file = dir;
this.dir = undefined;
this.dir = null;

this.uid = uid( this.id );

this._ready = new Promise( ( ok, fail ) => {
this._deferred = { ok, fail };
});
} else {
this._ready = Promise.resolve( this.dir );
// this._ready = Promise.resolve( this.dir );
}
} catch ( err ) {
if ( err.code === 'ENOENT' ) {
Expand All @@ -47,7 +49,41 @@ export default class Source extends Node {
this.static = options && options.static;
}

getFileFromChecksum ( checksum ) {
return this.fileByChecksum[ checksum ];
}

ready () {
if ( !this._ready ) {
this._ready = queue.add( ( fulfil, reject ) => {
const start = Date.now();

this._makeReady();

lsr( this.dir )
.then( files => {
files.forEach( file => {
const absolutePath = resolve( this.dir, file );
const buffer = readFileSync( absolutePath );
const checksum = crc32( buffer );

this.checksumByFile[ absolutePath ] = checksum;
this.fileByChecksum[ checksum ] = absolutePath;
});

// For most situations, generating checksums takes no time at all,
// but it's probably worth warning about this if it becomes a
// source of pain. TODO 'warn' event?
const duration = Date.now() - start;
if ( duration > 1000 ) {
this.emit( 'info', `the ${this.dir} directory took ${duration}ms to initialise - consider excluding unnecessary files from the build` );
}
})
.then( () => fulfil( this.dir ) )
.catch( reject );
});
}

return this._ready;
}

Expand All @@ -56,17 +92,9 @@ export default class Source extends Node {
return;
}

this._active = true;

// this is a file watch that isn't fully initialized
if ( this._deferred ) {
this._makeReady();
}
this._makeReady();

// make sure the file is in the appropriate target directory to start
if ( this.file ) {
linkSync( this.file ).to( this.targetFile );
}
this._active = true;

let changed = [];

Expand Down Expand Up @@ -102,13 +130,14 @@ export default class Source extends Node {
relay();
});
});
}

if ( this.file ) {
this._fileWatcher = watch( this.file, options );
} else {
this._watcher = watch( this.dir, options );

this._fileWatcher.on( 'change', () => {
link( this.file ).to( this.targetFile );
[ 'add', 'change', 'unlink' ].forEach( type => {
this._watcher.on( type, path => {
changes.push({ type, path });
relay();
});
});
}
}
Expand Down Expand Up @@ -141,17 +170,12 @@ export default class Source extends Node {
}

_makeReady () {
this.dir = resolve( session.config.gobbledir, this.uid );
this.targetFile = resolve( this.dir, basename( this.file ) );
if ( this.isFileSource && !this._isReady ) {
this.dir = resolve( session.config.gobbledir, this.uid );
this.targetFile = resolve( this.dir, basename( this.file ) );

try {
mkdirSync( this.dir );
this._deferred.ok( this.dir );
} catch (e) {
this._deferred.fail( e );
throw e;
linkSync( this.file ).to( this.targetFile );
this._isReady = true; // TODO less conflicty flag name
}

delete this._deferred;
}
}
4 changes: 4 additions & 0 deletions src/nodes/Transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export default class Transformer extends Node {
this.input.on( 'info', this._oninfo );
}

getFileFromChecksum ( checksum ) {
return this.input.getFileFromChecksum( checksum );
}

ready () {
let outputdir;
let transformation;
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function ( node, options ) {
return node.ready()
.then( inputdir => {
return copydir( inputdir ).to( dest )
.then( () => flattenSourcemaps( inputdir, dest, dest, task ) );
.then( () => flattenSourcemaps( node, inputdir, dest, dest, task ) );
})
.then( () => {
node.teardown();
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/serve/handleRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import serveDir from './serveDir';
import serveSourcemap from './serveSourcemap';
import serveError from './serveError';

export default function handleRequest ( srcDir, error, sourcemapPromises, request, response ) {
export default function handleRequest ( node, srcDir, error, sourcemapPromises, request, response ) {
const parsedUrl = parse( request.url );
const pathname = parsedUrl.pathname;

Expand All @@ -31,7 +31,7 @@ export default function handleRequest ( srcDir, error, sourcemapPromises, reques
filepath = join( srcDir, pathname );

if ( extname( filepath ) === '.map' ) {
return serveSourcemap( filepath, sourcemapPromises, request, response )
return serveSourcemap( node, filepath, sourcemapPromises, request, response )
.catch( err => serveError( err, request, response ) );
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/serve/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default function serve ( node, options = {} ) {
});

server.on( 'request', ( request, response ) => {
handleRequest( srcDir, error, sourcemapPromises, request, response )
handleRequest( node, srcDir, error, sourcemapPromises, request, response )
.catch( err => task.emit( 'error', err ) );
});

Expand Down
23 changes: 21 additions & 2 deletions src/nodes/serve/serveSourcemap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { crc32 } from 'crc';
import { dirname, relative, resolve } from 'path';
import { readFileSync } from 'sander';
import { load } from 'sorcery';

export default function serveSourcemap ( filepath, sourcemapPromises, request, response ) {
export default function serveSourcemap ( node, filepath, sourcemapPromises, request, response ) {
const owner = filepath.slice( 0, -4 );

if ( !sourcemapPromises[ filepath ] ) {
Expand All @@ -10,7 +13,23 @@ export default function serveSourcemap ( filepath, sourcemapPromises, request, r
throw new Error( 'Could not resolve sourcemap for ' + owner );
}

return chain.apply().toString();
const map = chain.apply();
const dir = dirname( owner );
const cwd = process.cwd();

map.sources = map.sources.map( ( source, i ) => {
const content = map.sourcesContent[i];
const checksum = crc32( content );
const originalSource = node.getFileFromChecksum( checksum );

const absolutePath = resolve( dir, originalSource || source );

return relative( cwd, absolutePath );
});

map.sourceRoot = 'file://' + process.cwd();

return map.toString();
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/nodes/watch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function watch ( node, options ) {
progressIndicator: true
});

return flattenSourcemaps( dir, dest, dest, task ).then( () => {
return flattenSourcemaps( node, dir, dest, dest, task ).then( () => {
task.emit( 'info', {
code: 'SOURCEMAP_PROCESS_COMPLETE',
duration: Date.now() - sourcemapProcessStart
Expand Down
26 changes: 22 additions & 4 deletions src/utils/flattenSourcemaps.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
import { extname, resolve } from 'path';
import { lsr } from 'sander';
import { basename, dirname, extname, relative, resolve } from 'path';
import { lsr, readFileSync, writeFile } from 'sander';
import * as mapSeries from 'promise-map-series';
import { load } from 'sorcery';
import { crc32 } from 'crc';
import { SOURCEMAP_COMMENT, getSourcemapComment } from './sourcemap';

const whitelist = { '.js': true, '.css': true };

export default function flattenSourcemaps ( inputdir, outputdir, base, task ) {
export default function flattenSourcemaps ( node, inputdir, outputdir, base, task ) {
return lsr( inputdir ).then( files => {
const jsAndCss = files.filter( file => whitelist[ extname( file ) ] );

return mapSeries( jsAndCss, file => {
return load( resolve( inputdir, file ) )
.then( chain => {
if ( chain ) {
return chain.write( resolve( outputdir, file ), { base });
const map = chain.apply({ base });

map.sources = map.sources.map( source => {
const checksum = crc32( readFileSync( base, source ) );
const originalSource = node.getFileFromChecksum( checksum );

const dir = dirname( resolve( base, file ) );
return originalSource ? relative( dir, originalSource ) : source;
});

const code = readFileSync( inputdir, file, { encoding: 'utf-8' })
.replace( SOURCEMAP_COMMENT, getSourcemapComment( encodeURI( basename( file + '.map' ) ), extname( file ) ) );

return Promise.all([
writeFile( outputdir, file, code ),
writeFile( outputdir, file + '.map', map.toString() )
]);
}
})
.catch( err => {
Expand Down
Loading