Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing to use a ts loader plugin #4519

Open
niklasgrewe opened this issue Sep 6, 2023 · 3 comments
Open

Failing to use a ts loader plugin #4519

niklasgrewe opened this issue Sep 6, 2023 · 3 comments
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API

Comments

@niklasgrewe
Copy link

What version of Bun is running?

0.8.1+16b4bf341acc0f4804f0b6bdf5298c180cd00366

What platform is your computer?

Darwin 22.6.0 arm64 arm

What steps can reproduce the bug?

create a minimal loader that returns static typescript:

import { plugin } from "bun";

plugin({
  name: "Test Loader",
  setup(build) {
    build.onResolve({ filter: /.*/, namespace: "test" }, (args) => ({
      path: args.path,
      namespace: "test",
    }));

    build.onLoad({ filter: /.*/, namespace: "test" }, async (args) => ({
      contents:
        `export function assert(b: boolean): void { if (!b) throw new Error("Assertion failed"); }`,
      loader: "ts",
    }));
  },
});

using it with the following script

import { assert } from "test:test.ts";

assert(false);

What is the expected behavior?

expected output:

error: Assertion failed

What do you see instead?

get the following error:

error: Expected ")" but found ":"
export function assert(b: boolean): void { if (!b) throw new Error("Assertion failed"); }
                        ^
test:test.ts:1:25 24


error: Expected "{" but found "boolean"
export function assert(b: boolean): void { if (!b) throw new Error("Assertion failed"); }
                          ^
test:test.ts:1:27 26


error: Unexpected )
export function assert(b: boolean): void { if (!b) throw new Error("Assertion failed"); }
                                 ^
test:test.ts:1:34 33

Additional information

No response

@niklasgrewe niklasgrewe added the bug Something isn't working label Sep 6, 2023
@lucas-jones
Copy link

lucas-jones commented Sep 6, 2023

I'm no expert, just happen to be wanting to do the same thing.

https://bun.sh/docs/api/transpiler

This works for me

import { plugin, Transpiler } from "bun";

plugin({
    name: "Test",
    async setup(build) {
        const { readFileSync } = await import("fs");

        const transpiler = new Transpiler({ loader: "ts" });

        build.onLoad({ filter: /\.(ts)$/ }, (args) => {

            let typescript = readFileSync(args.path, "utf8");
            const javascript = transpiler.transformSync(typescript);
            
            return {
                contents: javascript
            };
        });
    },
});

@vjpr
Copy link
Contributor

vjpr commented Jan 27, 2024

I noticed a temperamental regression here too. I was using loader: 'tsx' in a plugin. It used to work. But now it would like work once, and then stop working.

https://bun.sh/docs/runtime/plugins#loaders

Note that the returned object has a loader property. This tells Bun which of its internal loaders should be used to handle the result.

This seems to be broken.

@edemaine
Copy link

edemaine commented Jun 21, 2024

I see the same issue; our Civet plugin doesn't handle JSX with loader: 'tsx'. Running the Transpiler manually seems like a reasonable workaround, but I think you also need to give it the tsconfig manually, which is annoying (in a plugin context, where should we search for it?). I also can't seem to get it to emit the implicit import for JSX content; see #8335 (reply in thread)

Update: loader: 'tsx' does seem to work for TypeScript code, whereas loader: 'js' errors. But loader: 'tsx' does not support JSX notation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API
Projects
None yet
Development

No branches or pull requests

5 participants