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 extension for Text Loader #5710

Closed
robobun opened this issue Sep 19, 2023 · 2 comments · Fixed by #10456
Closed

Custom extension for Text Loader #5710

robobun opened this issue Sep 19, 2023 · 2 comments · Fixed by #10456
Labels
bug Something isn't working needs investigate Needs to be investigated to find the root cause

Comments

@robobun
Copy link

robobun commented Sep 19, 2023

I would like to be able to import .env.example files with the standard module import syntax:

import whatever from './.env.example';

I tried to use a custom loader:

import { plugin } from "bun";

plugin({
  name: "Env",
  async setup(build) {
    build.onLoad({ filter: /\.env\.example/ }, async (args) => {
      return {
        contents: await Bun.file(args.path).arrayBuffer(),
        loader: 'text',
      };
    })
  },
});

Which resulted in the following error:

error: Expected loader to be one of "js", "jsx", "object", "ts", "tsx", "toml", or "json"
error: script "dev:build" exited with code 1 (SIGHUP)
This error seems to come from the Module Loader itself: https://github.com/oven-sh/bun/blob/main/src/bun.js/bindings/ModuleLoader.cpp#L225
The Loader class for bun-types lists text as an option: https://github.com/oven-sh/bun/blob/main/packages/bun-types/bun.d.ts#L3073
It may also look as text isn't listed on the runtime plugin documentation: https://github.com/oven-sh/bun/blob/main/docs/runtime/plugins.md?plain=1#L269

Originally reported on Discord: Custom extension for Text Loader

@joshkolenko
Copy link

@robobun You can achieve this by returning an object with an exports property and the loader set to object. The export will then be treated as an ES module.

For example:

import { plugin } from "bun";

plugin({
  name: "Env",
  async setup(build) {
    build.onLoad({ filter: /\.env\.example/ }, async (args) => {
      return {
        exports: { default: await Bun.file(args.path).arrayBuffer() },
        loader: 'object',
      };
    })
  },
});

@Jarred-Sumner
Copy link
Collaborator

You can do bun --loader:.html:text ./input.js

Jarred-Sumner added a commit that referenced this issue Apr 23, 2024
Fixes #10206
Fixes #5710
Jarred-Sumner added a commit that referenced this issue Apr 23, 2024
…d `"file"` (#10456)

* Fixes #3449

Fixes #10206
Fixes #5710

* Apply formatting changes

* Update loaders.md

* Update text-loader-fixture-import.ts

* Add guide

* Update bundler_loader.test.ts

* Address comment

---------

Co-authored-by: Jarred-Sumner <Jarred-Sumner@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs investigate Needs to be investigated to find the root cause
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants