@@ -499,11 +499,14 @@ exports.main = function main(argv, options, callback) {
499
499
// Initialize the program
500
500
program = __pin ( assemblyscript . newProgram ( compilerOptions ) ) ;
501
501
502
- // Set up transforms
503
- const transforms = [ ] ;
502
+ // Collect transforms *constructors* from the `--transform` CLI flag as well
503
+ // as the `transform` option into the `transforms` array.
504
+ let transforms = [ ] ;
505
+ // `transform` option from `main()`
504
506
if ( Array . isArray ( options . transforms ) ) {
505
507
transforms . push ( ...options . transforms ) ;
506
508
}
509
+ // `--transform` CLI flag
507
510
if ( opts . transform ) {
508
511
let tsNodeRegistered = false ;
509
512
let transformArgs = unique ( opts . transform ) ;
@@ -514,28 +517,36 @@ exports.main = function main(argv, options, callback) {
514
517
tsNodeRegistered = true ;
515
518
}
516
519
try {
517
- const classOrModule = dynrequire ( dynrequire . resolve ( filename , { paths : [ baseDir , process . cwd ( ) ] } ) ) ;
518
- if ( typeof classOrModule === "function" ) {
519
- Object . assign ( classOrModule . prototype , {
520
- program,
521
- baseDir,
522
- stdout,
523
- stderr,
524
- log : console . error ,
525
- readFile,
526
- writeFile,
527
- listFiles
528
- } ) ;
529
- transforms . push ( new classOrModule ( ) ) ;
530
- } else {
531
- transforms . push ( classOrModule ) ; // legacy module
532
- }
520
+ transforms . push ( dynrequire ( dynrequire . resolve ( filename , { paths : [ baseDir , process . cwd ( ) ] } ) ) ) ;
533
521
} catch ( e ) {
534
522
return callback ( e ) ;
535
523
}
536
524
}
537
525
}
538
526
527
+ // Fix up the prototype of the transforms’ constructors and instantiate them.
528
+ try {
529
+ transforms = transforms . map ( classOrModule => {
530
+ // Except if it’s a legacy module, just pass it through.
531
+ if ( typeof classOrModule !== "function" ) {
532
+ return classOrModule ;
533
+ }
534
+ Object . assign ( classOrModule . prototype , {
535
+ program,
536
+ baseDir,
537
+ stdout,
538
+ stderr,
539
+ log : console . error ,
540
+ readFile,
541
+ writeFile,
542
+ listFiles
543
+ } ) ;
544
+ return new classOrModule ( ) ;
545
+ } ) ;
546
+ } catch ( e ) {
547
+ return callback ( e ) ;
548
+ }
549
+
539
550
function applyTransform ( name , ...args ) {
540
551
for ( let i = 0 , k = transforms . length ; i < k ; ++ i ) {
541
552
let transform = transforms [ i ] ;
0 commit comments