1- import { exec } from 'node:child_process' ;
1+ import { exec , spawn } from 'node:child_process' ;
22import path from 'node:path' ;
3+ import { after } from 'node:test' ;
34import fse from 'fs-extra' ;
45import { awaitFileChanges , awaitFileExists } from 'test-helper' ;
56import { describe , expect , test } from 'vitest' ;
@@ -59,46 +60,64 @@ describe('build --watch should handle add / change / unlink', async () => {
5960 const tempSrcPath = path . join ( __dirname , 'test-temp-src' ) ;
6061 await fse . remove ( tempSrcPath ) ;
6162 await fse . remove ( path . join ( __dirname , 'dist' ) ) ;
62- await fse . copy ( path . join ( __dirname , 'src' ) , tempSrcPath ) ;
63+ await fse . copy ( path . join ( __dirname , 'src' ) , path . resolve ( tempSrcPath ) ) ;
6364 const tempConfigFile = path . join ( __dirname , 'test-temp-rslib.config.mjs' ) ;
6465 await fse . remove ( tempConfigFile ) ;
6566 fse . outputFileSync (
6667 tempConfigFile ,
6768 `import { defineConfig } from '@rslib/core';
68- import { generateBundleEsmConfig } from 'test-helper';
69+ import { generateBundleEsmConfig, generateBundleCjsConfig } from 'test-helper';
6970
7071 export default defineConfig({
7172 lib: [
7273 generateBundleEsmConfig({
73- source: {
74- entry: {
75- index: 'test-temp-src',
76- },
77- },
74+ bundle: false,
75+ }),
76+ generateBundleCjsConfig({
7877 bundle: false,
7978 }),
8079 ],
80+ source: {
81+ entry: {
82+ index: ['./test-temp-src/**'],
83+ },
84+ },
8185 });
8286 ` ,
8387 ) ;
8488
8589 const srcIndexFile = path . join ( tempSrcPath , 'index.ts' ) ;
86- const srcFooFile = path . join ( tempSrcPath , 'foo.js' ) ;
90+ const srcFooFile = path . join ( tempSrcPath , 'foo.ts' ) ;
91+ const srcFoo2File = path . join ( tempSrcPath , 'foo2.ts' ) ;
92+ const distIndexFile = path . join ( __dirname , 'dist/esm/index.js' ) ;
8793 const distFooFile = path . join ( __dirname , 'dist/esm/foo.js' ) ;
94+ const distFoo2File = path . join ( __dirname , 'dist/esm/foo2.js' ) ;
8895
89- const process = exec ( `npx rslib build --watch -c ${ tempConfigFile } ` , {
96+ const command = 'npx' ;
97+ const args = [ 'rslib' , 'build' , '--watch' , '-c' , tempConfigFile ] ;
98+ const child = spawn ( command , args , {
9099 cwd : __dirname ,
100+ stdio : 'inherit' , // 将子进程的 stdin、stdout、stderr 直接绑定到父进程
101+ shell : true , // 使用 shell 执行,方便处理复杂的命令
91102 } ) ;
92103
93- // add
104+ await awaitFileExists ( distIndexFile ) ;
105+
94106 fse . outputFileSync ( srcFooFile , `export const foo = 'foo';` ) ;
95- await awaitFileExists ( distFooFile ) ;
107+ fse . outputFileSync ( srcFoo2File , `export const foo2 = 'foo2';` ) ;
108+ await awaitFileExists ( distFoo2File ) ;
96109 const content1 = await fse . readFile ( distFooFile , 'utf-8' ) ;
97110 expect ( content1 ! ) . toMatchInlineSnapshot ( `
98111 "const foo = 'foo';
99112 export { foo };
100113 "
101114 ` ) ;
115+ const content2 = await fse . readFile ( distFoo2File , 'utf-8' ) ;
116+ expect ( content2 ! ) . toMatchInlineSnapshot ( `
117+ "const foo2 = 'foo2';
118+ export { foo2 };
119+ "
120+ ` ) ;
102121
103122 // unlink
104123 // Following "change" cases won't succeed if error is thrown in this step.
@@ -108,13 +127,15 @@ describe('build --watch should handle add / change / unlink', async () => {
108127 const wait = await awaitFileChanges ( distFooFile ) ;
109128 fse . outputFileSync ( srcFooFile , `export const foo = 'foo1';` ) ;
110129 await wait ( ) ;
111- const content2 = await fse . readFile ( distFooFile , 'utf-8' ) ;
112- expect ( content2 ! ) . toMatchInlineSnapshot ( `
130+ const content3 = await fse . readFile ( distFooFile , 'utf-8' ) ;
131+ expect ( content3 ! ) . toMatchInlineSnapshot ( `
113132 "const foo = 'foo1';
114133 export { foo };
115134 "
116135 ` ) ;
117136
118- process . kill ( ) ;
137+ after ( ( ) => {
138+ child . kill ( ) ;
139+ } ) ;
119140 } ) ;
120141} ) ;
0 commit comments