Skip to content

Commit

Permalink
Merge pull request #12 from gwleuverink/refactor/bun-cli-to-js-api
Browse files Browse the repository at this point in the history
Refactor/bun cli to js api
  • Loading branch information
gwleuverink authored Feb 13, 2024
2 parents aaf5b0b + 68be742 commit 0e62bed
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 37 deletions.
27 changes: 8 additions & 19 deletions src/Bundlers/Bun.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,17 @@ public function build(
bool $minify = true
): SplFileInfo {

$path = base_path('node_modules/.bin/');
$bun = base_path('node_modules/.bin/bun');
$buildScript = __DIR__ . '/bin/bun.js';
$options = [
// '--splitting', // Breaks relative paths to imports from resources/js (TODO: Experiment more after writing tests)
'--chunk-naming' => 'chunks/[name]-[hash].[ext]', // Not in use without --splitting
'--asset-naming' => 'assets/[name]-[hash].[ext]', // Not in use without --splitting
'--entrypoints' => $inputPath . $fileName,
'--public-path' => $outputPath,
'--outdir' => $outputPath,
'--target' => 'browser',
'--root' => $inputPath,
'--format' => 'esm',

$sourcemaps
? '--sourcemap=external'
: '--sourcemap=none',

$minify
? '--minify'
: '',
'--entrypoint' => $inputPath . $fileName,
'--inputPath' => $inputPath,
'--outputPath' => $outputPath,
...[$sourcemaps ? '--sourcemaps' : ''],
...[$minify ? '--minify' : ''],
];

Process::run("{$path}bun build {$this->args($options)}")
Process::run("{$bun} {$buildScript} {$this->args($options)}")
->throw(function ($res) use ($inputPath, $fileName): void {
$failed = file_get_contents($inputPath . $fileName);
throw new BundlingFailedException($res, $failed);
Expand Down
56 changes: 56 additions & 0 deletions src/Bundlers/bin/bun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { parseArgs } from "util";

const options = parseArgs({
args: Bun.argv,
strict: true,
allowPositionals: true,

options: {
entrypoint: {
type: "string",
},

inputPath: {
type: "string",
},

outputPath: {
type: "string",
},

sourcemaps: {
type: "boolean",
},

minify: {
type: "boolean",
},
},
}).values;

const result = await Bun.build({
entrypoints: [options.entrypoint],
publicPath: options.outputPath,
outdir: options.outputPath,
root: options.inputPath,
minify: options.minify,

sourcemap: options.sourcemaps ? "external" : "none",

naming: {
entry: '[dir]/[name].[ext]',
chunk: "chunks/[name]-[hash].[ext]", // Not in use without --splitting
asset: "assets/[name]-[hash].[ext]", // Not in use without --splitting
},

target: "browser",
format: "esm",
});

if (!result.success) {
console.error("Build failed");
for (const message of result.logs) {
console.error(message);
}
process.exit(1); // Exit with an error code
}
14 changes: 7 additions & 7 deletions tests/Browser/AlpineInteropTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function it_can_bootstrap_alpine_via_iife_import()
// Doesn't raise console errors
$this->assertEmpty($browser->driver->manage()->getLog('browser'));

$browser->assertSeeIn('#component', 'Hello World!');
$browser->waitForTextIn('#component', 'Hello World!');
}

/** @test */
Expand All @@ -48,7 +48,7 @@ public function it_can_bootstrap_plugins_via_iife_import()
// Doesn't raise console errors
$this->assertEmpty($browser->driver->manage()->getLog('browser'));

$browser->assertSeeIn('#component', 'Hello World!');
$browser->waitForTextIn('#component', 'Hello World!');
}

/** @test */
Expand Down Expand Up @@ -79,7 +79,7 @@ public function it_can_use_imports_from_x_init()
// Doesn't raise console errors
$this->assertEmpty($browser->driver->manage()->getLog('browser'));

$browser->assertSeeIn('#component', 'Fello World!');
$browser->waitForTextIn('#component', 'Fello World!');

}

