Is there a syntax that allows a library to be compatible with being loaded as either an AMD or ES6 module? Right now I have two different *.js files for each module. One with a define(()=>{ ...
for AMD and one with a export { ModuleName}
for ES6. The export
keyword doesn’t seem to be valid for anywhere other than root scope, so trying to place it in any kind of conditional does not work. If loaded as an AMD module, it generates the error SyntaxError: Unexpected token 'export'
.
This is a .NET WASM library that contains some static JS resources, and there are two scenarios that users would consume the library. One using AMD and the other ES6. I don’t really have a JavaScript compilation pipeline that allows me to detect the usage scenario at the downstream developer’s compile time to conditionally generate adjusted JS. So trying to determine if there’s a syntax that will operate correctly in both scenarios when the module is loaded at runtime.
let SomeInteropHelpers = globalThis.SomeInteropHelpers || {};
define(() => {
(function (SomeInteropHelpers) {
var HelpersProxy = SomeInteropHelpers.HelpersProxy || {};// create child namespace
HelpersProxy.GetArrayObjectItems = function (arrayObject) {
return arrayObject.items;
}
HelpersProxy.Check = function () {
console.log('Checked');
};
SomeInteropHelpers.HelpersProxy = HelpersProxy; // add to parent namespace
})(SomeInteropHelpers || globalThis.SomeInteropHelpers || (globalThis.SomeInteropHelpers = {}));
});
export { SomeInteropHelpers };