-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sebastian Schöps
committed
May 4, 2021
0 parents
commit 3cc97d5
Showing
9 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php | ||
|
||
return [ | ||
'stack' => 'scripts', | ||
'url' => 'snippets', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
}; | ||
} | ||
} |