Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schöps committed May 4, 2021
0 parents commit 3cc97d5
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## Installation
`composer require nanuc/js-snippets`

Publish config (optionally):
`php artisan vendor:publish --provider="Nanuc\JSSnippets\JSSnippetsServiceProvider" --tag=config`


## Usage
```
<x-js-snippet>
<script>
console.log('Hello');
console.log('I will');
console.log('get minified');
console.log('and downloaded as a plain Javascript file');
</script>
</x-js-snippet>
```

Scripts in the `js-snippet` component will get minified (thanks to https://github.com/tedious/JShrink) and loaded as separate Javascript source.

## Behind the scenes
The component will minify its content and put it in a file in the view cache.
It will also generate a key based on the content.

Then it will create a script tag with a link that downloads this file as a plain Javascript source.
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "nanuc/js-snippets",
"authors": [
{
"name": "Sebastian Schöps",
"email": "sebastian.schoeps@nanuc.com"
}
],
"require": {
"tedivm/jshrink": "~1.0",
"php": "^7.3|^8.0",
"ext-json": "*"
},
"require-dev": {
"orchestra/testbench": "^6.0"
},
"autoload": {
"psr-4": {
"Nanuc\\JSSnippets\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Nanuc\\JSSnippets\\JSSnippetsServiceProvider"
]
}
}
}
6 changes: 6 additions & 0 deletions config/js-snippets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'stack' => 'scripts',
'url' => 'snippets',
];
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

Route::get(config('js-snippets.url') . '/{hash}.js', \Nanuc\JSSnippets\Http\Controllers\SnippetController::class);
13 changes: 13 additions & 0 deletions src/Http/Controllers/SnippetController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Nanuc\JSSnippets\Http\Controllers;

use Nanuc\JSSnippets\Snippet;

class SnippetController
{
public function __invoke($hash)
{
return response()->download(Snippet::getViewPathForHash($hash));
}
}
27 changes: 27 additions & 0 deletions src/JSSnippetsServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Nanuc\JSSnippets;

use Illuminate\Support\ServiceProvider;
use Nanuc\JSSnippets\View\Components\Snippet;

class JSSnippetsServiceProvider extends ServiceProvider
{
public function boot()
{
$this->publishes([
__DIR__.'/../config/js-snippets.php' => config_path('js-snippets.php'),
], 'config');

$this->loadRoutesFrom(__DIR__.'/../routes/web.php');

$this->loadViewComponentsAs('js', [
Snippet::class,
]);
}

public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/js-snippets.php', 'js-snippets');
}
}
11 changes: 11 additions & 0 deletions src/Snippet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Nanuc\JSSnippets;

class Snippet
{
public static function getViewPathForHash($hash)
{
return storage_path('framework/views/' . $hash . '.js');
}
}
25 changes: 25 additions & 0 deletions src/View/Components/Snippet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Nanuc\JSSnippets\View\Components;

use Illuminate\Support\Facades\File;
use Illuminate\View\Component;
use JShrink\Minifier;

class Snippet extends Component
{
public function render()
{
return function (array $data) {
$hash = md5($data['slot']);
$filepath = \Nanuc\JSSnippets\Snippet::getViewPathForHash($hash);

if(!File::exists($filepath)) {
$minifiedJavascript = Minifier::minify(str_replace(['<script>', '</script>'], '', $data['slot']));
file_put_contents($filepath, $minifiedJavascript);
}

return '@push("' . config('js-snippets.stack') . '")<script src="' . url(config('js-snippets.url') . '/' . $hash . '.js') . '"/>@endpush';
};
}
}

0 comments on commit 3cc97d5

Please sign in to comment.