Skip to content

Commit 673e53e

Browse files
authored
Merge pull request #2 from monster010/1-errorexception-undefined-array-key-1
Fixed #1
2 parents 808212f + 7ed58b7 commit 673e53e

File tree

1 file changed

+40
-66
lines changed

1 file changed

+40
-66
lines changed

src/Vite.php

+40-66
Original file line numberDiff line numberDiff line change
@@ -106,36 +106,36 @@ public function __invoke($entryPoints, $buildDirectory = null)
106106
];
107107
}
108108
}
109-
}
110-
111-
$tags[] = $this->makeTagForChunk(
112-
$epoint,
113-
$this->assetPath("{$buildDirectory}/{$chunk['file']}"),
114-
$chunk,
115-
$manifest
116-
);
117109

118-
foreach ($chunk['css'] ?? [] as $css) {
119-
$partialManifest = $this->array_where($manifest, 'file', $css);
120-
121-
$preloads[] = [
122-
array_key_first($partialManifest),
123-
$this->assetPath("{$buildDirectory}/{$css}"),
124-
$partialManifest[array_key_first($partialManifest)],
125-
$manifest
126-
];
127-
128-
$tags[] = [
129-
array_key_first($partialManifest),
130-
$this->assetPath("{$buildDirectory}/{$css}"),
131-
$partialManifest[array_key_first($partialManifest)],
132-
$manifest
133-
];
110+
$tags[] = $this->makeTagForChunk(
111+
$epoint,
112+
$this->assetPath("{$buildDirectory}/{$chunk['file']}"),
113+
$chunk,
114+
$manifest
115+
);
116+
117+
foreach ($chunk['css'] ?? [] as $css) {
118+
$partialManifest = $this->array_where($manifest, 'file', $css);
119+
120+
$preloads[] = [
121+
array_key_first($partialManifest),
122+
$this->assetPath("{$buildDirectory}/{$css}"),
123+
$partialManifest[array_key_first($partialManifest)],
124+
$manifest
125+
];
126+
127+
$tags[] = [
128+
array_key_first($partialManifest),
129+
$this->assetPath("{$buildDirectory}/{$css}"),
130+
$partialManifest[array_key_first($partialManifest)],
131+
$manifest
132+
];
133+
}
134134
}
135135

136-
[$stylesheets, $scripts] = $this->array_chunk_by(array_unique($tags), fn ($prev, $tag) => str_starts_with($tag, '<link')); // Rework
136+
[$stylesheets, $scripts] = $this->array_partition(array_unique($tags), fn ($prev, $tag) => str_starts_with($tag, '<link')); // Rework
137137

138-
usort(array_unique($preloads), fn ($args) => $this->isStylesheetPath(...$args));
138+
usort($preloads, fn ($args) => $this->isStylesheetPath(...$args));
139139

140140
$preloads = array_map(fn ($args) => $this->makePreloadTagForChunk(...$args), $preloads);
141141

@@ -363,44 +363,18 @@ private function array_where($arr, $key, $value)
363363
});
364364
}
365365

366-
private function array_chunk_by(array $array, callable $callback, bool $preserve_keys = false): array
367-
{
368-
$reducer = function (array $carry, $key) use ($array, $callback, $preserve_keys) {
369-
$current = $array[$key];
370-
$length = count($carry);
371-
372-
if ($length > 0) {
373-
$chunk = &$carry[$length - 1];
374-
end($chunk);
375-
$previous = $chunk[key($chunk)];
376-
377-
if ($callback($previous, $current)) {
378-
// Split, create a new group.
379-
if ($preserve_keys) {
380-
$carry[] = [$key => $current];
381-
} else {
382-
$carry[] = [$current];
383-
}
384-
} else {
385-
// Put into the $currentrent group.
386-
if ($preserve_keys) {
387-
$chunk[$key] = $current;
388-
} else {
389-
$chunk[] = $current;
390-
}
391-
}
392-
} else {
393-
// The first group.
394-
if ($preserve_keys) {
395-
$carry[] = [$key => $current];
396-
} else {
397-
$carry[] = [$current];
398-
}
399-
}
400-
401-
return $carry;
402-
};
403-
404-
return array_reduce(array_keys($array), $reducer, []);
405-
}
366+
private function array_partition(array $array, callable $callback) {
367+
$passed = [];
368+
$failed = [];
369+
370+
foreach($array as $key => $item) {
371+
if($callback($item, $key)) {
372+
$passed[$key] = $item;
373+
} else {
374+
$failed[$key] = $item;
375+
}
376+
}
377+
378+
return [$passed, $failed];
379+
}
406380
}

0 commit comments

Comments
 (0)