Skip to content

[web_generator] Setting up web_generator for Dart JS Interop Gen #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/workflows/dart.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Created with package:mono_repo v6.6.2
# Created with package:mono_repo v6.6.3
name: Dart CI
on:
push:
Expand Down Expand Up @@ -36,7 +36,7 @@ jobs:
name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
- name: mono_repo self validate
run: dart pub global activate mono_repo 6.6.2
run: dart pub global activate mono_repo 6.6.3
- name: mono_repo self validate
run: dart pub global run mono_repo generate --validate
job_002:
Expand Down Expand Up @@ -340,7 +340,7 @@ jobs:
- job_004
- job_005
job_010:
name: "generate_and_analyze; Dart dev; PKG: web_generator; `dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web`"
name: "generate_and_analyze; Dart dev; PKG: web_generator; `dart pub -C ../web get && dart bin/update_idl_bindings.dart && dart analyze --fatal-infos ../web`"
runs-on: ubuntu-latest
steps:
- name: Cache Pub hosted dependencies
Expand All @@ -365,8 +365,8 @@ jobs:
run: dart pub upgrade
if: "always() && steps.checkout.conclusion == 'success'"
working-directory: web_generator
- name: "web_generator; dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web"
run: "dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web"
- name: "web_generator; dart pub -C ../web get && dart bin/update_idl_bindings.dart && dart analyze --fatal-infos ../web"
run: "dart pub -C ../web get && dart bin/update_idl_bindings.dart && dart analyze --fatal-infos ../web"
if: "always() && steps.web_generator_pub_upgrade.conclusion == 'success'"
working-directory: web_generator
needs:
Expand All @@ -380,7 +380,7 @@ jobs:
- job_008
- job_009
job_011:
name: "generate_all_and_analyze; Dart dev; PKG: web_generator; `dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web`"
name: "generate_all_and_analyze; Dart dev; PKG: web_generator; `dart pub -C ../web get && dart bin/update_idl_bindings.dart --generate-all && dart analyze --fatal-infos ../web`"
runs-on: ubuntu-latest
steps:
- name: Cache Pub hosted dependencies
Expand All @@ -405,8 +405,8 @@ jobs:
run: dart pub upgrade
if: "always() && steps.checkout.conclusion == 'success'"
working-directory: web_generator
- name: "web_generator; dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web"
run: "dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web"
- name: "web_generator; dart pub -C ../web get && dart bin/update_idl_bindings.dart --generate-all && dart analyze --fatal-infos ../web"
run: "dart pub -C ../web get && dart bin/update_idl_bindings.dart --generate-all && dart analyze --fatal-infos ../web"
if: "always() && steps.web_generator_pub_upgrade.conclusion == 'success'"
working-directory: web_generator
needs:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pubspec.lock

