Skip to content

Commit

Permalink
fixed build command
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Jan 14, 2024
1 parent db796a6 commit 84edcc9
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 14 deletions.
2 changes: 1 addition & 1 deletion config/bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
|
*/
'build_paths' => [
resource_path('views/**/*.blade.php'),
resource_path('views'),
],
];
2 changes: 1 addition & 1 deletion docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ There are a couple of commands at your disposal:

Scan all your build_paths configured in `config/bundle.php` & compile all your imports.

You may configure what paths are scanned by publishing the Bundle config file and updating the `build_paths` array. Note this config option accepts an array of glob patterns.
You may configure what paths are scanned by publishing the Bundle config file and updating the `build_paths` array. Note this config option accepts an array of paths.

```php
'build_paths' => [
Expand Down
2 changes: 1 addition & 1 deletion docs/production-builds.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Eventhough Bun is very fast, since Bundle transpiles & bundles your imports on t

You may run `php artisan bundle:build` to bundle all your imports beforehand. These will be added to your `storage/app/bundle` directory, make sure to add those to vsc or otherwise build them in CI before deployment.

You can control which paths are scanned by publishing the Bundle config file and updating the `build_paths` array. Note this config option accepts an array of glob patterns.
You can control which paths are scanned by publishing the Bundle config file and updating the `build_paths` array. Note this config option accepts an array paths.

```php
'build_paths' => [
Expand Down
27 changes: 17 additions & 10 deletions src/Commands/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use function Laravel\Prompts\info;
use function Laravel\Prompts\error;
use function Laravel\Prompts\warning;
use function Laravel\Prompts\progress;

class Build extends Command
Expand All @@ -20,24 +21,30 @@ class Build extends Command

public function handle(Finder $finder): int
{
$this->call('bundle:clear');
$this->callSilent('bundle:clear');

// Find and bundle all components
collect(config('bundle.build_paths'))
// Find all files matching given glob pattern
->map(fn ($glob) => $finder->files()->in($glob)->depth(0))
// Find all files matching *.blade.*
->map(fn ($path) => $finder->in($path)->files()->name('*.blade.*'))
// Map them to an array
->flatMap(fn (Finder $iterator) => iterator_to_array($iterator))
// Pregmatch each file for x-bundle components
->flatMap(fn (SplFileInfo $file) => preg_grep('/^<x-bundle.*?>$/', file($file)))
// We have a list of components. Filter uniques
->flatMap(fn (SplFileInfo $file) => preg_grep('/<x-bundle.*?>$/', file($file)))
// Trim whitespace
->map(fn ($component) => trim($component))
// Filter uniques
->unique()
// Handle no no imports found
->whenEmpty(fn () => warning('No usages of <x-bundle /> found in your build_paths.'))
// Start progress bar & render components
->pipe(fn ($components) => progress(
'Building Bundle imports',
$components,
fn ($component) => $this->renderComponent($component)
));
->whenNotEmpty(function ($components) {
progress(
'Building Bundle imports',
$components,
fn ($component) => $this->renderComponent($component)
);
});

if ($this->errors) {
error('Bundle compiled with errors');
Expand Down
40 changes: 39 additions & 1 deletion tests/Feature/Commands/BuildCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,45 @@

// Scan the fixtures dir as build path
config()->set('bundle.build_paths', [
realpath(getcwd() . '/tests/Fixtures'),
realpath(getcwd() . '/tests/Fixtures/build-command-resources'),
]);

// Make sure all cached scripts are cleared
$this->artisan('bundle:clear');
$manager->buildDisk()->assertDirectoryEmpty('');

// Execute build command
$this->artisan('bundle:build');

// Assert expected scripts are present
expect($manager->buildDisk()->allFiles())->toBeGreaterThanOrEqual(1);
});

it('scans paths recursively', function () {
$manager = BundleManager::new();

// Scan the fixtures dir as build path
config()->set('bundle.build_paths', [
realpath(getcwd() . '/tests/Fixtures/build-command-resources'),
]);

// Make sure all cached scripts are cleared
$this->artisan('bundle:clear');
$manager->buildDisk()->assertDirectoryEmpty('');

// Execute build command
$this->artisan('bundle:build');

// Assert expected scripts are present
expect($manager->buildDisk()->allFiles())->toBeGreaterThanOrEqual(2);
});

it('scans wildcard blade extentions like both php & md', function () {
$manager = BundleManager::new();

// Scan the fixtures dir as build path
config()->set('bundle.build_paths', [
realpath(getcwd() . '/tests/Fixtures/build-command-resources/markdown'),
]);

// Make sure all cached scripts are cleared
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<x-bundle import="~/alert" as="alert" />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<x-bundle import="lodash/filter" as="filter" />

0 comments on commit 84edcc9

Please sign in to comment.