jsModuleMainLibrary top-level property

ConfigVariable<String?> jsModuleMainLibrary
final

The path to a Dart library whose main() method will be called when the compiled JavaScript module is loaded.

All the package's executables are compiled into a single JS file. The main() method of the given library will be invoked when that JS file is first imported. It should take no arguments.

This is most commonly used to export functions so that the npm package is usable as a JS library. For example, to export a sayHello() function, this library might look like:

import 'package:js/js.dart';

@JS()
class _Exports {
  external set sayHello(function);
}

@JS()
external get _Exports exports;

void main() {
  exports.sayHello = allowInterop(() => print("Hello, world!"));
}

This path is relative to the root of the package. It defaults to null, which means no user-defined code will run when the module is loaded.

Implementation

final jsModuleMainLibrary = InternalConfigVariable.value<String?>(null);