diff --git a/bin/pest-tia-vite-deps.mjs b/bin/pest-tia-vite-deps.mjs index 856a6639..341b02b1 100644 --- a/bin/pest-tia-vite-deps.mjs +++ b/bin/pest-tia-vite-deps.mjs @@ -25,8 +25,8 @@ const PAGE_DIR_CANDIDATES = [ 'assets/pages', ] -async function loadRolldown() { - const projectRequire = createRequire(join(PROJECT_ROOT, 'package.json')) +export async function loadRolldown(projectRoot = PROJECT_ROOT) { + const projectRequire = createRequire(join(projectRoot, 'package.json')) let path = null try { path = projectRequire.resolve('rolldown') } catch {} @@ -40,6 +40,10 @@ async function loadRolldown() { } catch {} } + if (path === null) { + try { path = projectRequire.resolve('vite/rolldown') } catch {} + } + if (path === null) return null return await import(pathToFileURL(path).href) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index c3021ebb..71cdac0c 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2322,6 +2322,8 @@ ✓ it ignores a default export that only appears in a comment or string with ('default-export-in-line-comment') ✓ it ignores a default export that only appears in a comment or string with ('default-export-in-block-comment') ✓ it ignores a default export that only appears in a comment or string with ('default-export-in-string') + ✓ it loads the rolldown that Vite+ exposes as vite/rolldown + ✓ it finds no rolldown when neither rolldown nor vite/rolldown resolves - it bundles a script setup component that mentions export default in a comment → rolldown is not resolvable. - it bundles a generic component whose script tag holds a closing angle bracket → rolldown is not resolvable. @@ -2660,4 +2662,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1949 passed (4417 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1951 passed (4419 assertions) \ No newline at end of file diff --git a/tests/Unit/Plugins/Tia/ViteDepsHelper.php b/tests/Unit/Plugins/Tia/ViteDepsHelper.php index 49ee082d..d307f81a 100644 --- a/tests/Unit/Plugins/Tia/ViteDepsHelper.php +++ b/tests/Unit/Plugins/Tia/ViteDepsHelper.php @@ -1,5 +1,6 @@ getOutput(), true, flags: JSON_THROW_ON_ERROR); } +function tiaLoadRolldownResults(): array +{ + static $cache = null; + if ($cache !== null) { + return $cache; + } + + $vitePlus = sys_get_temp_dir().'/pest-tia-vite-plus-'.bin2hex(random_bytes(6)); + $missing = sys_get_temp_dir().'/pest-tia-no-rolldown-'.bin2hex(random_bytes(6)); + + mkdir($vitePlus.'/node_modules/vite', 0755, true); + mkdir($missing, 0755, true); + file_put_contents($vitePlus.'/package.json', '{}'); + file_put_contents($vitePlus.'/node_modules/vite/package.json', tiaJson([ + 'name' => 'vite', + 'type' => 'module', + 'exports' => ['./package.json' => './package.json', './rolldown' => './rolldown.mjs'], + ])); + file_put_contents($vitePlus.'/node_modules/vite/rolldown.mjs', "export const rolldown = 'vite-plus-stub'\n"); + file_put_contents($missing.'/package.json', '{}'); + + $helper = str_replace('\\', '/', tiaViteHelperPath()); + $roots = tiaJson(['vite-plus' => str_replace('\\', '/', $vitePlus), 'missing' => str_replace('\\', '/', $missing)]); + + $script = <<run(); + + foreach (['node_modules/vite/rolldown.mjs', 'node_modules/vite/package.json', 'package.json'] as $file) { + @unlink($vitePlus.'/'.$file); + } + foreach (['node_modules/vite', 'node_modules', ''] as $dir) { + @rmdir($vitePlus.'/'.$dir); + } + @unlink($missing.'/package.json'); + @rmdir($missing); + + if (! $process->isSuccessful()) { + throw new ProcessFailedException($process); + } + + return $cache = json_decode($process->getOutput(), true, flags: JSON_THROW_ON_ERROR); +} + beforeEach(function (): void { if ((new ExecutableFinder)->find('node') === null) { $this->markTestSkipped('node is not available.'); @@ -681,6 +735,14 @@ it('ignores a default export that only appears in a comment or string', function ->and($matches[0])->toHaveCount(2); })->with(['default-export-in-line-comment', 'default-export-in-block-comment', 'default-export-in-string']); +it('loads the rolldown that Vite+ exposes as vite/rolldown', function (): void { + expect(tiaLoadRolldownResults()['vite-plus'])->toBe(['rolldown' => 'vite-plus-stub']); +}); + +it('finds no rolldown when neither rolldown nor vite/rolldown resolves', function (): void { + expect(tiaLoadRolldownResults()['missing'])->toBeNull(); +}); + it('bundles a script setup component that mentions export default in a comment', function (): void { $rolldown = tiaRolldownPackageDir(); if ($rolldown === null) { diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 92dcb586..47152363 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -21,7 +21,7 @@ test('parallel', function () use ($run): void { $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); $profileOutput = $run('tests/Fixtures/Suites/SuccessOnly.php', '--profile'); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1822 passed (3892 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 31 skipped, 1824 passed (3894 assertions)'; expect($output) ->toContain("Tests: {$expected}")