Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<string> $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.
*/
Expand Down
43 changes: 43 additions & 0 deletions tests/ModuleInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading