From 61e0c60cb0cbfbb6152294f4d6ba9bc5280991a6 Mon Sep 17 00:00:00 2001 From: roble Date: Sun, 11 Oct 2026 21:24:07 +0100 Subject: [PATCH] fix: keep relative imports working when a module is flattened A flattened file sits one folder higher, so an import that reached outside its framework folder (shared lib/, types) was one ../ too long and the build failed. Co-Authored-By: Claude Opus 5.5 --- src/Installer.php | 39 +++++++++++++++++++++++++++++++ tests/ModuleInstallerTest.php | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/Installer.php b/src/Installer.php index a780a61..45f88e4 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -551,6 +551,7 @@ protected function flattenFrameworkFiles(string $jsRoot, string $framework): voi } $this->copyDirectory($jsRoot.'/'.$framework, $jsRoot); + $this->shortenImportsLeavingTheFrameworkFolder($jsRoot, array_keys($targetPaths)); $fs = new Filesystem; foreach (self::KNOWN_FRAMEWORKS as $fw) { @@ -565,6 +566,44 @@ protected function flattenFrameworkFiles(string $jsRoot, string $framework): voi } } + /** + * A flattened file sits one folder higher than it did, so a relative import that + * reached outside its framework folder (shared `lib/`, `types`) is one `../` too long. + * Imports that stayed inside the folder moved along with the file and stay as they are. + * + * @param list $files Paths relative to $jsRoot, as they were inside the framework folder. + */ + private function shortenImportsLeavingTheFrameworkFolder(string $jsRoot, array $files): void + { + foreach ($files as $relative) { + if (! in_array(pathinfo($relative, PATHINFO_EXTENSION), ['vue', 'ts', 'tsx', 'js'], true)) { + continue; + } + + $depth = substr_count(str_replace('\\', '/', $relative), '/'); + $path = $jsRoot.'/'.$relative; + $content = file_get_contents($path); + + $rewritten = preg_replace_callback( + '#((?:from|import|require)\s*\(?\s*[\'"])((?:\.\./)+)#', + function (array $match) use ($depth) { + $levels = substr_count($match[2], '../'); + + if ($levels <= $depth) { + return $match[0]; + } + + return $match[1].($levels === 1 ? './' : str_repeat('../', $levels - 1)); + }, + $content + ); + + if ($rewritten !== $content) { + file_put_contents($path, $rewritten); + } + } + } + /** * Recursively copies all files from $source into $dest, preserving relative paths. */ diff --git a/tests/ModuleInstallerTest.php b/tests/ModuleInstallerTest.php index c247805..8d3cd85 100644 --- a/tests/ModuleInstallerTest.php +++ b/tests/ModuleInstallerTest.php @@ -1529,6 +1529,49 @@ public function test_copy_framework_files_flattens_files_and_removes_framework_s (new Filesystem)->remove($moduleDir); } + public function test_flattening_keeps_relative_imports_pointing_at_shared_files(): void + { + $baseDir = sys_get_temp_dir(); + $moduleDir = $baseDir.'/relative-imports'; + $jsRoot = $moduleDir.'/resources/js'; + mkdir($jsRoot.'/vue/components', 0755, true); + mkdir($jsRoot.'/lib', 0755, true); + file_put_contents($jsRoot.'/lib/money.ts', 'export const money = 1;'); + file_put_contents($jsRoot.'/vue/app.ts', implode("\n", [ + "import { branding } from '../branding';", + "import Card from './components/Card.vue';", + ])); + file_put_contents($jsRoot.'/vue/components/Card.vue', implode("\n", [ + "import { money } from '../../lib/money';", + "import type { Plan } from '../../types';", + "import Other from './Other.vue';", + "import { helper } from '../helper';", + "const lazy = () => import('../../lib/money');", + ])); + + $installer = $this->makeInstallerWithModuleDir($baseDir); + $installer->setSaucebaseJsonPath($this->writeSaucebaseJson(['framework' => 'vue'])); + + $installer->callCopyFrameworkFiles(new Package('saucebase/relative-imports', '1.0.0.0', '1.0.0')); + + // The file moved up one folder, so an import that left the framework folder is one step shorter. + $this->assertSame(implode("\n", [ + "import { branding } from './branding';", + "import Card from './components/Card.vue';", + ]), file_get_contents($jsRoot.'/app.ts')); + + // Imports that stayed inside the framework folder moved with the file and are untouched. + $this->assertSame(implode("\n", [ + "import { money } from '../lib/money';", + "import type { Plan } from '../types';", + "import Other from './Other.vue';", + "import { helper } from '../helper';", + "const lazy = () => import('../lib/money');", + ]), file_get_contents($jsRoot.'/components/Card.vue')); + + (new Filesystem)->remove($moduleDir); + } + public function test_copy_framework_files_removes_all_known_framework_subdirs(): void { $baseDir = sys_get_temp_dir();