Skip to content

Commit

Permalink
Build: Rebuild package stylesheet entrypoints for file updates
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed May 30, 2019
1 parent c12859d commit 9688deb
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion bin/packages/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const path = require( 'path' );
const glob = require( 'fast-glob' );
const ProgressBar = require( 'progress' );
const workerFarm = require( 'worker-farm' );
const { Readable } = require( 'stream' );
const { Readable, Transform } = require( 'stream' );

const files = process.argv.slice( 2 );

Expand All @@ -18,6 +18,52 @@ const files = process.argv.slice( 2 );
*/
const PACKAGES_DIR = path.resolve( __dirname, '../../packages' );

/**
* Get the package name for a specified file
*
* @param {string} file File name
* @return {string} Package name
*/
function getPackageName( file ) {
return path.relative( PACKAGES_DIR, file ).split( path.sep )[ 0 ];
}

/**
* Returns a stream transform which maps an individual stylesheet to its
* package entrypoint. Unlike JavaScript which uses an external bundler to
* efficiently manage rebuilds by entrypoints, stylesheets are rebuilt fresh
* in their entirety from the build script.
*
* @return {Transform} Stream transform instance.
*/
function createStyleEntryTransform() {
const packages = new Set;

return new Transform( {
objectMode: true,
async transform( file, encoding, callback ) {
// Only stylesheets are subject to this transform.
if ( path.extname( file ) !== '.scss' ) {
this.push( file );
callback();
return;
}

// Only operate once per package, assuming entries are common.
const packageName = getPackageName( file );
if ( packages.has( packageName ) ) {
callback();
return;
}

packages.add( packageName );
const entries = await glob( path.resolve( PACKAGES_DIR, packageName, 'src/*.scss' ) );
entries.forEach( ( entry ) => this.push( entry ) );
callback();
},
} );
}

let onFileComplete = () => {};

let stream;
Expand All @@ -26,6 +72,7 @@ if ( files.length ) {
stream = new Readable( { encoding: 'utf8' } );
files.forEach( ( file ) => stream.push( file ) );
stream.push( null );
stream = stream.pipe( createStyleEntryTransform() );
} else {
const bar = new ProgressBar( 'Build Progress: [:bar] :percent', {
width: 30,
Expand Down

0 comments on commit 9688deb

Please sign in to comment.