Skip to content

Commit f7b68e9

Browse files
authored
Merge pull request #5 from tonysm/fix-windows
Fix windows file discovery
2 parents b08176d + ec5a253 commit f7b68e9

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

src/AssetResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class AssetResolver
99
{
1010
public function __invoke(string $fileRelativePath)
1111
{
12-
if (! File::exists($absolutePath = Importmap::getRootPath() . '/resources/' . trim($fileRelativePath, '/'))) {
12+
if (! File::exists($absolutePath = Importmap::getFileAbsolutePath('/resources/' . trim($fileRelativePath, '/')))) {
1313
return asset($fileRelativePath);
1414
}
1515

src/Importmap.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Importmap
1414

1515
public function __construct(public ?string $rootPath = null)
1616
{
17-
$this->rootPath = rtrim($this->rootPath ?: base_path(), '/');
17+
$this->rootPath = rtrim($this->rootPath ?: base_path(), DIRECTORY_SEPARATOR);
1818
$this->packages = collect();
1919
$this->directories = collect();
2020
}
@@ -54,6 +54,11 @@ public function getRootPath(): string
5454
return $this->rootPath;
5555
}
5656

57+
public function getFileAbsolutePath(string $relativePath): string
58+
{
59+
return $this->rootPath . str_replace('/', DIRECTORY_SEPARATOR, $relativePath);
60+
}
61+
5762
private function hasManifest(): bool
5863
{
5964
return File::exists($this->manifestPath());
@@ -106,9 +111,9 @@ private function expandDirectories(): Collection
106111
$moduleName = $this->moduleNameFrom($moduleFilename, $mapping);
107112
$modulePath = $this->modulePathFrom($moduleFilename, $mapping);
108113

109-
// We're ignoring anything that starts with `vendor/`, as that's probably
114+
// We're ignoring anything that starts with `vendor`, as that's probably
110115
// being mapped directly as a result of pinning with a --download flag.
111-
if (str_starts_with($moduleFilename, 'vendor/')) {
116+
if (str_starts_with($moduleFilename, 'vendor')) {
112117
return null;
113118
}
114119

@@ -121,10 +126,10 @@ private function expandDirectories(): Collection
121126
private function absoluteRootOf(string $path): string
122127
{
123128
if (Str::startsWith($path, '/')) {
124-
return $path;
129+
return str_replace('/', DIRECTORY_SEPARATOR, $path);
125130
}
126131

127-
return $this->rootPath . '/' . $path;
132+
return $this->rootPath . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $path);
128133
}
129134

130135
private function findJavascriptFilesInTree(string $absolutePath): Collection
@@ -138,23 +143,23 @@ private function findJavascriptFilesInTree(string $absolutePath): Collection
138143

139144
private function relativePathFrom(string $fileAbsolutePath, string $folderAbsolutePath)
140145
{
141-
return trim(Str::after($fileAbsolutePath, $folderAbsolutePath), '/');
146+
return trim(Str::after($fileAbsolutePath, $folderAbsolutePath), DIRECTORY_SEPARATOR);
142147
}
143148

144149
private function moduleNameFrom(string $moduleFileName, MappedDirectory $mapping): string
145150
{
146-
return implode('/', array_filter([
151+
return str_replace(DIRECTORY_SEPARATOR, '/', implode('/', array_filter([
147152
$mapping->under,
148-
preg_replace('#(/?index)?\.jsm?$#', '', $moduleFileName),
149-
]));
153+
preg_replace('#([\\\/]?index)?\.jsm?$#', '', $moduleFileName),
154+
])));
150155
}
151156

152157
private function modulePathFrom(string $moduleFilename, MappedDirectory $mapping): string
153158
{
154-
return implode('/', array_filter([
155-
rtrim($mapping->path ?: $mapping->under, '/'),
159+
return str_replace(DIRECTORY_SEPARATOR, '/', implode('/', array_filter([
160+
rtrim($mapping->path ?: $mapping->under, DIRECTORY_SEPARATOR . '/'),
156161
$moduleFilename,
157-
]));
162+
])));
158163
}
159164

160165
private function resolveAssetPaths(Collection $paths, callable $assetResolver): array

tests/ImportmapTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Tonysm\ImportmapLaravel\Importmap;
55

66
beforeEach(function () {
7-
$this->map = new Importmap(rootPath: __DIR__ . '/stubs/');
7+
$this->map = new Importmap(rootPath: __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR);
88

99
$this->map->pin("app");
1010
$this->map->pin("editor", to: "js/rich_text.js");

0 commit comments

Comments
 (0)