rails / jsbundling-rails

Bundle and transpile JavaScript in Rails with esbuild, rollup.js, or Webpack.
MIT License
831 stars 143 forks source link

bun individual files #188

Closed silva96 closed 1 month ago

silva96 commented 7 months ago

How can I get what I use to have using esbuild

in package.json:

"scripts": {
    "build": "esbuild app/javascript/*.* --bundle --sourcemap --outdir=app/assets/builds"
}

This would look like this in chrome developer tools:

CleanShot 2024-01-18 at 12 03 49@2x

There I can add breakpoints to debug each individual stimulus controller.

Now that I'm using Bun

package.json:

  "scripts": {
    "build": "bun bun.config.js"
    }

Default generated bun.config.js

import path from 'path';
import fs from 'fs';

const config = {
  sourcemap: "external",
  entrypoints: ["app/javascript/application.js"],
  outdir: path.join(process.cwd(), "app/assets/builds"),
};

const build = async (config) => {
  const result = await Bun.build(config);

  if (!result.success) {
    if (process.argv.includes('--watch')) {
      console.error("Build failed");
      for (const message of result.logs) {
        console.error(message);
      }
      return;
    } else {
      throw new AggregateError(result.logs, "Build failed");
    }
  }
};

(async () => {
  await build(config);

  if (process.argv.includes('--watch')) {
    fs.watch(path.join(process.cwd(), "app/javascript"), { recursive: true }, (eventType, filename) => {
      console.log(`File changed: ${filename}. Rebuilding...`);
      build(config);
    });
  } else {
    process.exit(0);
  }
})();

Currently only one file in the chrome console, no individual controllers.

CleanShot 2024-01-18 at 11 56 12@2x

pokonski commented 4 months ago

You can provide a list of files to entrypoints: https://bun.sh/docs/bundler#entrypoints