2020-05-11 18:45:32 +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 babel = require( '@babel/core' );
|
|
|
|
const chalk = require( 'chalk' );
|
|
|
|
const mkdirp = require( 'mkdirp' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
const getBabelConfig = require( './get-babel-config' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module Constants
|
|
|
|
*/
|
2020-07-02 18:50:04 +00:00
|
|
|
const PACKAGE_DIR = process.cwd();
|
2020-05-11 18:45:32 +00:00
|
|
|
const SRC_DIR = 'src';
|
|
|
|
const BUILD_DIR = {
|
|
|
|
main: 'build',
|
|
|
|
module: 'build-module',
|
|
|
|
};
|
|
|
|
const DONE = chalk.reset.inverse.bold.green( ' DONE ' );
|
|
|
|
|
|
|
|
const isJsFile = ( filepath ) => {
|
|
|
|
return /.\.js$/.test( filepath );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Build Path for a specified file
|
|
|
|
*
|
2022-04-19 02:59:46 +00:00
|
|
|
* @param {string} file File to build
|
|
|
|
* @param {string} buildFolder Output folder
|
2020-05-11 18:45:32 +00:00
|
|
|
* @return {string} Build path
|
|
|
|
*/
|
|
|
|
function getBuildPath( file, buildFolder ) {
|
2020-07-02 18:50:04 +00:00
|
|
|
const pkgSrcPath = path.resolve( PACKAGE_DIR, SRC_DIR );
|
|
|
|
const pkgBuildPath = path.resolve( PACKAGE_DIR, buildFolder );
|
2020-05-11 18:45:32 +00:00
|
|
|
const relativeToSrcPath = path.relative( pkgSrcPath, file );
|
|
|
|
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.
|
|
|
|
const buildPaths = files.reduce(
|
|
|
|
( accumulator, filePath ) => {
|
|
|
|
if ( isJsFile( filePath ) ) {
|
|
|
|
accumulator.jsFiles.add( filePath );
|
|
|
|
}
|
|
|
|
return accumulator;
|
|
|
|
},
|
|
|
|
{ jsFiles: new Set() }
|
|
|
|
);
|
|
|
|
|
|
|
|
buildPaths.jsFiles.forEach( buildJsFile );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a javaScript file for the required environments (node and ES5)
|
|
|
|
*
|
2022-04-19 02:59:46 +00:00
|
|
|
* @param {string} file File path to build
|
2020-05-11 18:45:32 +00:00
|
|
|
* @param {boolean} silent Show logs
|
|
|
|
*/
|
|
|
|
function buildJsFile( file, silent ) {
|
|
|
|
buildJsFileFor( file, silent, 'main' );
|
|
|
|
buildJsFileFor( file, silent, 'module' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a file for a specific environment
|
|
|
|
*
|
|
|
|
* @param {string} file File path to build
|
|
|
|
* @param {boolean} silent Show logs
|
|
|
|
* @param {string} environment Dist environment (node or es5)
|
|
|
|
*/
|
|
|
|
function buildJsFileFor( file, silent, environment ) {
|
|
|
|
const buildDir = BUILD_DIR[ environment ];
|
|
|
|
const destPath = getBuildPath( file, buildDir );
|
|
|
|
const babelOptions = getBabelConfig( environment );
|
|
|
|
babelOptions.sourceMaps = true;
|
|
|
|
babelOptions.sourceFileName = file;
|
|
|
|
|
|
|
|
mkdirp.sync( path.dirname( destPath ) );
|
|
|
|
const transformed = babel.transformFileSync( file, babelOptions );
|
|
|
|
fs.writeFileSync( destPath + '.map', JSON.stringify( transformed.map ) );
|
|
|
|
fs.writeFileSync(
|
|
|
|
destPath,
|
|
|
|
transformed.code +
|
|
|
|
'\n//# sourceMappingURL=' +
|
|
|
|
path.basename( destPath ) +
|
|
|
|
'.map'
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( ! silent ) {
|
|
|
|
process.stdout.write(
|
|
|
|
chalk.green( ' \u2022 ' ) +
|
2020-07-02 18:50:04 +00:00
|
|
|
path.relative( PACKAGE_DIR, file ) +
|
2020-05-11 18:45:32 +00:00
|
|
|
chalk.green( ' \u21D2 ' ) +
|
2020-07-02 18:50:04 +00:00
|
|
|
path.relative( PACKAGE_DIR, destPath ) +
|
2020-05-11 18:45:32 +00:00
|
|
|
'\n'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the provided package path
|
|
|
|
*
|
|
|
|
* @param {string} packagePath absolute package path
|
|
|
|
*/
|
|
|
|
function buildPackage( packagePath ) {
|
|
|
|
const srcDir = path.resolve( packagePath, SRC_DIR );
|
2020-07-02 18:50:04 +00:00
|
|
|
|
2020-07-02 21:16:01 +00:00
|
|
|
let packageName;
|
|
|
|
try {
|
2021-10-28 19:32:31 +00:00
|
|
|
packageName = require( path.resolve( PACKAGE_DIR, 'package.json' ) )
|
|
|
|
.name;
|
2020-07-02 21:16:01 +00:00
|
|
|
} catch ( e ) {
|
|
|
|
packageName = PACKAGE_DIR.split( path.sep ).pop();
|
|
|
|
}
|
2021-10-28 19:32:31 +00:00
|
|
|
process.stdout.write(
|
|
|
|
chalk.inverse( `>> Building package: ${ packageName }\n` )
|
|
|
|
);
|
2020-07-02 18:50:04 +00:00
|
|
|
|
2020-05-11 18:45:32 +00:00
|
|
|
const jsFiles = glob.sync( `${ srcDir }/**/*.js`, {
|
|
|
|
ignore: [
|
|
|
|
`${ srcDir }/**/test/**/*.js`,
|
|
|
|
`${ srcDir }/**/__mocks__/**/*.js`,
|
|
|
|
],
|
|
|
|
nodir: true,
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Build js files individually.
|
|
|
|
jsFiles.forEach( ( file ) => buildJsFile( file, true ) );
|
|
|
|
|
|
|
|
process.stdout.write( `${ DONE }\n` );
|
|
|
|
}
|
|
|
|
|
|
|
|
const files = process.argv.slice( 2 );
|
|
|
|
|
|
|
|
if ( files.length ) {
|
|
|
|
buildFiles( files );
|
|
|
|
} else {
|
2020-07-02 18:50:04 +00:00
|
|
|
buildPackage( PACKAGE_DIR );
|
2020-05-11 18:45:32 +00:00
|
|
|
}
|