Skip to content

Commit

Permalink
Try caching compiled JavaScript when building packages
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jan 23, 2019
1 parent 527e25c commit 5caf40e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 27 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ notifications:

cache:
directories:
- $HOME/.cache/wordpress-packages
- $HOME/.composer/cache
- $HOME/.jest-cache
- $HOME/.npm
Expand Down
99 changes: 75 additions & 24 deletions bin/packages/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const mkdirp = require( 'mkdirp' );
const sass = require( 'node-sass' );
const postcss = require( 'postcss' );
const deasync = require( 'deasync' );
const crypto = require( 'crypto' );
const minimist = require( 'minimist' );

/**
* Internal dependencies
Expand Down Expand Up @@ -92,14 +94,15 @@ function buildFiles( files ) {
}

/**
* Build a javaScript file for the required environments (node and ES5)
* Build a JavaScript file for the required environments (node and ES5).
*
* @param {string} file File path to build
* @param {boolean} silent Show logs
* @param {string} file File path to build.
* @param {boolean} silent Show logs.
* @param {string} cacheDirectory Path to object cache directory.
*/
function buildJsFile( file, silent ) {
buildJsFileFor( file, silent, 'main' );
buildJsFileFor( file, silent, 'module' );
function buildJsFile( file, silent, cacheDirectory = null ) {
buildJsFileFor( file, silent, 'main', cacheDirectory );
buildJsFileFor( file, silent, 'module', cacheDirectory );
}

/**
Expand Down Expand Up @@ -155,23 +158,39 @@ function buildScssFile( styleFile ) {
}

/**
* Build a file for a specific environment
* 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)
* @param {string} file File path to build.
* @param {boolean} silent Show logs.
* @param {string} environment Dist environment (node or ES5).
* @param {string} cacheDirectory Path to object cache directory.
*/
function buildJsFileFor( file, silent, environment ) {
function buildJsFileFor( file, silent, environment, cacheDirectory ) {
const buildDir = BUILD_DIR[ environment ];
const destPath = getBuildPath( file, buildDir );
const babelOptions = getBabelConfig( environment );
babelOptions.sourceMaps = true;
babelOptions.sourceFileName = file;

const source = fs.readFileSync( file, 'utf8' );

const key = sha1( file + environment + source );
let code = readCacheFile( cacheDirectory, key );
let map = readCacheFile( cacheDirectory, key + '-map' );

if ( ! code || ! map ) {
const babelOptions = getBabelConfig( environment );
babelOptions.sourceMaps = true;
babelOptions.sourceFileName = file;

const transformed = babel.transformSync( source, babelOptions );
code = transformed.code + '\n//# sourceMappingURL=' + path.basename( destPath ) + '.map';
map = JSON.stringify( transformed.map );

writeCacheFile( cacheDirectory, key, code );
writeCacheFile( cacheDirectory, key + '-map', map );
}

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' );
fs.writeFileSync( destPath, code );
fs.writeFileSync( destPath + '.map', map );

if ( ! silent ) {
process.stdout.write(
Expand All @@ -184,12 +203,42 @@ function buildJsFileFor( file, silent, environment ) {
}
}

function sha1( text ) {
const hash = crypto.createHash( 'sha1' );
hash.update( text );
return hash.digest( 'hex' );
}

function readCacheFile( cacheDirectory, key ) {
if ( ! cacheDirectory ) {
return null;
}

const filePath = path.resolve( cacheDirectory, key );
if ( ! fs.existsSync( filePath ) ) {
return null;
}

return fs.readFileSync( filePath, 'utf8' );
}

function writeCacheFile( cacheDirectory, key, text ) {
if ( ! cacheDirectory ) {
return null;
}

const filePath = path.resolve( cacheDirectory, key );
mkdirp.sync( path.dirname( filePath ) );
fs.writeFileSync( filePath, text );
}

/**
* Build the provided package path
* Build the provided package path.
*
* @param {string} packagePath absolute package path
* @param {string} packagePath Absolute package path.
* @param {string} cacheDirectory Path to object cache directory.
*/
function buildPackage( packagePath ) {
function buildPackage( packagePath, cacheDirectory ) {
const srcDir = path.resolve( packagePath, SRC_DIR );
const jsFiles = glob.sync( `${ srcDir }/**/*.js`, {
ignore: [
Expand All @@ -202,21 +251,23 @@ function buildPackage( packagePath ) {
process.stdout.write( `${ path.basename( packagePath ) }\n` );

// Build js files individually.
jsFiles.forEach( ( file ) => buildJsFile( file, true ) );
jsFiles.forEach( ( file ) => buildJsFile( file, true, cacheDirectory ) );

// Build package CSS files
buildPackageScss( packagePath );

process.stdout.write( `${ DONE }\n` );
}

const files = process.argv.slice( 2 );
const {
_: files,
cacheDirectory = path.resolve( process.env.HOME, '.cache/wordpress-packages' ),
} = minimist( process.argv.slice( 2 ) );

if ( files.length ) {
buildFiles( files );
} else {
process.stdout.write( chalk.inverse( '>> Building packages \n' ) );
getPackages()
.forEach( buildPackage );
getPackages().forEach( ( packageName ) => buildPackage( packageName, cacheDirectory ) );
process.stdout.write( '\n' );
}
20 changes: 17 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"lerna": "3.4.3",
"lint-staged": "7.3.0",
"lodash": "4.17.10",
"minimist": "1.2.0",
"mkdirp": "0.5.1",
"node-sass": "4.11.0",
"pegjs": "0.10.0",
Expand Down

0 comments on commit 5caf40e

Please sign in to comment.