Open gebederry opened 5 months ago
this is a very popular bug i got it too on 2 libs
+1
Try to use import * as test from './path/to/test.yml';
console.log(test)
It should output:
Module {
name: 'Tom',
age: 10
}
The problem is that some libs gives the error too
Me too like ragrex.js
It is the bun's problem, but for yaml we can do with ourselves.
Noticed that console.log(test)
was typed as Module
, so we just need to add some codes.
I think maybe you can change to:
// from https://bun.sh/docs/runtime/plugins#loaders
import { plugin } from 'bun';
plugin({
name: 'YAML',
setup(builder) {
const { load } = require('js-yaml');
const { readFileSync } = require('fs');
builder.onLoad({ filter: /\.(yaml|yml)$/ }, (args) => {
const text = readFileSync(args.path, 'utf8');
const json = load(text);
return {
exports: {
default: json, // added this line
...json // and this line
},
loader: 'object',
};
});
},
});
The result:
Module {
default: {
age: 10,
name: "Tom",
},
age: 10,
name: "Tom",
}
the original bun-plugin-yaml file
import { BunPlugin } from "bun";
import { readFileSync } from "fs";
import { load } from "js-yaml";
function YamlPlugin(): BunPlugin {
return {
name: "bun-plugin-yaml",
setup(builder) {
builder.onLoad({ filter: /\.(yaml|yml)$/ }, args => {
const text = readFileSync(args.path, "utf8");
const exports = load(text) as Record<string, any>;
return {
exports,
loader: "object",
};
});
},
};
}
export default YamlPlugin;
What version of Bun is running?
1.1.1+ca1dbb4eb
What platform is your computer?
Linux 5.10.0-27-amd64 x86_64-Debian GNU/Linux 11 (bullseye) Microsoft Windows NT 10.0.22631.0 x64
What steps can reproduce the bug?
Created a file that registers the plugin from official docs:
Preloaded it in
bunfig.toml
:And created a
test.yml
file:Attempted to import
yaml/yml
file in other module:And run it.
In addition,
I got the same SyntaxError output using bun-plugin-yaml.
What is the expected behavior?
Output:
What do you see instead?
Additional information
No response