diff --git a/js/initcontext.go b/js/initcontext.go index a10bc88c814..9842a0f2396 100644 --- a/js/initcontext.go +++ b/js/initcontext.go @@ -170,6 +170,8 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) { if _, err := i.runtime.RunProgram(pgm.pgm); err != nil { return goja.Undefined(), err } + + pgm.exports = module.Get("exports") } return pgm.exports, nil diff --git a/js/module_loading_test.go b/js/module_loading_test.go index e5e51fbaaee..df94a096ca9 100644 --- a/js/module_loading_test.go +++ b/js/module_loading_test.go @@ -318,3 +318,68 @@ func TestLoadCycleBinding(t *testing.T) { }) } } + +func TestBrowserified(t *testing.T) { + fs := afero.NewMemMapFs() + //nolint: lll + require.NoError(t, afero.WriteFile(fs, "/browserified.js", []byte(` + (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.npmlibs = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i