compileModule method

String compileModule(
  1. String moduleName
)

Compile a single non-entry moduleName to formatted Dart source.

Use this when the Program contains multiple user-defined modules (e.g. one per file, as produced by PackageEncoder) and you want to emit each module as its own independent Dart file.

The module is compiled without a main() entry point. All classes, enums, mixins, extensions, top-level variables, and standalone functions defined in the module are emitted, with import directives reconstructed from the module's metadata.

Throws StateError when moduleName is not found in the program.

Implementation

String compileModule(String moduleName) {
  final module = program.modules.firstWhere(
    (m) => m.name == moduleName,
    orElse: () =>
        throw StateError('Module "$moduleName" not found in program'),
  );
  final lib = _buildLibrary(module, null);
  return noFormat ? _emitRaw(lib) : _format(lib);
}