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

Custom Loader Plugin sourceMaps not working #6173

Open
M-jerez opened this issue Sep 29, 2023 · 1 comment
Open

Custom Loader Plugin sourceMaps not working #6173

M-jerez opened this issue Sep 29, 2023 · 1 comment
Labels
bug Something isn't working bundler Something to do with the bundler

Comments

@M-jerez
Copy link

M-jerez commented Sep 29, 2023

What version of Bun is running?

1.0.1

What platform is your computer?

Darwin 21.6.0 x86_64 i386

What steps can reproduce the bug?

I'm writing a plugin that uses a custom typescript transformer and outputs js with some extra types metadata. this is a similar scenario to an svelte plugin.

The problem is the sourceMaps are broken, esBuild support adding the source map as inlined sourcemaps, example here. But bun is not reading these source maps.

here is an example of the loader

export function runTypesLoader(options: LoaderOptions = {}): BunPlugin {
    return {
        name: 'bun-plugin-run-types',
        setup(builder) {
            builder.config = {
                ...builder.config,
                sourcemap: 'inline',
            };
            builder.onLoad({filter: /\.(ts|tsx)$/}, (args) => {
                const code = readFileSync(args.path, 'utf8');
                const contents = transform(code, args.path, options).code;
                return {
                    contents,
                    loader: 'js',
                };
            });
        },
    };
}

here is an example of an small size original TS file:

import {typeOf, TypeClass} from '@deepkit/type';
import {RpcError} from '@mionkit/core';

export const RPC_ERROR_TYPE = typeOf<RpcError>() as TypeClass;
export const ERROR_TYPE = typeOf<Error>() as TypeClass;

and here is an example of the transformed JS code returned by builder.onLoad (see the inline sourceMap)

import { typeOf } from '@deepkit/type';
import { RpcError } from '@mionkit/core';
export const RPC_ERROR_TYPE = typeOf([], [() => RpcError, 'P7!']);
export const ERROR_TYPE = typeOf([], ['!']);
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9u.... // rest of inlined url encoded sourceMap

So when there are any errors bun is ignoring the inlined sourceMap and referencing the transformed code instead the original file.

What is the expected behavior?

bun to use the inlined sourceMap and prints valid errors that reference the original file instead the transformed code.

What do you see instead?

No response

Additional information

plugin loader and unit tests here: https://github.com/MionKit/mion/tree/ad-support-for-bun.sh/packages/bun/loader

@M-jerez M-jerez added the bug Something isn't working label Sep 29, 2023
@M-jerez M-jerez changed the title Plugin sourceMaps Custom Loader Plugin sourceMaps not working Sep 29, 2023
@Electroid Electroid added the bundler Something to do with the bundler label Sep 29, 2023
@niieani
Copy link

niieani commented Nov 28, 2023

It would also be useful to be able to pass the sourcemap directly, alongside contents, not just inlined.
Note that this should also work for loaders stacktraces, not just the bundler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bundler Something to do with the bundler
Projects
None yet
Development

No branches or pull requests

3 participants