11import { exec } from 'child_process'
22import { promisify } from 'util'
33import { resolve , join , dirname , relative } from 'path'
4- import { existsSync , writeJson , readFile , writeFile } from 'fs-extra'
4+ import { existsSync , writeJson } from 'fs-extra'
55import glob from 'fast-glob'
66
77import { Plugin , OnDone , HookOptions } from '../interfaces'
@@ -28,7 +28,6 @@ type Options = {
2828
2929class TypeScriptPlugin implements Plugin {
3030 name = 'TypeScriptPlugin'
31- private typescriptResult : { stdout : string } [ ] = [ ]
3231
3332 constructor ( public options : Options = { } as Options ) {
3433 mark ( 'TypeScriptPlugin::constructor' )
@@ -38,11 +37,11 @@ class TypeScriptPlugin implements Plugin {
3837 mark ( 'TypeScriptPlugin::onRun(start)' )
3938 const configPath = this . getConfigPath ( context )
4039 try {
41- this . typescriptResult = await Promise . all ( [
40+ await Promise . all ( [
4241 // prettier-ignore
43- execAsync ( `npx tsc -p ${ configPath } --listEmittedFiles -- module commonjs --outDir ${ output } ` ) ,
42+ execAsync ( `npx tsc -p ${ configPath } --module commonjs --outDir ${ output } ` ) ,
4443 // prettier-ignore
45- execAsync ( `npx tsc -p ${ configPath } --listEmittedFiles -- module esnext --outDir ${ resolve ( output , 'esm' ) } ` ) ,
44+ execAsync ( `npx tsc -p ${ configPath } --module esnext --outDir ${ resolve ( output , 'esm' ) } ` ) ,
4645 ] )
4746 } catch ( error ) {
4847 throw new Error ( error . stdout )
@@ -52,32 +51,6 @@ class TypeScriptPlugin implements Plugin {
5251 done ( )
5352 }
5453
55- async onAfterRun ( done : OnDone ) {
56- if ( this . options . replace !== undefined ) {
57- const [ cjs , esm ] = this . typescriptResult
58- this . replace ( cjs . stdout )
59- this . replace ( esm . stdout )
60- }
61- done ( )
62- }
63-
64- private async replace ( rawFiles : string ) : Promise < void > {
65- // Remove artifacts from tsc stdout.
66- const files = rawFiles
67- . split ( / \n / )
68- . map ( ( x ) => x . replace ( / ^ T S F I L E : \s / , '' ) )
69- . filter ( Boolean )
70- for ( const file of files ) {
71- let content = await readFile ( file , 'utf-8' )
72- for ( const key in this . options . replace ) {
73- const needleRe = new RegExp ( key , 'g' )
74- content = content . replace ( needleRe , this . options . replace [ key ] )
75- }
76- // TODO: Maybe optimized — don't rewrite file if nothing replaced.
77- await writeFile ( file , content )
78- }
79- }
80-
8154 private getConfigPath ( context : string ) : string {
8255 const configPath = resolve ( context , this . options . configPath || 'tsconfig.json' )
8356 if ( ! existsSync ( configPath ) ) {
0 commit comments