Skip to content

Commit

Permalink
tdd sourcemaps feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Jan 11, 2024
1 parent 61e11b1 commit d191807
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
20 changes: 17 additions & 3 deletions config/bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions src/BundleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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);
Expand All @@ -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([
Expand Down
11 changes: 7 additions & 4 deletions src/Bundlers/Bun.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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'
];


Expand Down
5 changes: 5 additions & 0 deletions src/Contracts/BundleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion src/Contracts/Bundler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit d191807

Please sign in to comment.