ES6 modules, IMHO, were inspired by the value of IIFEs, encapsulation being an important benefit. So, refactoring an IIFE could be straightforward.
First you can remove the IIFE wrapper (you don't have to but, there's no benefit to keeping it, and you may have to be careful because the scope for the arguments you're passing in may differ).
If you know that the library is intended for the browser only AND you want to maintain backwards compatibility, then you can replace your root
variable with window
.
The next challenge is to identify the public API and export it. So, say that some of the original API looks like this:
root.MyLib.prototype.somePublicFn = function () {...}
You'd export this function like this
export let somePublicFn = function () {...}
And, when you do
import * as libFns from 'myLib'
libFns
will act as a sort of namespace that will let you do,
libFns.somePublicFn(...)
in the importing module.
And, like I mentioned above, if you want to also make these exports available globally, you'd have to hand-wire this yourself and do something like
const api = {
somePublicFn
...
}
root.MyLib.prototype = Object.assign(root.MyLib.prototype, api)