Skip to content

Commit

Permalink
refactor to use bun's js api
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Feb 7, 2024
1 parent 0eb1c64 commit fdccec9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 19 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
}

0 comments on commit fdccec9

Please sign in to comment.