Skip to content

Conversation

@jtenner
Copy link

@jtenner jtenner commented Feb 5, 2025

Hey! Thank you for this amazing software.

The only issue I had was an issue with bun when using the --watch flag.

When the compilation of a given module fails with a syntax error, it prevents the plugin from generating any more modules. This breaks the --watch flag features.

Instead of failing, it makes sense to catch the error, log it to stderr, and then use a previously cached version of that module.

Features added:

  • Bugfix: Prevent plugin from crashing
  • Bugfix: Report errors to console for end users

A few questions:

  1. Is there a specific coding style I should conform to? Or is this solution fine?
  2. How can we "test" this, or should we just accept that it can't be tested well?

To try it out locally:

  1. Install Bun
  2. Initialize a bun project bun init -y
  3. Add bunfig.toml with preload = ["./dist/bun-civet.mjs"]
  4. Create index.civet with Bun.write "./test.txt", "Hello world!"
  5. bun --watch index.civet
  6. Add a syntax error (like a bunch of ='s) and change the "Hello world!" string to something else
  7. Notice that ./text.txt remains unmodified
  8. Remove the syntax errors from index.civet
  9. Check ./text.txt for the corresponding changes

I'm very happy to help or make modifications.

Copy link
Collaborator

@edemaine edemaine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried about this change, because it would cause bun foo.civet to merely display an error, but not give an exit code, when foo.civet has a parse error. I'm trying to figure out whether there's a better approach, though Bun's onLoad doesn't seem to have a way to return an error other than throwing one... Possibly this is just a bug in bun --watch.

contents
loader: 'tsx'
}
const cache = new Map<string, string>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const cache = new Map<string, string>
cache := new Map<string, string>

@edemaine
Copy link
Collaborator

I've reported as oven-sh/bun#17270 — we'll see what the Bun folks think about it.

@uinz
Copy link

uinz commented May 30, 2025

oven-sh/bun#4689 (comment)

try this

# bunfig.toml
preload = ["./plugin.ts"]
// plugin.ts
import { compile } from "@danielx/civet";
import { plugin } from "bun";

await plugin({
  name: "Civet loader",
  async setup(builder) {
    builder.onLoad({ filter: /\.civet$/ }, async ({ path }) => {
-     const source = await file(path).text();
+     const { default: source } = await import(`${path}?`, { with: { type: "text" } });

      const contents = await compile(source);

      return {
        contents,
        loader: "tsx",
      };
    });
  },
});

oven-sh/bun#10983 (comment)

Although the above method can temporarily solve the bun bug, bun has another bug. Calling process.exit() in watch mode will exit the watch process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants