Skip to content
Merged
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
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.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought to be clever / functional here with some sort of conditional flow (an overEvery and/or series Promise) but I couldn't come up with an implementation which wouldn't end up being unnecessarily obscure / unreadable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best to keep it simple! 😀

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