Lazy load platform-specific modules
Closed this issue · 0 comments
Relates to #856
Summary
unix.js
and win.js
(and related modules) are only ever needed on their respective platform. If we can avoid loading them until the platform has been determined we could improve the load time of the library.
Example
For example, changing getHelpersByPlatform
to something like:
- import * as unix from "./unix.js";
- import * as win from "./win.js";
// ...
export function getHelpersByPlatform({ env, platform }) {
if (isWindow({ env, platform })) {
- return win
+ return import("./win.js");
}
- return unix;
+ return import("./unix.js");
}
leveraging dynamic import()
s (note that import()
only does work the first time it's called for a given module, after that it returns immediately).
Problem
Unfortunately, this exact approach won't work because import()
is asynchronous. So, it would require a breaking change since the API currently isn't async
. But, I don't even think that is an option because an async
API doesn't make sense for this library.
Purpose
The purpose of this issue is to find out if it's possible to dynamically import platform-specific modules in a way that preserves a sensible public API. I.e., making the public API async
just to have dynamic imports is not sensible to me (leave a comment if you disagree).
Requirements
- No API changes for the purpose of this change.
- No additional dependencies (vendored or not).