From 923ac39c0b718ac5d488f65232f0dcd7161423d4 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 24 Apr 2023 14:11:59 -0700 Subject: Support plugins in `Bun.build` (#2720) * wip * Implement `onLoad` plugins * Support exceptions and async `onLoad` plugins * Fix filtering * Handle empty files * Fix JSON loader --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> --- src/bun.js/bindings/ModuleLoader.h | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'src/bun.js/bindings/ModuleLoader.h') diff --git a/src/bun.js/bindings/ModuleLoader.h b/src/bun.js/bindings/ModuleLoader.h index 9090abcdc..f24a8fdb1 100644 --- a/src/bun.js/bindings/ModuleLoader.h +++ b/src/bun.js/bindings/ModuleLoader.h @@ -37,6 +37,7 @@ union OnLoadResultValue { struct OnLoadResult { OnLoadResultValue value; OnLoadResultType type; + void* bundlerPluginContext { nullptr }; }; class PendingVirtualModuleResult : public JSC::JSInternalFieldObjectImpl<3> { @@ -55,12 +56,13 @@ public: [](auto& spaces, auto&& space) { spaces.m_subspaceForPendingVirtualModuleResult = std::forward(space); }); } - JS_EXPORT_PRIVATE static PendingVirtualModuleResult* create(VM&, Structure*); - static PendingVirtualModuleResult* create(JSC::JSGlobalObject* globalObject, const WTF::String& specifier, const WTF::String& referrer); + JS_EXPORT_PRIVATE static PendingVirtualModuleResult* create(VM&, Structure*, void* bundlerPluginContext = nullptr); + static PendingVirtualModuleResult* create(JSC::JSGlobalObject* globalObject, const WTF::String& specifier, const WTF::String& referrer, void* bundlerPluginContext = nullptr); static PendingVirtualModuleResult* createWithInitialValues(VM&, Structure*); static Structure* createStructure(VM&, JSGlobalObject*, JSValue); JSC::JSInternalPromise* internalPromise(); + JSC::JSPromise* promise(); static std::array initialValues() { @@ -74,21 +76,32 @@ public: DECLARE_EXPORT_INFO; DECLARE_VISIT_CHILDREN; - PendingVirtualModuleResult(JSC::VM&, JSC::Structure*); + void* m_bundlerPluginContext { nullptr }; + + PendingVirtualModuleResult(JSC::VM&, JSC::Structure*, void* bundlerPluginContext = nullptr); void finishCreation(JSC::VM&, const WTF::String& specifier, const WTF::String& referrer); }; -OnLoadResult handleOnLoadResultNotPromise(Zig::GlobalObject* globalObject, JSC::JSValue objectValue); +OnLoadResult handleOnLoadResult(Zig::GlobalObject* globalObject, JSC::JSValue objectValue, ZigString* specifier, void* bunPluginContext = nullptr); +OnLoadResult handleOnLoadResultNotPromise(Zig::GlobalObject* globalObject, JSC::JSValue objectValue, void* bunPluginContext = nullptr); + +JSValue handleVirtualModuleResultForJSBundlerPlugin( + Zig::GlobalObject* globalObject, + JSValue virtualModuleResult, + const ZigString* specifier, + const ZigString* referrer, + void* bundlerPluginContext); + JSValue fetchSourceCodeSync( Zig::GlobalObject* globalObject, ErrorableResolvedSource* res, - ZigString* specifier, - ZigString* referrer); + const ZigString* specifier, + const ZigString* referrer); JSValue fetchSourceCodeAsync( Zig::GlobalObject* globalObject, ErrorableResolvedSource* res, - ZigString* specifier, - ZigString* referrer); + const ZigString* specifier, + const ZigString* referrer); } // namespace Bun \ No newline at end of file -- cgit v1.2.3