Compare commits

...

1 Commits

Author SHA1 Message Date
Jarred Sumner
4d40d567c5 Fix using text loader in Bun.plugin 2025-05-14 20:41:41 -07:00
7 changed files with 142 additions and 64 deletions

41
src/api/schema.js generated
View File

@@ -21,14 +21,17 @@ const Loader = {
"css": 5,
"file": 6,
"json": 7,
"toml": 8,
"wasm": 9,
"napi": 10,
"base64": 11,
"dataurl": 12,
"text": 13,
"sqlite": 14,
"html": 15,
"jsonc": 8,
"toml": 9,
"wasm": 10,
"napi": 11,
"base64": 12,
"dataurl": 13,
"text": 14,
"bunsh": 15,
"sqlite": 16,
"sqlite_embedded": 17,
"html": 18,
};
const LoaderKeys = {
"1": "jsx",
@@ -38,14 +41,17 @@ const LoaderKeys = {
"5": "css",
"6": "file",
"7": "json",
"8": "toml",
"9": "wasm",
"10": "napi",
"11": "base64",
"12": "dataurl",
"13": "text",
"14": "sqlite",
"15": "html",
"8": "jsonc",
"9": "toml",
"10": "wasm",
"11": "napi",
"12": "base64",
"13": "dataurl",
"14": "text",
"15": "bunsh",
"16": "sqlite",
"17": "sqlite_embedded",
"18": "html",
"jsx": "jsx",
"js": "js",
"ts": "ts",
@@ -53,13 +59,16 @@ const LoaderKeys = {
"css": "css",
"file": "file",
"json": "json",
"jsonc": "jsonc",
"toml": "toml",
"wasm": "wasm",
"napi": "napi",
"base64": "base64",
"dataurl": "dataurl",
"text": "text",
"bunsh": "bunsh",
"sqlite": "sqlite",
"sqlite_embedded": "sqlite_embedded",
"html": "html",
};
const FrameworkEntryPointType = {

View File

@@ -328,21 +328,24 @@ pub const FileWriter = Writer(std.fs.File);
pub const Api = struct {
pub const Loader = enum(u8) {
_none,
jsx,
js,
ts,
tsx,
css,
file,
json,
toml,
wasm,
napi,
base64,
dataurl,
text,
sqlite,
html,
jsx = 1,
js = 2,
ts = 3,
tsx = 4,
css = 5,
file = 6,
json = 7,
jsonc = 8,
toml = 9,
wasm = 10,
napi = 11,
base64 = 12,
dataurl = 13,
text = 14,
bunsh = 15,
sqlite = 16,
sqlite_embedded = 17,
html = 18,
_,
pub fn jsonStringify(self: @This(), writer: anytype) !void {

View File

@@ -994,7 +994,7 @@ pub fn transpileSourceCode(
}
var parse_result: ParseResult = switch (disable_transpilying or
(loader == .json)) {
(loader == .json or loader == .text)) {
inline else => |return_file_only| brk: {
break :brk jsc_vm.transpiler.parseMaybeReturnFileOnly(
parse_options,
@@ -1074,7 +1074,17 @@ pub fn transpileSourceCode(
.source_code = bun.String.createUTF8(parse_result.source.contents),
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.tag = ResolvedSource.Tag.json_for_object_loader,
.tag = .json_for_object_loader,
};
}
if (loader == .text and !disable_transpilying) {
return ResolvedSource{
.allocator = null,
.jsvalue_for_export = bun.String.createUTF8ForJS(globalObject.?, parse_result.source.contents),
.specifier = input_specifier,
.source_url = input_specifier.createIfDifferent(path.text),
.tag = .export_default_object,
};
}

View File

@@ -233,7 +233,8 @@ OnLoadResult handleOnLoadResultNotPromise(Zig::GlobalObject* globalObject, JSC::
loader = BunLoaderTypeNone;
if (JSC::JSString* loaderJSString = loaderValue.toStringOrNull(globalObject)) {
WTF::String loaderString = loaderJSString->value(globalObject);
auto loaderString = loaderJSString->view(globalObject);
RETURN_IF_EXCEPTION(scope, result);
if (loaderString == "js"_s) {
loader = BunLoaderTypeJS;
} else if (loaderString == "object"_s) {
@@ -246,15 +247,19 @@ OnLoadResult handleOnLoadResultNotPromise(Zig::GlobalObject* globalObject, JSC::
loader = BunLoaderTypeTSX;
} else if (loaderString == "json"_s) {
loader = BunLoaderTypeJSON;
} else if (loaderString == "jsonc"_s) {
loader = BunLoaderTypeJSONC;
} else if (loaderString == "toml"_s) {
loader = BunLoaderTypeTOML;
} else if (loaderString == "text"_s) {
loader = BunLoaderTypeTEXT;
}
}
}
}
if (UNLIKELY(loader == BunLoaderTypeNone)) {
throwException(globalObject, scope, createError(globalObject, "Expected loader to be one of \"js\", \"jsx\", \"object\", \"ts\", \"tsx\", \"toml\", or \"json\""_s));
throwException(globalObject, scope, createError(globalObject, "Expected loader to be one of \"js\", \"jsx\", \"object\", \"ts\", \"tsx\", \"toml\", \"json\", \"jsonc\", \"text\""_s));
result.value.error = scope.exception();
return result;
}
@@ -359,6 +364,23 @@ static JSValue handleVirtualModuleResult(
return reject(JSValue::decode(reinterpret_cast<EncodedJSValue>(res->result.err.ptr)));
}
// This makes it so require("my-text-loader") returns a string instead of a {default: string}
switch (res->result.value.tag) {
case SyntheticModuleType::ExportsObject:
case SyntheticModuleType::ExportDefaultObject: {
JSC::JSValue value = JSC::JSValue::decode(res->result.value.jsvalue_for_export);
if (commonJSModule) {
commonJSModule->setExportsObject(value);
commonJSModule->hasEvaluated = true;
return commonJSModule;
}
break;
}
default: {
break;
}
}
auto provider = Zig::SourceProvider::create(globalObject, res->result.value);
return resolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(provider)));
}

View File

@@ -222,16 +222,24 @@ const JSErrorCode JSErrorCodeUserErrorCode = 254;
typedef uint8_t BunLoaderType;
const BunLoaderType BunLoaderTypeNone = 254;
const BunLoaderType BunLoaderTypeJSX = 0;
const BunLoaderType BunLoaderTypeJS = 1;
const BunLoaderType BunLoaderTypeTS = 2;
const BunLoaderType BunLoaderTypeTSX = 3;
const BunLoaderType BunLoaderTypeCSS = 4;
const BunLoaderType BunLoaderTypeFILE = 5;
const BunLoaderType BunLoaderTypeJSON = 6;
const BunLoaderType BunLoaderTypeTOML = 7;
const BunLoaderType BunLoaderTypeWASM = 8;
const BunLoaderType BunLoaderTypeNAPI = 9;
const BunLoaderType BunLoaderTypeJSX = 1;
const BunLoaderType BunLoaderTypeJS = 2;
const BunLoaderType BunLoaderTypeTS = 3;
const BunLoaderType BunLoaderTypeTSX = 4;
const BunLoaderType BunLoaderTypeCSS = 5;
const BunLoaderType BunLoaderTypeFILE = 6;
const BunLoaderType BunLoaderTypeJSON = 7;
const BunLoaderType BunLoaderTypeJSONC = 8;
const BunLoaderType BunLoaderTypeTOML = 9;
const BunLoaderType BunLoaderTypeWASM = 10;
const BunLoaderType BunLoaderTypeNAPI = 11;
const BunLoaderType BunLoaderTypeBASE64 = 12;
const BunLoaderType BunLoaderTypeDATAURL = 13;
const BunLoaderType BunLoaderTypeTEXT = 14;
const BunLoaderType BunLoaderTypeBUNSH = 15;
const BunLoaderType BunLoaderTypeSQLITE = 16;
const BunLoaderType BunLoaderTypeSQLITE_EMBEDDED = 17;
const BunLoaderType BunLoaderTypeHTML = 18;
#pragma mark - Stream

View File

@@ -630,24 +630,24 @@ pub const Format = enum {
};
pub const Loader = enum(u8) {
jsx,
js,
ts,
tsx,
css,
file,
json,
jsonc,
toml,
wasm,
napi,
base64,
dataurl,
text,
bunsh,
sqlite,
sqlite_embedded,
html,
jsx = 1,
js = 2,
ts = 3,
tsx = 4,
css = 5,
file = 6,
json = 7,
jsonc = 8,
toml = 9,
wasm = 10,
napi = 11,
base64 = 12,
dataurl = 13,
text = 14,
bunsh = 15,
sqlite = 16,
sqlite_embedded = 17,
html = 18,
pub const Optional = enum(u8) {
none = 254,
@@ -862,7 +862,7 @@ pub const Loader = enum(u8) {
.html => .html,
.file, .bunsh => .file,
.json => .json,
.jsonc => .json,
.jsonc => .jsonc,
.toml => .toml,
.wasm => .wasm,
.napi => .napi,
@@ -891,6 +891,8 @@ pub const Loader = enum(u8) {
.text => .text,
.html => .html,
.sqlite => .sqlite,
.jsonc => .jsonc,
.bunsh, .sqlite_embedded => .file,
_ => .file,
};
}

View File

@@ -13,6 +13,25 @@ declare global {
var asyncret: any;
}
plugin({
name: "text",
setup(builder) {
builder.onLoad({ filter: /my-text-file/, namespace: "text" }, async ({ path }) => {
console.log("text", path);
return {
contents: "hello!!",
loader: "text",
};
});
builder.onResolve({ filter: /my-text-file/, namespace: "text" }, ({ path }) => {
return {
path,
namespace: "text",
};
});
},
});
plugin({
name: "url text file loader",
setup(builder) {
@@ -549,3 +568,8 @@ it("recursion throws stack overflow at entry point", () => {
expect(result.stderr.toString()).toContain("RangeError: Maximum call stack size exceeded.");
});
it("text loader works", () => {
const result = require("text:my-text-file");
expect(result).toBe("hello!!");
});