Skip to content

Commit

Permalink
Handle absolute url assets (#15)
Browse files Browse the repository at this point in the history
* Fix #13 handle absolute url assets

* Fix styling

* Fix #13 handle absolute url assets

* wip

* wip

* wip

* Fix styling

* wip

* wip

* wip

* wip

* wip

---------

Co-authored-by: dmason30 <[email protected]>
  • Loading branch information
dmason30 and dmason30 authored Sep 27, 2023
1 parent f50ee77 commit bb042fc
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[/tests/.pest/**.snap]
insert_final_newline = false

[/tests/fixtures/**.{js,css}]
insert_final_newline = false

Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.2, 8.1]
laravel: [10.*, 9.*]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 9.*
testbench: 7.*
carbon: ^2.63
- laravel: 10.*
testbench: 8.*
carbon: ^2.63
Expand Down
14 changes: 6 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@
],
"require": {
"php": "^8.1",
"illuminate/filesystem": "^9.21|^10.0",
"illuminate/filesystem": "^10.0",
"laravel/nova": "^4.22",
"spatie/laravel-package-tools": "^1.9.2"
},
"require-dev": {
"laravel/pint": "^1.0",
"nunomaduro/collision": "^6.0",
"nunomaduro/collision": "^7.0",
"nunomaduro/larastan": "^2.0.1",
"orchestra/testbench": "^7.0|^8.0",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.19",
"pestphp/pest-plugin-laravel": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"phpunit/phpunit": "^9.5",
"spatie/pest-plugin-snapshots": "^1.1"
"phpstan/phpstan-phpunit": "^1.3"
},
"repositories": [
{
Expand Down
19 changes: 9 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Fidum Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
Expand All @@ -37,6 +31,11 @@
<junit outputFile="build/report.junit.xml"/>
</logging>
<php>
<env name="ASSET_URL" value="http://localhost" />
<env name="ASSET_URL" value="http://localhost"/>
</php>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
33 changes: 31 additions & 2 deletions src/Commands/PublishCommand.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

declare(strict_types=1);

namespace Fidum\NovaPackageBundler\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Laravel\Nova\Asset;
use Laravel\Nova\Events\ServingNova;
use Laravel\Nova\Nova;
Expand All @@ -26,12 +29,19 @@ public function handle(Filesystem $files): int
/** @var Asset $file */
foreach (Nova::{$method}() as $file) {
$name = $file->name();
$path = $file->isRemote() ? public_path($file->path()) : $file->path();
$path = $file->path();

if ($file->isRemote() && ! Str::startsWith($path, ['http://', 'https://', '://'])) {
$path = public_path($path);
}

$this->components->task("Reading asset [$name] from [$path]", function () use (&$content, $path) {
$result = file_get_contents($path);
$result = $this->readFile($path);

if ($result) {
$content .= trim($result).PHP_EOL;

return true;
}

return file_exists($path);
Expand Down Expand Up @@ -75,4 +85,23 @@ private function bootTools(): void
$this->line('');
}
}

private function readFile(string $path): ?string
{
$result = @file_get_contents($path, false, stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
'http' => [
'timeout' => 5,
],
]));

if (is_string($result)) {
return $result;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** Test JS content **/
/** Public JS content **/
'use strict';

module.exports = function isObject(x) {
return typeof x === 'object' && x !== null;
};
/** Tool JS content **/
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** Test CSS content **/
/** Public CSS content **/
.container{width:100%}@media (min-width:640px){.container{max-width:640px}}@media (min-width:768px){.container{max-width:768px}}@media (min-width:1024px){.container{max-width:1024px}}@media (min-width:1280px){.container{max-width:1280px}}@media (min-width:1536px){.container{max-width:1536px}}@media (min-width:640px){.sm\:container{width:100%}@media (min-width:640px){.sm\:container{max-width:640px}}@media (min-width:768px){.sm\:container{max-width:768px}}@media (min-width:1024px){.sm\:container{max-width:1024px}}@media (min-width:1280px){.sm\:container{max-width:1280px}}@media (min-width:1536px){.sm\:container{max-width:1536px}}}@media (min-width:768px){.md\:container{width:100%}@media (min-width:640px){.md\:container{max-width:640px}}@media (min-width:768px){.md\:container{max-width:768px}}@media (min-width:1024px){.md\:container{max-width:1024px}}@media (min-width:1280px){.md\:container{max-width:1280px}}@media (min-width:1536px){.md\:container{max-width:1536px}}}@media (min-width:1024px){.lg\:container{width:100%}@media (min-width:640px){.lg\:container{max-width:640px}}@media (min-width:768px){.lg\:container{max-width:768px}}@media (min-width:1024px){.lg\:container{max-width:1024px}}@media (min-width:1280px){.lg\:container{max-width:1280px}}@media (min-width:1536px){.lg\:container{max-width:1536px}}}@media (min-width:1280px){.xl\:container{width:100%}@media (min-width:640px){.xl\:container{max-width:640px}}@media (min-width:768px){.xl\:container{max-width:768px}}@media (min-width:1024px){.xl\:container{max-width:1024px}}@media (min-width:1280px){.xl\:container{max-width:1280px}}@media (min-width:1536px){.xl\:container{max-width:1536px}}}@media (min-width:1536px){.\32xl\:container{width:100%}@media (min-width:640px){.\32xl\:container{max-width:640px}}@media (min-width:768px){.\32xl\:container{max-width:768px}}@media (min-width:1024px){.\32xl\:container{max-width:1024px}}@media (min-width:1280px){.\32xl\:container{max-width:1280px}}@media (min-width:1536px){.\32xl\:container{max-width:1536px}}}
/** Tool CSS content **/
3 changes: 2 additions & 1 deletion tests/OverrideNovaPackagesMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

use Fidum\NovaPackageBundler\Http\Middleware\OverrideNovaPackagesMiddleware;
use Laravel\Nova\Nova;

use function Pest\testDirectory;

it('replaces registered styles and scripts with the bundled files', function () {
$this->app->setBasePath(testDirectory('fixtures'));
expect(public_path())->toBe('tests/fixtures'.DIRECTORY_SEPARATOR.'public');
expect(public_path())->toBe('tests'.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'public');

Nova::script('test-package', testDirectory('fixtures/input/test.js'));
Nova::style('test-package', testDirectory('fixtures/input/test.css'));
Expand Down
23 changes: 17 additions & 6 deletions tests/PublishCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,44 @@

use Fidum\NovaPackageBundler\Tests\Support\TestTool;
use Laravel\Nova\Nova;

use function Pest\Laravel\artisan;
use function Pest\testDirectory;
use function Spatie\Snapshots\assertMatchesFileSnapshot;

it('finds and bundles registered scripts and styles', function () {
$this->app->setBasePath(testDirectory('fixtures'));
expect(public_path())->toBe('tests/fixtures'.DIRECTORY_SEPARATOR.'public');
expect(public_path())->toBe('tests'.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'public');

Nova::$tools = [TestTool::make()];
Nova::serving(function () {
Nova::script('test-package', testDirectory('fixtures/input/test.js'));
Nova::style('test-package', testDirectory('fixtures/input/test.css'));
Nova::remoteScript('/input/public.js');
Nova::remoteStyle('/input/public.css');
Nova::remoteScript('https://unpkg.com/[email protected]/index.js');
Nova::remoteStyle('https://unpkg.com/[email protected]/dist/components.min.css');

// Files that don't exist are handled
Nova::script('test-package', testDirectory('fixtures/this-doesnt-exist.js'));
Nova::style('test-package', testDirectory('fixtures/input/this-doesnt-exist.css'));
Nova::remoteScript('this-doesnt-exist.js');
Nova::remoteStyle('this-doesnt-exist.css');
});

artisan('nova:tools:publish')
->assertSuccessful()
->execute();

$script = public_path('/vendor/nova-tools/app.js');
expect($script)->toBeReadableFile();
assertMatchesFileSnapshot($script);
expect($script)
->toBeReadableFile()
->and(file_get_contents($script))
->toMatchSnapshot();

$style = public_path('/vendor/nova-tools/app.css');
expect($style)->toBeReadableFile();
assertMatchesFileSnapshot($style);
expect($style)->toBeReadableFile()
->and(file_get_contents($style))
->toMatchSnapshot();
});

afterAll(function () {
Expand Down
1 change: 1 addition & 0 deletions tests/Support/TestTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use Laravel\Nova\Nova;
use Laravel\Nova\Tool;

use function Pest\testDirectory;

class TestTool extends Tool
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit bb042fc

Please sign in to comment.