Build modules are separate logical units that can export values. Build projects can import values from one or more build modules. Only values with 'public' visibility can be exported/imported.
There are two ways to import values from a module:
- in a declaration (
import { X } from "MyModule"
), and - within an expression (
importFrom("MyModule").X
).
Values may be imported from one or more modules at the top of a project definition. This is recommended when the imported value is used multiple times in the same project.
import * as Compression from "compression";
const zipArchive = Compression.Zip.pack([ f`file.txt` ]]);
import {Zip} from "compression";
const zipArchive = Zip.pack([ f`file.txt` ]);
import {Zip, Gzip} from "compression";
const zipArchive = Zip.pack([ f`file.txt` ]);
const gzipArchive = Gzip.pack([ f`file.txt` ]);
import {Zip as FastZip} from "compression";
const zipArchive = FastZip.pack([ f`file.txt` ]);
import {Zip as FastZip, GZip as GZ} from "compression";
const zipArchive = FastZip.pack([ f`file.txt` ]);
const gzip = GZ.pack([ f`file.txt` ]);
Elements from other modules may be imported within an expression using the importFrom
function. This function takes the module's name (as a string value) to import and returns an object with all values exported by the selected module.
const zipArchive = importFrom("compression").Zip.pack([ f`file.txt` ]);
Note: Consider using a single import
declaration at the file's top if you find yourself overusing importFrom
with the same module in a single file.
Declarations in a build project file are not visible to other projects unless they are explicitly exported with an export
declaration. The declaration must also be marked as public with the @@public
decorator to be visible to other modules.
export * from "compression";
export { Zip } from "compression";
export { Zip, Gzip } from "compression";
export { Zip as FastZip } from "compression";
export { Zip as FastZip, Gzip as GZ } from "compression";
export const myValue = 42;
const myObject = { "foo": "bar" };
export { myObject };
import * as Compression from "compression";
import { Zip, Gzip as GZ } from "compression";
export { Compression, Zip, GZ as MyGZ };
export const gzipArchive = GZ.pack([ f`file.txt` ]);