web_generator/lib/src/*.js
web_generator/lib/src/*.js.*
web_generator/lib/src/node_modules/
web_generator/lib/src/node_modules/
10 changes: 5 additions & 5 deletions tool/ci.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Created with package:mono_repo v6.6.2
# Created with package:mono_repo v6.6.3

# Support built in commands on windows out of the box.

Expand Down Expand Up @@ -72,12 +72,12 @@ for PKG in ${PKGS}; do
dart fix --compare-to-golden test_fixes || EXIT_CODE=$?
;;
command_1)
echo 'dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web'
dart pub -C ../web get && dart bin/update_bindings.dart && dart analyze --fatal-infos ../web || EXIT_CODE=$?
echo 'dart pub -C ../web get && dart bin/update_idl_bindings.dart && dart analyze --fatal-infos ../web'
dart pub -C ../web get && dart bin/update_idl_bindings.dart && dart analyze --fatal-infos ../web || EXIT_CODE=$?
;;
command_2)
echo 'dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web'
dart pub -C ../web get && dart bin/update_bindings.dart --generate-all && dart analyze --fatal-infos ../web || EXIT_CODE=$?
echo 'dart pub -C ../web get && dart bin/update_idl_bindings.dart --generate-all && dart analyze --fatal-infos ../web'
dart pub -C ../web get && dart bin/update_idl_bindings.dart --generate-all && dart analyze --fatal-infos ../web || EXIT_CODE=$?
;;
format)
echo 'dart format --output=none --set-exit-if-changed .'
Expand Down
14 changes: 7 additions & 7 deletions web_generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ run on Node.
To regenerate `web` bindings from the current IDL versions, run:

```shell
dart bin/update_bindings.dart
dart bin/update_idl_bindings.dart
```

## Update to the latest Web IDL versions and regenerate

To re-generate the package from newer IDL versions, you can either run:

```shell
dart bin/update_bindings.dart --update
dart bin/update_idl_bindings.dart --update
```

or, manually edit `lib/src/package.json` to use specific IDL versions, and
re-run `update_bindings.dart`.
re-run `update_idl_bindings.dart`.

### Updating the dartdoc info from MDN

Expand All @@ -38,7 +38,7 @@ 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_bindings.dart` to produce Dart code using the updated documentation.
`update_idl_bindings.dart` to produce Dart code using the updated documentation.

## Generation conventions

Expand Down Expand Up @@ -80,7 +80,7 @@ definitions:
To ignore the compatibility data and emit all members, run:

```shell
dart bin/update_bindings.dart --generate-all
dart bin/update_idl_bindings.dart --generate-all
```

This is useful if you want to avoid having to write bindings manually for some
Expand All @@ -90,10 +90,10 @@ experimental and non-standard APIs.

Based on:

<!-- START updated by bin/update_bindings.dart. Do not modify by hand -->
<!-- START updated by bin/update_idl_bindings.dart. Do not modify by hand -->
| Item | Version |
| --- | --: |
| `@webref/css` | [6.20.3](https://www.npmjs.com/package/@webref/css/v/6.20.3) |
| `@webref/elements` | [2.4.0](https://www.npmjs.com/package/@webref/elements/v/2.4.0) |
| `@webref/idl` | [3.60.1](https://www.npmjs.com/package/@webref/idl/v/3.60.1) |
<!-- END updated by bin/update_bindings.dart. Do not modify by hand -->
<!-- END updated by bin/update_idl_bindings.dart. Do not modify by hand -->
100 changes: 100 additions & 0 deletions web_generator/bin/gen_interop_bindings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:io/ansi.dart' as ansi;
import 'package:io/io.dart';
import 'package:path/path.dart' as p;
import 'package:web_generator/src/cli.dart';

void main(List<String> arguments) async {
final ArgResults argResult;

try {
argResult = _parser.parse(arguments);
} on FormatException catch (e) {
print('''
${ansi.lightRed.wrap(e.message)}

$_usage''');
exitCode = ExitCode.usage.code;
return;
}

if (argResult['help'] as bool) {
print(_usage);
return;
}

if (argResult.rest.isEmpty) {
print('''
${ansi.lightRed.wrap('At least one argument is needed')}

$_usage''');
exitCode = ExitCode.usage.code;
return;
}

assert(p.fromUri(Platform.script).endsWith(_thisScript.toFilePath()));

// Run `npm install` or `npm update` as needed.
final update = argResult['update'] as bool;
await runProc(
'npm',
[update ? 'update' : 'install'],
workingDirectory: bindingsGeneratorPath,
);

final contextFile = await createJsTypeSupertypeContext();

// Compute JS type supertypes for union calculation in translator.
await generateJsTypeSupertypes(contextFile.path);

if (argResult['compile'] as bool) {
// Compile Dart to Javascript.
await compileDartMain();
}

final inputFile = argResult.rest.first;
final outputFile = argResult['output'] as String? ??
p.join(p.current, inputFile.replaceAll('.d.ts', '.dart'));
final configFile =
argResult['config'] as String? ?? p.join(p.current, 'webgen.yaml');
final relativeOutputPath =
p.relative(outputFile, from: bindingsGeneratorPath);
// Run app with `node`.
await runProc(
'node',
[
'main.mjs',
'--declaration',
'--input=${p.relative(inputFile, from: bindingsGeneratorPath)}',
'--output=$relativeOutputPath',
'--config=$configFile'
],
workingDirectory: bindingsGeneratorPath,
);

await contextFile.delete();
return;
}

final _thisScript = Uri.parse('bin/gen_interop_bindings.dart');

final _usage = '''
${ansi.styleBold.wrap('Dart Interop Gen')}:
$_thisScript dts <.d.ts file> [options]

Usage:
${_parser.usage}''';

final _parser = ArgParser()
..addFlag('help', negatable: false, help: 'Show help information')
..addFlag('update', abbr: 'u', help: 'Update npm dependencies')
..addFlag('compile', defaultsTo: true)
..addOption('output',
abbr: 'o', help: 'The output path to generate the Dart interface code')
..addOption('config',
hide: true,
abbr: 'c',
help: 'The configuration file to use for this tool (NOTE: Unimplemented)')
..addFlag('help', negatable: false);
Loading