Module: lib/bundler/parseModule

Reads an ES module's shape: what it imports, what it exports, and where its top-level statements sit.

Scope, stated up front

This is a module-structure reader, not an ECMAScript parser. It answers exactly the questions the bundler asks:

  • which specifiers does this module depend on, and under what local names?
  • which names does it export, and where does each one come from?
  • where does each top-level statement begin and end, and what does it declare?

Everything between those declarations is opaque text that the emitter copies verbatim. That is deliberate: the less of JavaScript the bundler claims to understand, the fewer ways it can silently miscompile a module. Where a construct is outside what the reader can describe, it says so (ModuleParseError) and the build fails with a location — the same house rule the compiler follows everywhere else.

Import and export declarations are only legal at the top level of a module, so scanning for them at bracket depth zero, skipping every string, template, regex and comment via module:lib/bundler/scanner, finds all of them and nothing else.

Source:

Classes

ModuleParseError

Members

(inner, constant) BINDING_KEYWORDS :Set:.<string:>

Declaration keywords that introduce a binding at the top level.

Type:
  • Set:.<string:>
Source:

(inner, constant) recordCache :LruCache

Module records by source text.

Reading a module is a pure function of its source -- the file name is used only in diagnostics -- so the same text always yields the same record. That matters because the runtime's modules are re-read on every build, and avenx watch rebuilds on every keystroke: without this, a watch rebuild re-parsed seventy modules that had not changed. Bounded, because a long watch session should not accumulate every version of every file a developer has typed.

Type:
Source:

Methods

(static) declaredNames(text) → {Array:.<string:>}

Reads the names a top-level declaration introduces.

Handles const/let/var (including object and array destructuring patterns), function, async function, function* and class. Anything else — an expression statement, a bare call — declares nothing, which is the correct answer rather than a failure.

Parameters:
Name Type Description
text string

The statement source.

Source:
Returns:

Declared binding names.

Type
Array:.<string:>

(static) parseModule(source, file) → {ModuleRecord}

Reads a module's import/export structure.

Parameters:
Name Type Description
source string

The module source.

file string

Path or virtual id, used in diagnostics.

Source:
Throws:

When a declaration cannot be read.

Type
ModuleParseError
Returns:

The module's structure.

Type
ModuleRecord

(inner) parseNamedClause(inner) → {Array:.<{imported:: string:, local:: string:}>}

Splits a { a, b as c } clause into binding pairs.

Parameters:
Name Type Description
inner string

The text between the braces.

Source:
Returns:

The bindings, in order.

Type
Array:.<{imported:: string:, local:: string:}>

(inner) readExport(text, mask, start, file, readSpecifier) → {object}

Reads one export declaration.

Parameters:
Name Type Description
text string

Comment-blanked source.

mask CodeMask

Mask for the source.

start number

Offset of the export keyword.

file string

Module path, for diagnostics.

readSpecifier function

Reads a from tail.

Source:
Throws:

When the declaration is malformed.

Type
ModuleParseError
Returns:

What the declaration exports and where it ends.

Type
object

(inner) readImport(text, mask, start, file) → {object|null}

Reads one import declaration.

Parameters:
Name Type Description
text string

Comment-blanked source.

mask CodeMask

Mask for the source.

start number

Offset of the import keyword.

file string

Module path, for diagnostics.

Source:
Throws:

When the declaration is malformed.

Type
ModuleParseError
Returns:

The import record, or null when this is import(.

Type
object | null

(inner) readModule(source, file) → {ModuleRecord}

Reads a module's structure, without the memo.

Parameters:
Name Type Description
source string

The module source.

file string

Path or virtual id, used in diagnostics.

Source:
Throws:

When a declaration cannot be read.

Type
ModuleParseError
Returns:

The module's structure.

Type
ModuleRecord

Type Definitions

ModuleRecord

The structure of one ES module.

Type:
  • object
Properties:
Name Type Description
source string

The original source text.

file string

Path or virtual id, for diagnostics.

imports Array:.<object:>

Import declarations, in source order.

exports Array:.<object:>

Names this module exports from its own scope.

reExports Array:.<object:>

export { x } from 'm' entries.

starReExports Array:.<object:>

export * from 'm' entries.

statements Array:.<object:>

Top-level statements with their spans.

dynamicImports Array:.<object:>

import('…') expressions, with spans.

dependencies Array:.<string:>

Distinct specifiers this module needs.

Source: