oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.28k stars 2.69k forks source link

SyntaxError: Missing 'default' export in module... [`yaml` loader - example Runtime plugins] #9987

Open gebederry opened 5 months ago

gebederry commented 5 months ago

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:

// plugins/yamlPlugin.ts
import { plugin } from 'bun'

await plugin({
  name: 'YAML',
  async setup(build) {
    const { load } = await import('js-yaml')

    // when a .yaml file is imported...
    build.onLoad({ filter: /\.(yaml|yml)$/ }, async (args) => {
      // read and parse the file
      const text = await Bun.file(args.path).text()
      const exports = load(text) as Record<string, any>

      // and returns it as a module
      return {
        exports,
        loader: 'object' // special loader for JS objects
      }
    })
  }
})

Preloaded it in bunfig.toml:

preload = ["./plugins/yamlPlugin.ts"]

And created a test.yml file:

name: Tom
age: 10

Attempted to import yaml/yml file in other module:

import test from '../../../../../config/test.yml'
console.log('test::: ', test)

And run it.

In addition,

I got the same SyntaxError output using bun-plugin-yaml.

What is the expected behavior?

Output:

{ name: 'Tom', age: 10 }

What do you see instead?

1 | (function (entry, fetcher)
    ^
SyntaxError: Missing 'default' export in module '/opt/apps/rdo-qq-bot-ts/config/test.yml'.

tRnuH1o.png 3RIylBI.png

Additional information

No response

Mrgaton commented 4 months ago

this is a very popular bug i got it too on 2 libs

yusuf57rl commented 4 months ago

+1

Jinghao1209 commented 3 months ago

Try to use import * as test from './path/to/test.yml';

console.log(test)

It should output:

Module {
  name: 'Tom',
  age: 10
}
Mrgaton commented 3 months ago

The problem is that some libs gives the error too

ryoppippi commented 3 months ago

Me too like ragrex.js

ryoppippi commented 3 months ago

Related https://github.com/oven-sh/bun/issues/7465

Jinghao1209 commented 3 months ago

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;