Tools to generate Dart interfaces from TypeScript declaration files and Web IDL definitions.
This package is used to generate web from Web IDL definitions and MDN API documentation.
The tools to generate bindings are written in Dart, compiled to JavaScript, and run on Node.
There are two entrypoints present in this package:
gen_interop_bindings.dart: This entrypoint is for generating Dart interfaces from TS Declaration code, given the path to a .d.ts fileupdate_idl_bindings.dart: This entrypoint is for generating Dart interfaces from Web IDL definitions, given the path to a .idl file. If no idl file is present, it, by default, generates code for package:web.The web_generator requires Node.js v22 or newer. Older Node versions are not supported and may result in runtime failures.
To generate Dart interfaces for a given .d.ts file, run the following at the root of this package:
dart bin/gen_interop_bindings.dart <input.d.ts> dart bin/gen_interop_bindings.dart -o <output> <input>
If multiple files are passed, the output option is regarded as an output directory instead.
For more information on the command-line options you can pass alongside, you can check the help information
dart bin/gen_interop_bindings.dart --help
The generator also has support for configurating the output of the generator, allowing support for configuring features like: variardic argument count, preamble (if any), TS Compiler Options, etc.
These configuration options can either be passed from the command line, or via a YAML configuration file. To pass a configuration file, pass the --config option.
Given a sample configuration file:
input: a.d.ts output: b.d.ts ts-config-file: tsconfig.json
The following are equivalent
dart bin/gen_interop_bindings.dart -o b.d.ts --ts-config tsconfig.json a.d.ts dart bin/gen_interop_bindings.dart --config config.yaml
Note that not all configuration options are direct mappings between the CLI and the configuration file.
| Option | Description | Example |
|---|---|---|
name | The name of the bindings | name: MyBindings |
description | A description of the bindings (optional) | description: My awesome bindings |
preamble | Preamble text to insert before the bindings (optional) | preamble: | // Generated. Do not edit. |
input | A file (single string) or set of files (array of strings) passed into the generator | input: bindings.d.ts or input: - bindings.d.ts |
output | The output file or directory to write the bindings to | output: lib/src/js |
include | Declarations to include in the generated output (as a list). Can either be passed as a raw string to match the full name, or as a regular expression. By default, the generator outputs all exported declarations | include: - myNumber |
language_version | The Dart Language Version to use, usually for formatting (optional) | language_version: 3.6.0 |
ts_config | An object consisting of TS Configurations following the tsconfig.json file schema used for configuring the TypeScript Program/Compiler (optional) | ts_config: compilerOptions: target: es2020 |
ts_config_file | The TS Configuration file (tsconfig.json) if any (optional) | ts_config_file: tsconfig.json |
generate_all | Include generating declarations for code that isn't exported. Defaults to false | generate_all: true |
ignore_errors | Ignore source code warnings and errors (they will still be printed). Defaults to false | ignore_errors: true |
functions.varargs | The number of arguments that variable-argument functions should take. Defaults to 4 | functions: varargs: 6 |
The generator scripts use a number of conventions to consistently handle TS definitions:
external declarations annotated with @JS.externalJSObjectimplements between extension typescall declarations, construct signatures, getters and setters.@redeclare.var declarations.JSObject with static members.var declarations, and functions.js_interop types.never type returned from a const declaration, function, readonly property or method has the declaration annotated with @doNotStore.package:web typesnull or undefined equals neverJSFunction overriding the call method. This allows for a stronger type signature than just JSFunction. While closures leave their calls as external, construct signatures just redirect to the class constructor.JSAny by default.typeof declarations are references to the type of the declaration, which are:keyof correctly).JSFunctionkeyof declarations are represented as unions of the keys for the operand type.@deprecated and @experimentalTo generate Dart interfaces for a given .idl file, run the following at the root of this package:
dart bin/update_idl_bindings.dart
If multiple files are passed, the output option is regarded as an output directory instead.
To regenerate web bindings from the current IDL versions, run the entrypoint without any arguments:
dart bin/update_idl_bindings.dart
To re-generate the package from newer IDL versions, you can either run:
dart bin/update_idl_bindings.dart --update
or, manually edit lib/src/package.json to use specific IDL versions, and re-run update_idl_bindings.dart.
package:web's dartdoc comments come from the MDN Web Docs project. In order to update to the latest version of the documentation, run:
dart bin/scrape_mdn.dart
That will collect the MDN documentation into third_party/mdn/mdn.json; changes to that file should be committed to git. You'll need to run update_idl_bindings.dart to produce Dart code using the updated documentation.
The generator scripts use a number of conventions to consistently handle Web IDL definitions:
JSObject.implements between extension types.JSArray and JSPromise.String.JSFunction.String instead of JSString.JSAny?.JSObject.To ignore the compatibility data and emit all members, run:
dart bin/update_idl_bindings.dart --generate-all
This is useful if you want to avoid having to write bindings manually for some experimental and non-standard APIs.
Based on:
| Item | Version |
|---|---|
@webref/css | 6.20.3 |
@webref/elements | 2.4.0 |
@webref/idl | 3.60.1 |