Expand Down Expand Up @@ -114,7 +114,7 @@ public function it_can_use_imports_from_x_data()
// Doesn't raise console errors
$this->assertEmpty($browser->driver->manage()->getLog('browser'));

$browser->assertSeeIn('#component', 'Gello World!');
$browser->waitForTextIn('#component', 'Gello World!');
}

/** @test */
Expand Down Expand Up @@ -144,9 +144,9 @@ public function it_can_use_imports_from_x_click_listener()
HTML);

$browser
->assertSeeIn('#component', 'Click to change text')
->waitForTextIn('#component', 'Click to change text')
->press('#component')
->assertSeeIn('#component', 'Cello World!');
->waitForTextIn('#component', 'Cello World!');

// Doesn't raise console errors
$this->assertEmpty($browser->driver->manage()->getLog('browser'));
Expand All @@ -173,6 +173,6 @@ public function it_supports_backed_components_with_alpine_data()
// Doesn't raise console errors
$this->assertEmpty($browser->driver->manage()->getLog('browser'));

$browser->assertSeeIn('#component', 'Hello backed component!');
$browser->waitForTextIn('#component', 'Hello backed component!');
}
}
12 changes: 6 additions & 6 deletions tests/Browser/LivewireInteropTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public function it_can_use_imports_from_x_init()
{
$browser = $this->serveLivewire(CanUseImportsFromXInit::class);

$browser->assertSeeIn('#component', 'Hello from x-init!');
$browser->waitForTextIn('#component', 'Hello from x-init!');
}

/** @test */
public function it_can_use_imports_from_x_data()
{
$browser = $this->serveLivewire(CanUseImportsFromXData::class);

$browser->assertSeeIn('#component', 'Hello from x-data!');
$browser->waitForTextIn('#component', 'Hello from x-data!');
}

/** @test */
Expand All @@ -61,9 +61,9 @@ public function it_can_use_imports_from_x_click_listener()
$browser = $this->serveLivewire(CanUseImportsFromClickListener::class);

$browser
->assertSeeIn('#component', 'Click to change text')
->waitForTextIn('#component', 'Click to change text')
->press('#component')
->assertSeeIn('#component', 'Hello from x-on:click!');
->waitForTextIn('#component', 'Hello from x-on:click!');
}

/** @test */
Expand All @@ -72,9 +72,9 @@ public function it_can_use_imports_in_actions_using_one_time_js_expressions()
$browser = $this->serveLivewire(CanUseImportsFromAction::class);

$browser
->assertSeeIn('#component', 'Text changes when the Livewire action is invoked')
->waitForTextIn('#component', 'Text changes when the Livewire action is invoked')
->waitForLivewire()->click('@action')
->assertSeeIn('#component', 'Hello from wire action!');
->waitForTextIn('#component', 'Hello from wire action!');
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Browser/LocalModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function it_imports_from_local_resource_directory()
<div id="output"></div>
HTML)
->assertSeeIn('#output', 'Yello World!');
->waitForTextIn('#output', 'Yello World!');
}

/** @test */
Expand All @@ -67,6 +67,6 @@ public function it_can_import_named_functions()
<div id="output"></div>
HTML)
->assertSeeIn('#output', 'Bar');
->waitForTextIn('#output', 'Bar');
}
}
6 changes: 3 additions & 3 deletions tests/Browser/NodeModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function it_imports_from_node_modules()
<div id="output"></div>
HTML)
->assertSeeIn('#output', 'Hello World!');
->waitForTextIn('#output', 'Hello World!');
}

/** @test */
Expand All @@ -70,7 +70,7 @@ public function it_can_import_modules_per_method()
<div id="output"></div>
HTML)
->assertSeeIn('#output', 'Yello World!');
->waitForTextIn('#output', 'Yello World!');
}

/** @test */
Expand Down Expand Up @@ -98,6 +98,6 @@ public function it_can_use_both_local_and_node_module_together_on_the_same_page(
<div id="output"></div>
HTML)
->assertSeeIn('#output', 'Wello World!');
->waitForTextIn('#output', 'Wello World!');
}
}

0 comments on commit 0e62bed

Please sign in to comment.