From d191807b7b7159a515aaa4a1d39ee8cfdb1e3a9c Mon Sep 17 00:00:00 2001 From: Willem Leuverink Date: Fri, 12 Jan 2024 00:13:50 +0100 Subject: [PATCH] tdd sourcemaps feature --- config/bundle.php | 20 +++++++++++++++++--- src/BundleManager.php | 12 ++++++++++-- src/Bundlers/Bun.php | 11 +++++++---- src/Contracts/BundleManager.php | 5 +++++ src/Contracts/Bundler.php | 2 +- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/config/bundle.php b/config/bundle.php index c7da6bf..7a50ccc 100644 --- a/config/bundle.php +++ b/config/bundle.php @@ -6,13 +6,27 @@ | Caching |-------------------------------------------------------------------------- | - | Here you may specify wherether Bundle loads already compiled scripts - | from disk when previously compiled. Typically you want to enable - | this on production & disable this for your local development. + | Here you may specify wherether Bundle loads previously compiled + | js from disk. Typically you want to enable this on production + | and disable this on your local development environment. | */ 'caching_enabled' => env('BUNDLE_CACHING_ENABLED', app()->isProduction()), + + /* + |-------------------------------------------------------------------------- + | Sourcemaps + |-------------------------------------------------------------------------- + | + | Here you may specify wherether Bundle will generate sourcemaps for + | your imports. Sourcemaps are generated as a separate file, so it + | won't impact performance when your imports are build for prod. + | + */ + 'sourcemaps_enabled' => env('BUNDLE_SOURCEMAPS_ENABLED', false), + + /* |-------------------------------------------------------------------------- | Build paths (glob patterns) diff --git a/src/BundleManager.php b/src/BundleManager.php index ad61e18..db0c6e0 100644 --- a/src/BundleManager.php +++ b/src/BundleManager.php @@ -4,11 +4,13 @@ use Throwable; use SplFileInfo; +use Illuminate\Http\Response; use Illuminate\Support\Facades\Storage; use Leuverink\Bundle\Traits\Constructable; +use Illuminate\Config\Repository as ConfigRepository; use Leuverink\Bundle\Contracts\Bundler as BundlerContract; +use Illuminate\Contracts\Config\Repository as RepositoryContract; use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract; -use Illuminate\Http\Response; use Leuverink\Bundle\Contracts\BundleManager as BundleManagerContract; @@ -41,9 +43,10 @@ public function bundle(string $script): SplFileInfo // Attempt bundling & cleanup try { $processed = $this->bundler->build( + sourcemaps: $this->config()->get('sourcemaps_enabled'), inputPath: $this->tempDisk()->path(''), outputPath: $this->buildDisk()->path(''), - fileName: $file + fileName: $file, ); } catch (Throwable $e) { $this->cleanup($file); @@ -59,6 +62,11 @@ public function bundle(string $script): SplFileInfo //-------------------------------------------------------------------------- // Helper methods //-------------------------------------------------------------------------- + public function config(): RepositoryContract + { + return new ConfigRepository(config('bundle')); + } + public function tempDisk(): FilesystemContract { return Storage::build([ diff --git a/src/Bundlers/Bun.php b/src/Bundlers/Bun.php index 4bcdff5..f867fbf 100644 --- a/src/Bundlers/Bun.php +++ b/src/Bundlers/Bun.php @@ -14,7 +14,7 @@ class Bun implements Bundler use Constructable; - public function build(string $inputPath, string $outputPath, string $fileName): SplFileInfo + public function build(string $inputPath, string $outputPath, string $fileName, bool $sourcemaps = false): SplFileInfo { $path = base_path('node_modules/.bin/'); $options = [ @@ -26,10 +26,13 @@ public function build(string $inputPath, string $outputPath, string $fileName): '--outdir' => $outputPath, '--target' => 'browser', '--root' => $inputPath, - '--format' => 'esm', - // '--sourcemap=external', // Maybe only locally? // '--splitting', // Breaks relative paths to imports from resources/js (TODO: Experiment more after writing tests) - '--minify' // Only in production? + '--format' => 'esm', + '--minify', // Only in production? + + $sourcemaps + ? '--sourcemap=external' + : '--sourcemap=none' ]; diff --git a/src/Contracts/BundleManager.php b/src/Contracts/BundleManager.php index 86da4eb..1a580d4 100644 --- a/src/Contracts/BundleManager.php +++ b/src/Contracts/BundleManager.php @@ -6,6 +6,8 @@ use Illuminate\Http\Response; use Leuverink\Bundle\Contracts\Bundler; use Illuminate\Contracts\Filesystem\Filesystem; +use Illuminate\Contracts\Config\Repository as RepositoryContract; + interface BundleManager { @@ -23,6 +25,9 @@ public function buildDisk(): Filesystem; /** Get the contents of a given bundle */ public function bundleContents($fileName): Response; + /** Get the bundle config */ + public function config(): RepositoryContract; + /** Get the contents of a given chunk */ // public function chunkContents($fileName): Response; } diff --git a/src/Contracts/Bundler.php b/src/Contracts/Bundler.php index dfb9807..02c7897 100644 --- a/src/Contracts/Bundler.php +++ b/src/Contracts/Bundler.php @@ -6,5 +6,5 @@ interface Bundler { - public function build(string $inputPath, string $outputPath, string $fileName): SplFileInfo; + public function build(string $inputPath, string $outputPath, string $fileName, bool $sourcemaps = false): SplFileInfo; }