@@ -48,6 +48,73 @@ export async function copyRequiredFiles() {
4848 await Promise . all ( task ) ;
4949}
5050
51+ // function to unlink files from build path, this expect files paths exist in src
52+ // this used when watch mode is enabled and we need to unlink files from build path
53+ // when file is deleted in src path
54+ export async function unlinkFilesFormBuild (
55+ files : string | string [ ] ,
56+ isDir : boolean = false ,
57+ ) {
58+ const { srcPath, lib } = getConfig ( ) ;
59+
60+ const cjsPath = lib ?. cjs ?. output ?. path ;
61+ const esmPath = lib ?. esm ?. output ?. path ;
62+
63+ const hasCJS = fse . pathExistsSync ( cjsPath ) ;
64+ const hasESM = fse . pathExistsSync ( esmPath ) ;
65+
66+ const tasks : Promise < void > [ ] = [ ] ;
67+ const isTS = ( ext : string ) => [ '.ts' , '.tsx' , '.mts' ] . includes ( ext ) ;
68+
69+ function removeFile ( file : string ) {
70+ const relativePath = path . relative ( srcPath , file ) ;
71+
72+ // helper function to remove from both CJS and ESM paths
73+ const removeFromBuildPaths = ( targetPath : string ) => {
74+ if ( hasCJS ) {
75+ const cjsTarget = path . resolve ( cjsPath , targetPath ) ;
76+ if ( fse . existsSync ( cjsTarget ) ) {
77+ tasks . push ( fse . remove ( cjsTarget ) ) ;
78+ }
79+ }
80+
81+ if ( hasESM ) {
82+ const esmTarget = path . resolve ( esmPath , targetPath ) ;
83+ if ( fse . existsSync ( esmTarget ) ) {
84+ tasks . push ( fse . remove ( esmTarget ) ) ;
85+ }
86+ }
87+ } ;
88+
89+ if ( isDir ) {
90+ // for directories, remove directly from both CJS and ESM paths
91+ removeFromBuildPaths ( relativePath ) ;
92+ } else {
93+ // for files, handle extension conversion
94+ const dirName = path . dirname ( relativePath ) ;
95+ const fileName = path . basename ( relativePath ) ;
96+ const ext = path . extname ( fileName ) ;
97+
98+ const buildFileName =
99+ // in case file is ts group then we need to unlink the js file
100+ // but for dts files we do not convert to js
101+ isTS ( ext ) && ! fileName . endsWith ( '.d.ts' )
102+ ? fileName . replace ( ext , '.js' )
103+ : fileName ;
104+
105+ removeFromBuildPaths ( path . join ( dirName , buildFileName ) ) ;
106+ }
107+ }
108+
109+ if ( Array . isArray ( files ) ) {
110+ files . forEach ( removeFile ) ;
111+ } else {
112+ removeFile ( files ) ;
113+ }
114+
115+ await Promise . all ( tasks ) ;
116+ }
117+
51118async function postbuild ( getBuildTime : ReturnType < typeof createTimer > ) {
52119 const { root, srcPath, buildPath, lib } = getConfig ( ) ;
53120
0 commit comments