Skip to content

Commit

Permalink
Add support for React HMR #minor
Browse files Browse the repository at this point in the history
  • Loading branch information
craigrileyuk authored Dec 21, 2024
1 parent 852fa07 commit c7a3541
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 1.2.0

- **Feature**: Add support for React dev server

### 1.1.1

- **BugFix**: Don't do strict boolean checking on `did_action`
Expand Down
25 changes: 25 additions & 0 deletions src/ViteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,28 @@ public function usePreloadTagAttributes(callable|array|false $attributes): ViteA
return $this;
}

/**
* Inject required code for React to run in dev/HMR mode
*/
private function reactRefresh(): array
{
if ($this->config->useReact !== true) return [];

return [sprintf(
<<<'HTML'
<script type="module" %s>
import RefreshRuntime from '%s';
RefreshRuntime.injectIntoGlobalHook(window);
window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => (type) => type;
window.__vite_plugin_react_preamble_installed__ = true;
</script>
HTML,
"",
$this->hotAsset('@react-refresh')
)];
}

/**
* Generate Vite tags for an entrypoint.
*/
Expand All @@ -205,8 +227,11 @@ public function generateTags(array|string $entrypoints, string $buildDirectory =
$buildDirectory ??= $this->buildDirectory;

if ($this->isRunningHot()) {
$reactRefresh = $this->reactRefresh();

$entrypoints = ['@vite/client', ...$entrypoints];
$entryTags = array_map(fn ($entrypoint) => $this->makeTagForChunk($entrypoint, $this->hotAsset($entrypoint), null, null), $entrypoints);
$entryTags = array_merge($reactRefresh, $entryTags);
return implode("", $entryTags);
}

Expand Down
2 changes: 2 additions & 0 deletions src/ViteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ViteConfig
public string $namespace;
public bool $footer;
public string $entryHandle;
public bool $useReact;

public function __construct(array $configArray)
{
Expand All @@ -21,5 +22,6 @@ public function __construct(array $configArray)
$this->namespace = $configArray['namespace'] ?? "";
$this->footer = $configArray['footer'] ?? false;
$this->entryHandle = $configArray['entryHandle'] ?? "";
$this->useReact = $configArray['useReact'] ?? false;
}
}
2 changes: 2 additions & 0 deletions src/WpVite.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function getVite(): ViteAdapter
* 'hotFile' => (string) Filename to use for the hotfile
* 'dependencies' => (array) List of dependencies
* 'admin' => (bool) Only enqueue the asset on admin pages
* 'react' => (bool) Use the react dev server in HMR mode
* )
*
* @param array $args The configuration options
Expand Down Expand Up @@ -73,6 +74,7 @@ public function enqueue($args = [])
'dependencies' => $args['dependencies'] ?? [],
'namespace' => $args['namespace'],
'entryHandle' => $args['entryHandle'] ?? "",
'useReact' => $args['react'] ?? false
]);

$hook = isset($args['admin']) && $args['admin'] === true ? 'admin_enqueue_scripts' : 'wp_enqueue_scripts';
Expand Down

0 comments on commit c7a3541

Please sign in to comment.