2018-10-30 18:57:48 +00:00
|
|
|
/**
|
|
|
|
* script to build packages into `build/` directory.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* node ./bin/packages/build.js
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
const fs = require( 'fs' );
|
|
|
|
const path = require( 'path' );
|
|
|
|
const glob = require( 'glob' );
|
|
|
|
const chalk = require( 'chalk' );
|
|
|
|
const mkdirp = require( 'mkdirp' );
|
2021-06-02 03:54:00 +00:00
|
|
|
const sass = require( 'sass' );
|
2018-10-30 18:57:48 +00:00
|
|
|
const postcss = require( 'postcss' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
const getPackages = require( './get-packages' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module Constants
|
|
|
|
*/
|
|
|
|
const PACKAGES_DIR = path.resolve( __dirname, '../../packages' );
|
|
|
|
const SRC_DIR = 'src';
|
|
|
|
const BUILD_DIR = {
|
|
|
|
main: 'build',
|
|
|
|
module: 'build-module',
|
|
|
|
style: 'build-style',
|
|
|
|
};
|
|
|
|
const DONE = chalk.reset.inverse.bold.green( ' DONE ' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
const isScssFile = ( filepath ) => {
|
|
|
|
return /.\.scss$/.test( filepath );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Build Path for a specified file
|
|
|
|
*
|
|
|
|
* @param {string} file File to build
|
|
|
|
* @param {string} buildFolder Output folder
|
|
|
|
* @return {string} Build path
|
|
|
|
*/
|
|
|
|
function getBuildPath( file, buildFolder ) {
|
2021-03-24 21:29:37 +00:00
|
|
|
// if the file has extension of ts, replace with js
|
|
|
|
const fileName = file.replace( '.ts', '.js' );
|
|
|
|
const pkgName = getPackageName( fileName );
|
2018-10-30 18:57:48 +00:00
|
|
|
const pkgSrcPath = path.resolve( PACKAGES_DIR, pkgName, SRC_DIR );
|
|
|
|
const pkgBuildPath = path.resolve( PACKAGES_DIR, pkgName, buildFolder );
|
2021-03-24 21:29:37 +00:00
|
|
|
const relativeToSrcPath = path.relative( pkgSrcPath, fileName );
|
2018-10-30 18:57:48 +00:00
|
|
|
return path.resolve( pkgBuildPath, relativeToSrcPath );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a list of scss and js filepaths, divide them into sets them and rebuild.
|
|
|
|
*
|
|
|
|
* @param {Array} files list of files to rebuild
|
|
|
|
*/
|
|
|
|
function buildFiles( files ) {
|
|
|
|
// Reduce files into a unique sets of javaScript files and scss packages.
|
2020-05-07 18:24:12 +00:00
|
|
|
const buildPaths = files.reduce(
|
|
|
|
( accumulator, filePath ) => {
|
2021-07-14 20:38:57 +00:00
|
|
|
if ( isScssFile( filePath ) ) {
|
2020-05-07 18:24:12 +00:00
|
|
|
const pkgName = getPackageName( filePath );
|
|
|
|
const pkgPath = path.resolve( PACKAGES_DIR, pkgName );
|
|
|
|
accumulator.scssPackagePaths.add( pkgPath );
|
|
|
|
}
|
|
|
|
return accumulator;
|
|
|
|
},
|
2021-07-14 20:38:57 +00:00
|
|
|
{ scssPackagePaths: new Set() }
|
2020-05-07 18:24:12 +00:00
|
|
|
);
|
2018-10-30 18:57:48 +00:00
|
|
|
|
|
|
|
buildPaths.scssPackagePaths.forEach( buildPackageScss );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a package's scss styles
|
|
|
|
*
|
|
|
|
* @param {string} packagePath The path to the package.
|
|
|
|
*/
|
2020-11-23 20:21:16 +00:00
|
|
|
async function buildPackageScss( packagePath ) {
|
2018-10-30 18:57:48 +00:00
|
|
|
const srcDir = path.resolve( packagePath, SRC_DIR );
|
2021-05-26 19:35:46 +00:00
|
|
|
const scssFiles = glob.sync( `${ srcDir }/**/*.scss` );
|
2018-10-30 18:57:48 +00:00
|
|
|
|
|
|
|
// Build scss files individually.
|
2020-11-23 20:21:16 +00:00
|
|
|
return Promise.all( scssFiles.map( ( file ) => buildScssFile( file ) ) );
|
2018-10-30 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 20:21:16 +00:00
|
|
|
async function buildScssFile( styleFile ) {
|
2020-05-07 18:24:12 +00:00
|
|
|
const outputFile = getBuildPath(
|
|
|
|
styleFile.replace( '.scss', '.css' ),
|
|
|
|
BUILD_DIR.style
|
|
|
|
);
|
|
|
|
const outputFileRTL = getBuildPath(
|
|
|
|
styleFile.replace( '.scss', '-rtl.css' ),
|
|
|
|
BUILD_DIR.style
|
|
|
|
);
|
2020-11-23 20:21:16 +00:00
|
|
|
|
|
|
|
await mkdirp.sync( path.dirname( outputFile ) );
|
|
|
|
const builtSass = await sass.renderSync( {
|
2018-10-30 18:57:48 +00:00
|
|
|
file: styleFile,
|
2020-05-07 18:24:12 +00:00
|
|
|
includePaths: [
|
|
|
|
path.resolve( __dirname, '../../client/stylesheets/abstracts' ),
|
2021-06-02 03:54:00 +00:00
|
|
|
path.resolve( __dirname, '../../node_modules' ),
|
2020-05-07 18:24:12 +00:00
|
|
|
],
|
|
|
|
data:
|
2021-06-10 09:09:27 +00:00
|
|
|
'@forward "sass:math"; @use "sass:math";' +
|
2020-05-07 18:24:12 +00:00
|
|
|
[ 'colors', 'variables', 'breakpoints', 'mixins' ]
|
|
|
|
.map( ( imported ) => `@import "_${ imported }";` )
|
2021-06-10 09:09:27 +00:00
|
|
|
.join( ' ' ) +
|
|
|
|
fs.readFileSync( styleFile, 'utf8' ),
|
2018-10-30 18:57:48 +00:00
|
|
|
} );
|
|
|
|
|
2020-06-15 02:00:25 +00:00
|
|
|
const postCSSConfig = require( '@wordpress/postcss-plugins-preset' );
|
2020-11-23 20:21:16 +00:00
|
|
|
const ltrCSS = await postcss( postCSSConfig ).process( builtSass.css, {
|
|
|
|
from: 'src/app.css',
|
|
|
|
to: 'dest/app.css',
|
|
|
|
} );
|
|
|
|
fs.writeFileSync( outputFile, ltrCSS.css );
|
|
|
|
|
|
|
|
const rtlCSS = await postcss( [ require( 'rtlcss' )() ] ).process( ltrCSS, {
|
|
|
|
from: 'src/app.css',
|
|
|
|
to: 'dest/app.css',
|
|
|
|
} );
|
|
|
|
fs.writeFileSync( outputFileRTL, rtlCSS.css );
|
2018-10-30 18:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the provided package path
|
|
|
|
*
|
|
|
|
* @param {string} packagePath absolute package path
|
|
|
|
*/
|
2020-11-23 20:21:16 +00:00
|
|
|
async function buildPackage( packagePath ) {
|
|
|
|
// Build package CSS files
|
|
|
|
await buildPackageScss( packagePath );
|
|
|
|
|
2018-10-30 18:57:48 +00:00
|
|
|
process.stdout.write( `${ path.basename( packagePath ) }\n` );
|
|
|
|
process.stdout.write( `${ DONE }\n` );
|
|
|
|
}
|
|
|
|
|
|
|
|
const files = process.argv.slice( 2 );
|
|
|
|
|
|
|
|
if ( files.length ) {
|
|
|
|
buildFiles( files );
|
|
|
|
} else {
|
|
|
|
process.stdout.write( chalk.inverse( '>> Building packages \n' ) );
|
2020-11-12 09:31:18 +00:00
|
|
|
getPackages()
|
|
|
|
.filter(
|
|
|
|
( package ) =>
|
|
|
|
! /dependency-extraction-webpack-plugin/.test( package )
|
|
|
|
)
|
|
|
|
.forEach( buildPackage );
|
2018-10-30 18:57:48 +00:00
|
|
|
process.stdout.write( '\n' );
|
|
|
|
}
|