Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export lazy objects creation and modules import functionality to plugins #164

Merged
merged 2 commits into from Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -80,6 +80,7 @@
"find-up": "^2.1.0",
"fs-extra": "^7.0.1",
"glob": "^7.1.3",
"import-lazy": "^3.1.0",
"lodash": "^4.17.11",
"mocha": "^5.1.1",
"semver": "^5.6.0",
Expand Down
44 changes: 44 additions & 0 deletions src/internal/util/lazy.ts
@@ -1,3 +1,5 @@
import { Eth } from "web3x/eth";

import { BuidlerError, ERRORS } from "../core/errors";

export function lazyObject<T extends object>(objectCreator: () => T): T {
Expand Down Expand Up @@ -81,3 +83,45 @@ export function lazyObject<T extends object>(objectCreator: () => T): T {
}
});
}

/**
* This function is a lazy version of `require`. It imports a module
* synchronously, by creating a proxy that delays the actual `require` until
* the module is used.
*
* The disadvantage of using this technique is that the type information is
* lost. If done with enough care, this can be manually fixed.
*
* TypeScript doesn't emit `require` calls for modules that are imported only
* because of their types. So if one uses lazyImport along with a normal ESM
* import you can pass the module's type to this function.
*
* An example of this can be:
*
* `import func from "func-mod";`
* `const f = lazyImport<typeof func>("func-mod");`
*
* You can also pass it a selector to import just an element of the module:
*
* `import { Eth } from "web3x/eth";`
* `const LazyEth = lazyImport<Eth>("web3x/eth", "Eth");`
*
* Limitations:
* - It's not entirely clear when to use `typeof`.
* - If you get a compilation error saying something about "namespace" consider
* using a selector.
*/
export function lazyImport<ModuleT = any>(
packageName: string,
selector?: string
): ModuleT {
const importLazy = require("import-lazy");
const lazyRequire = importLazy(require);
const lazyModule = lazyRequire(packageName);

if (selector === undefined) {
return lazyModule;
}

return lazyObject(() => lazyModule[selector]);
}
1 change: 1 addition & 0 deletions src/plugins.ts
Expand Up @@ -4,3 +4,4 @@ export {
readArtifact,
readArtifactSync
} from "./internal/artifacts";
export { lazyObject, lazyImport } from "./internal/util/lazy";
5 changes: 5 additions & 0 deletions yarn.lock
Expand Up @@ -1772,6 +1772,11 @@ ieee754@^1.1.4:
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.12.tgz#50bf24e5b9c8bb98af4964c941cdb0918da7b60b"
integrity sha512-GguP+DRY+pJ3soyIiGPTvdiVXjZ+DbXOxGpXn3eMvNW4x4irjqXm4wHKscC+TfxSJ0yw/S1F24tqdMNsMZTiLA==

import-lazy@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-3.1.0.tgz#891279202c8a2280fdbd6674dbd8da1a1dfc67cc"
integrity sha512-8/gvXvX2JMn0F+CDlSC4l6kOmVaLOO3XLkksI7CI3Ud95KDYJuYur2b9P/PUt/i/pDAMd/DulQsNbbbmRRsDIQ==

imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
Expand Down