Skip to content

Commit 8f4e818

Browse files
wip create react refresh tags if needed
1 parent fc920f8 commit 8f4e818

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

packages/vite/src/TagsResolver/DevelopmentTagsResolver.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,21 @@ public function __construct(
2424

2525
public function resolveTags(array $entrypoints): array
2626
{
27-
return arr($entrypoints)
27+
$tags = arr($entrypoints)
2828
->map(function (string $entrypoint) {
2929
if (! Filesystem\exists($entrypoint) && ! Filesystem\exists(root_path($entrypoint))) {
3030
throw new FileSystemEntrypointWasNotFoundException($entrypoint);
3131
}
3232

3333
return $this->createDevelopmentTag($this->fileToAssetPath($entrypoint));
3434
})
35-
->prepend($this->createDevelopmentTag(self::CLIENT_SCRIPT_PATH))
36-
->toArray();
35+
->prepend($this->createDevelopmentTag(self::CLIENT_SCRIPT_PATH));
36+
37+
if ($this->bridgeFile->needsReactRefresh) {
38+
$tags->prepend($this->createReactRefreshTag());
39+
}
40+
41+
return $tags->toArray();
3742
}
3843

3944
private function createDevelopmentTag(string $path): string
@@ -62,4 +67,17 @@ private function fileToAssetPath(string $file): string
6267
->replaceStart(root_path(), '')
6368
->toString();
6469
}
70+
71+
private function createReactRefreshTag(): string
72+
{
73+
return <<<HTML
74+
<script type="module">
75+
import RefreshRuntime from '{$this->bridgeFile->url}/@react-refresh'
76+
RefreshRuntime.injectIntoGlobalHook(window)
77+
window.\$RefreshReg$ = () => {}
78+
window.\$RefreshSig$ = () => (type) => type
79+
window.__vite_plugin_react_preamble_installed__ = true
80+
</script>
81+
HTML;
82+
}
6583
}

packages/vite/src/Vite.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ private function getBridgeFile(): ViteBridgeFile
140140

141141
return static::$bridgeFile = new ViteBridgeFile(
142142
url: $content->get('url'),
143+
needsReactRefresh: $content->get('needsReactRefresh', false),
143144
);
144145
}
145146

packages/vite/src/ViteBridgeFile.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
{
99
public function __construct(
1010
public string $url,
11+
public bool $needsReactRefresh = false,
1112
) {}
1213
}

0 commit comments

Comments
 (0)