Skip to content

Commit

Permalink
Add babel/chokidar transpiler, see #1156
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Nov 26, 2021
1 parent 61d56d6 commit 7a3f04f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ eslint/cache
package-lock.json
build
dist
transpile/cache/status.json
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
"devDependencies": {
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@babel/preset-typescript": "7.16.0",
"@types/lodash": "^4.14.172",
"@types/qunit": "^2.11.2",
"@typescript-eslint/parser": "5.4.0",
"archiver": "^5.3.0",
"axios": "^0.21.4",
"babel-eslint": "^10.1.0",
"chokidar": "^3.5.2",
"docdash": "^1.2.0",
"eslint": "^7.32.0",
"eslint-plugin-react": "^7.25.1",
Expand Down
7 changes: 7 additions & 0 deletions transpile/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2018-2021, University of Colorado Boulder
// @author Sam Reid (PhET Interactive Simulations)
module.exports = {

// Use all of the default rules from eslint file for node code.
extends: '../eslint/chipper_eslintrc.js'
};
100 changes: 100 additions & 0 deletions transpile/transpile-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2021, University of Colorado Boulder

/**
* Transpiles all *.ts and copies all *.js files to chipper/dist. Does not do type checking.
*
* Usage:
* cd chipper
* node transpile/transpile-all.js --watch
*
* OPTIONS:
* --watch continue watching all directories and transpile on detected changes
* --ignoreInitial false (default) transpile all files on startup
* --ignoreInitial true skip initial transpile on startup
* --clean dispose of the cache that tracks file status on startup, can be combined with other commands
* you would need to run --clean if the transpiled file status ever gets out of sync with the
* cache file status, for example if you deleted/modified a transpiled file
*
* @author Sam Reid (PhET Interactive Simulations)
*/

const start = Date.now();
const chokidar = require( 'chokidar' );
const transpileFunction = require( './transpileFunction' );
const fs = require( 'fs' );
const crypto = require( 'crypto' );
const path = require( 'path' );

const statusPath = '../chipper/transpile/cache/status.json';
const args = process.argv.slice( 2 );

// Track the status of each repo
let status = {};

// Make sure a directory exists for the cached status file
fs.mkdirSync( path.dirname( statusPath ), { recursive: true } );

// Clear the cache on `--clean`
if ( args.includes( '--clean' ) ) {
fs.writeFileSync( statusPath, JSON.stringify( {}, null, 2 ) );
}

// Load cached status
try {
status = JSON.parse( fs.readFileSync( statusPath, 'utf-8' ) );
}
catch( e ) {
status = {};
fs.writeFileSync( statusPath, JSON.stringify( status, null, 2 ) );
}

const index = args.indexOf( '--ignoreInitial' );
let ignoreInitial = false;
if ( index >= 0 ) {
if ( args[ index + 1 ] === 'true' ) {
ignoreInitial = true;
}
else if ( args[ index + 1 ] === 'false' ) {
ignoreInitial = false;
}
else {
throw new Error( 'illegal value for ignoreInitial' );
}
}

const activeRepos = fs.readFileSync( '../perennial/data/active-repos', 'utf-8' ).trim().split( '\n' );
const paths = [];
activeRepos.forEach( repo => {
paths.push( `../${repo}/js` );
paths.push( `../${repo}/images` );
paths.push( `../${repo}/sounds` );
} );

chokidar.watch( paths, {
ignoreInitial: ignoreInitial,
ignored: [ '**/chipper/dist/**/' ]
} ).on( 'all', ( event, path ) => {
if ( path.endsWith( '.js' ) || path.endsWith( '.ts' ) ) {
const changeDetectedTime = Date.now();
const text = fs.readFileSync( path, 'utf-8' );
const hash = crypto.createHash( 'md5' ).update( text ).digest( 'hex' );

if ( !status[ path ] || status[ path ].md5 !== hash ) {
status[ path ] = { md5: hash };
transpileFunction( path, text );
fs.writeFileSync( statusPath, JSON.stringify( status, null, 2 ) );
const t = Date.now();
const elapsed = t - changeDetectedTime;
console.log( elapsed + 'ms: ' + path );
}
}
} ).on( 'ready', () => {
if ( !args.includes( '--watch' ) ) {
console.log( 'Finished transpilation in ' + ( Date.now() - start ) + 'ms' );
process.exit( 0 );
}
else {
console.log( 'Finished initial scan in ' + ( Date.now() - start ) + 'ms' );
console.log( 'Watching...' );
}
} );
19 changes: 19 additions & 0 deletions transpile/transpileFunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021, University of Colorado Boulder

const core = require( '@babel/core' );
const fs = require( 'fs' );
const path = require( 'path' );

const root = '../';

module.exports = ( filename, text ) => {
const x = core.transformSync( text, {
filename: filename,
presets: [ '@babel/preset-typescript' ],
sourceMaps: 'inline'
} );
const relativePath = path.relative( root, filename );
const targetPath = path.join( root, 'chipper', 'dist', ...relativePath.split( path.sep ) ).split( '.ts' ).join( '.js' );
fs.mkdirSync( path.dirname( targetPath ), { recursive: true } );
fs.writeFileSync( targetPath, x.code );
};

0 comments on commit 7a3f04f

Please sign in to comment.