Skip to content

Commit ebe3ef4

Browse files
authored
feat(vite)!: automatically discover entrypoints (#1051)
1 parent b188609 commit ebe3ef4

16 files changed

+130
-83
lines changed

src/Tempest/Vite/src/BuildConfig.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Tempest/Vite/src/Installer/Tailwind/main.css renamed to src/Tempest/Vite/src/Installer/Tailwind/main.entrypoint.css

File renamed without changes.
File renamed without changes.
File renamed without changes.

src/Tempest/Vite/src/Installer/ViteInstaller.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public function install(): void
5959

6060
// Publishes the Vite config
6161
$viteConfig = $this->publish(__DIR__ . "/{$templateDirectory}/vite.config.ts", destination: root_path('vite.config.ts'));
62-
$mainTs = $this->publish(__DIR__ . "/{$templateDirectory}/main.ts", destination: src_path('main.ts'));
62+
$mainTs = $this->publish(__DIR__ . "/{$templateDirectory}/main.entrypoint.ts", destination: src_path('main.entrypoint.ts'));
6363
$mainCss = $shouldInstallTailwind
64-
? $this->publish(__DIR__ . "/{$templateDirectory}/main.css", destination: src_path('main.css'))
64+
? $this->publish(__DIR__ . "/{$templateDirectory}/main.entrypoint.css", destination: src_path('main.entrypoint.css'))
6565
: null;
6666

6767
// Install package.json scripts
@@ -79,8 +79,8 @@ public function install(): void
7979

8080
// Updates the .gitignore
8181
$this->update(root_path('.gitignore'), function (ImmutableString $gitignore) {
82-
if (! $gitignore->contains($this->viteConfig->build->bridgeFileName)) {
83-
$gitignore = $gitignore->append(PHP_EOL, $this->viteConfig->build->bridgeFileName);
82+
if (! $gitignore->contains($this->viteConfig->bridgeFileName)) {
83+
$gitignore = $gitignore->append(PHP_EOL, $this->viteConfig->bridgeFileName);
8484
}
8585

8686
if (! $gitignore->contains('node_modules')) {
@@ -101,18 +101,12 @@ public function install(): void
101101
? '<strong>Vite and Tailwind CSS are now installed in your project</strong>!'
102102
: '<strong>Vite is now installed in your project</strong>!',
103103
PHP_EOL,
104-
$viteConfig
105-
? sprintf('Configure <href="file://%s">vite.config.ts</href> as you see fit', $viteConfig)
106-
: null,
107-
$mainTs
108-
? sprintf(
109-
"Add <code><x-vite-tags :entrypoints='%s' /></code> to the <head> of your template",
110-
json_encode(array_filter([$mainCss, $mainTs]), JSON_UNESCAPED_SLASHES),
111-
)
112-
: 'Create a file and include it in your template with <code><x-vite-tags entrypoint="./path/to/file.ts" /></code>',
113104
"Run <code>{$packageManager->getRunCommand('dev')}</code> to start the <strong>development server</strong>",
105+
$mainTs || $mainCss
106+
? 'Update <code>*.entrypoint.{ts,css}</code> files and observe changes in your browser'
107+
: 'Create a <code>src/main.entrypoint.ts</code> file to get started with front-end code',
114108
PHP_EOL,
115-
// '<style="fg-green">→</style> Read the <href="https://tempestphp.com/ocs/framework/vite">documentation</href>', // TODO: update when we have Vite docs
109+
// '<style="fg-green">→</style> Read the <href="https://tempestphp.com/main/framework/vite">documentation</href>', // TODO: update when we have Vite docs
116110
'<style="fg-green">→</style> Join the <href="https://tempestphp.com/discord">Discord server</href>',
117111
]);
118112
}

src/Tempest/Vite/src/TagsResolver/DevelopmentTagsResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function resolveTags(array $entrypoints): array
2626
{
2727
return arr($entrypoints)
2828
->map(function (string $entrypoint) {
29-
if (! file_exists(root_path($entrypoint))) {
29+
if (! file_exists($entrypoint) && ! file_exists(root_path($entrypoint))) {
3030
throw new FileSystemEntrypointNotFoundException($entrypoint);
3131
}
3232

src/Tempest/Vite/src/TagsResolver/ManifestTagsResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ private function getChunkPath(Chunk $chunk): string
238238

239239
private function getAssetPath(string $path): string
240240
{
241-
return ensure_starts_with($this->viteConfig->build->buildDirectory . '/' . $path, prefix: '/');
241+
return ensure_starts_with($this->viteConfig->buildDirectory . '/' . $path, prefix: '/');
242242
}
243243

244244
private function fileToAssetPath(string $file): string

src/Tempest/Vite/src/Vite.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function setNonce(string $nonce): void
4848
public function getTags(?array $entrypoints = null): array
4949
{
5050
return $this->getTagsResolver()->resolveTags(
51-
array_filter($entrypoints ?: []) ?: array_filter($this->viteConfig->build->entrypoints ?: []),
51+
array_filter($entrypoints ?: []) ?: array_filter($this->viteConfig->entrypoints ?: []),
5252
);
5353
}
5454

@@ -97,7 +97,7 @@ private function getManifest(): Manifest
9797
return static::$manifest;
9898
}
9999

100-
if (! is_file($path = root_path('public', $this->viteConfig->build->buildDirectory, $this->viteConfig->build->manifest))) {
100+
if (! is_file($path = root_path('public', $this->viteConfig->buildDirectory, $this->viteConfig->manifest))) {
101101
throw new ManifestNotFoundException($path);
102102
}
103103

@@ -146,6 +146,6 @@ private function getBridgeFile(): ViteBridgeFile
146146

147147
private function getBridgeFilePath(): string
148148
{
149-
return root_path('public', $this->viteConfig->build->bridgeFileName);
149+
return root_path('public', $this->viteConfig->bridgeFileName);
150150
}
151151
}

src/Tempest/Vite/src/ViteConfig.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@ final class ViteConfig
1010
* @param PrefetchConfig $prefetching Strategy for prefetching assets at runtime.
1111
* @param null|string $nonce The Content Security Policy nonce to apply to all generated tags.
1212
* @param string|false $integrityKey The key to check for integrity hashes within the manifest. Set to `false` to disable.
13-
* @param BuildConfig $build Configuration related to the production build.
1413
* @param bool $useManifestDuringTesting Whether to use the manifest during tests. `false` by default.
14+
* @param string $buildDirectory Name of the directory in which assets will be created by Vite. Relative to the `public` directory.
15+
* @param string $bridgeFileName Name of the bridge file that the development server creates for Tempest to read. Relative to the `public` directory.
16+
* @param string $manifest Name of the build manifest file generated by Vite for Tempest to read.
17+
* @param string[] $entrypoints Paths to the entrypoints, relative to the root of the project.
1518
*/
1619
public function __construct(
1720
public PrefetchConfig $prefetching = new PrefetchConfig(),
18-
public BuildConfig $build = new BuildConfig(),
1921
public ?string $nonce = null,
2022
public string|false $integrityKey = 'integrity',
2123
public bool $useManifestDuringTesting = false,
24+
public string $buildDirectory = 'build',
25+
public string $bridgeFileName = 'vite-tempest',
26+
public string $manifest = 'manifest.json',
27+
public array $entrypoints = [],
2228
) {
2329
}
30+
31+
public function addEntrypoint(string $path): void
32+
{
33+
$this->entrypoints[] = $path;
34+
}
2435
}

src/Tempest/Vite/src/ViteConfigCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function __construct(
1919
public function __invoke(): void
2020
{
2121
$this->console->writeRaw(json_encode([
22-
'build_directory' => $this->viteConfig->build->buildDirectory,
23-
'bridge_file_name' => $this->viteConfig->build->bridgeFileName,
24-
'manifest' => $this->viteConfig->build->manifest,
25-
'entrypoints' => $this->viteConfig->build->entrypoints,
22+
'build_directory' => $this->viteConfig->buildDirectory,
23+
'bridge_file_name' => $this->viteConfig->bridgeFileName,
24+
'manifest' => $this->viteConfig->manifest,
25+
'entrypoints' => $this->viteConfig->entrypoints,
2626
]));
2727
}
2828
}

0 commit comments

Comments
 (0)