mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
fix(tia): load rolldown from vite/rolldown under Vite+ (#2061)
* fix(tia): load rolldown from vite/rolldown under Vite+ * refactor(tia): call loadRolldown() with its default root --------- Co-authored-by: Ben Dailey <ben@dev-deb12> Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 39 skipped, 1951 passed (4419 assertions)
|
||||
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Symfony\Component\Process\Exception\ProcessFailedException;
|
||||
use Symfony\Component\Process\ExecutableFinder;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
@@ -567,6 +568,59 @@ function tiaBuildOptionResults(): array
|
||||
return $cache = json_decode($process->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 = <<<JS
|
||||
import { loadRolldown } from '{$helper}'
|
||||
const out = {}
|
||||
for (const [name, root] of Object.entries({$roots})) {
|
||||
const loaded = await loadRolldown(root)
|
||||
out[name] = loaded === null ? null : { rolldown: loaded.rolldown }
|
||||
}
|
||||
process.stdout.write(JSON.stringify(out))
|
||||
JS;
|
||||
|
||||
$process = new Process(['node', '--input-type=module', '-e', $script]);
|
||||
$process->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) {
|
||||
|
||||
@@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user