Skip to main content

Module module

Module module 

Source
Expand description

What a module imports and exports: the spec’s ImportEntry and ExportEntry records (ECMA-262 16.2.1.6), read from the tree.

A bundler, a linter or a code-search tool can build a module graph from these without resolving a file: ModuleRecord::requested_modules are the edges, and the entries say which names cross each one. The entries are sorted as ParseModule sorts them: exports of local names, re-exports of one name (export { a } from "m", and an imported name exported again), and export * from "m".

use parsec_js::module::{ImportName, ModuleRecord};
use parsec_js::{Options, SourceType, parse};

let src = r#"import { readFile as read } from "node:fs";
import config from "./config.json" with { type: "json" };
export { read };
export default function main() {}"#;
let options = Options { source_type: SourceType::Module, ..Options::default() };
let ast = parse(src, &options).unwrap();
let record = ModuleRecord::new(&ast, src);

let specifiers: Vec<_> = record.requested_modules.iter().map(|m| m.specifier.as_str()).collect();
assert_eq!(specifiers, ["node:fs", "./config.json"]);
assert_eq!(record.requested_modules[1].attributes, [("type".to_string(), "json".to_string())]);
assert_eq!(record.import_entries[0].import_name, ImportName::Name("readFile".into()));
assert_eq!(record.import_entries[0].local_name, "read");
// `read` is imported, so exporting it is a re-export of node:fs's
// readFile.
assert_eq!(record.indirect_export_entries[0].export_name.as_deref(), Some("read"));
assert_eq!(record.local_export_entries[0].local_name.as_deref(), Some("main"));

Strings are Rust strings: a specifier with a lone surrogate in it, "\uD800", has U+FFFD in its place. Export and import names can’t have one; the parser rejects them.

Structs§

ExportEntry
One name a module exports.
ImportEntry
One name a module imports.
ModuleRecord
A module’s imports and exports, as ParseModule records them.
ModuleRequest
A module a module asks for: its specifier and import attributes.

Enums§

ExportImportName
What an ExportEntry re-exports from another module.
ImportName
What an ImportEntry imports.