|
| 1 | +import { writeFile } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { waitForFile } from '@rstackjs/test-utils'; |
| 4 | +import { test } from '#test-helpers'; |
| 5 | + |
| 6 | +test('should restart lib watch build when Rstack config changes', async ({ |
| 7 | + prepareDist, |
| 8 | + execCliAsync, |
| 9 | + logHelper, |
| 10 | +}) => { |
| 11 | + const dist1 = await prepareDist(); |
| 12 | + const dist2 = await prepareDist('dist-2'); |
| 13 | + const configFile = path.join(import.meta.dirname, 'test-temp-rstack.config.ts'); |
| 14 | + const userWatchFile = path.join(import.meta.dirname, 'test-temp-user-watch.txt'); |
| 15 | + |
| 16 | + const writeConfig = (distPath: string) => |
| 17 | + writeFile( |
| 18 | + configFile, |
| 19 | + `import { define } from 'rstack'; |
| 20 | +
|
| 21 | +define.lib({ |
| 22 | + dev: { |
| 23 | + watchFiles: { |
| 24 | + paths: ${JSON.stringify(userWatchFile)}, |
| 25 | + type: 'restart', |
| 26 | + }, |
| 27 | + }, |
| 28 | + output: { |
| 29 | + distPath: '${distPath}', |
| 30 | + }, |
| 31 | +}); |
| 32 | +`, |
| 33 | + ); |
| 34 | + |
| 35 | + await writeFile(userWatchFile, 'initial\n'); |
| 36 | + await writeConfig('dist'); |
| 37 | + |
| 38 | + execCliAsync('lib --watch --config test-temp-rstack.config.ts'); |
| 39 | + await logHelper.expectLog('build completed, watching for changes...'); |
| 40 | + await waitForFile(path.join(dist1, 'index.js')); |
| 41 | + logHelper.clearLogs(); |
| 42 | + |
| 43 | + await writeConfig('dist-2'); |
| 44 | + |
| 45 | + await logHelper.expectLog('restarting build as test-temp-rstack.config.ts changed'); |
| 46 | + await logHelper.expectLog('build completed, watching for changes...'); |
| 47 | + await waitForFile(path.join(dist2, 'index.js')); |
| 48 | + logHelper.clearLogs(); |
| 49 | + |
| 50 | + await writeFile(userWatchFile, 'changed\n'); |
| 51 | + |
| 52 | + await logHelper.expectLog('restarting build as test-temp-user-watch.txt changed'); |
| 53 | + await logHelper.expectLog('build completed, watching for changes...'); |
| 54 | +}); |
| 55 | + |
| 56 | +test('should restart lib watch build when an imported config file changes', async ({ |
| 57 | + prepareDist, |
| 58 | + execCliAsync, |
| 59 | + logHelper, |
| 60 | +}) => { |
| 61 | + const dist1 = await prepareDist('dist-import-1'); |
| 62 | + const dist2 = await prepareDist('dist-import-2'); |
| 63 | + const configFile = path.join(import.meta.dirname, 'test-temp-import.config.ts'); |
| 64 | + const importedFile = path.join(import.meta.dirname, 'test-temp-imported.ts'); |
| 65 | + |
| 66 | + await writeFile(importedFile, "export const distPath = 'dist-import-1';\n"); |
| 67 | + await writeFile( |
| 68 | + configFile, |
| 69 | + `import { define } from 'rstack'; |
| 70 | +import { distPath } from './test-temp-imported.ts'; |
| 71 | +
|
| 72 | +define.lib({ |
| 73 | + output: { |
| 74 | + distPath, |
| 75 | + }, |
| 76 | +}); |
| 77 | +`, |
| 78 | + ); |
| 79 | + |
| 80 | + execCliAsync('lib --watch --config test-temp-import.config.ts'); |
| 81 | + await logHelper.expectLog('build completed, watching for changes...'); |
| 82 | + await waitForFile(path.join(dist1, 'index.js')); |
| 83 | + logHelper.clearLogs(); |
| 84 | + |
| 85 | + await writeFile(importedFile, "export const distPath = 'dist-import-2';\n"); |
| 86 | + |
| 87 | + await logHelper.expectLog('restarting build as test-temp-imported.ts changed'); |
| 88 | + await logHelper.expectLog('build completed, watching for changes...'); |
| 89 | + await waitForFile(path.join(dist2, 'index.js')); |
| 90 | +}); |
0 commit comments