https://github.com/morganney/dartscript
Creates a JavaScript module from a Dart function.
https://github.com/morganney/dartscript
commonjs dart esmodule function javascript
Last synced: 8 months ago
JSON representation
Creates a JavaScript module from a Dart function.
- Host: GitHub
- URL: https://github.com/morganney/dartscript
- Owner: morganney
- License: mit
- Created: 2024-05-23T03:31:40.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-05T13:52:32.000Z (over 1 year ago)
- Last Synced: 2025-06-06T17:49:38.380Z (8 months ago)
- Topics: commonjs, dart, esmodule, function, javascript
- Language: JavaScript
- Homepage:
- Size: 110 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## [`dartscript`](https://www.npmjs.com/package/dartscript)

[](https://www.npmjs.com/package/dartscript)
Simple CLI utility for creacting a JavaScript module of a [Dart function](https://dart.dev/language/functions).
## Requirements
You need to have the [Dart SDK installed](https://dart.dev/get-dart#choose-an-installation-option).
You need to wrap the function you want converted with [`allowInterop`](https://pub.dev/packages/js#making-a-dart-function-callable-from-javascript) from the [`dart:js`](https://api.dart.dev/stable/3.4.1/dart-js/dart-js-library.html) library within [`main()`](https://dart.dev/language/functions#the-main-function). This implies you need to `import package:js/js.dart` from the dart file passed to `dartscript`, and so will need to list it within your `pubspec.yaml` file:
```yaml
dependencies:
js: ^0.7.1
```
## Example
Given a file with a Dart function (that possibly calls another function)
**file.dart**
```dart
import 'package:js/js.dart';
@JS('greeting')
String greeting(String name) => '$name, hello from Dart!';
@JS('say')
String say(String from, String msg, [bool? greet]) {
var result = '$from says $msg';
if (greet != null && greet) {
result = greeting('Welcome');
}
return result;
}
void main() {
allowInterop(say);
}
```
Pass it to `dartscript` providing the function name to convert to a JS module
```
dartscript --func say --out say.js file.dart
```
**say.js**
```js
export function say(from, msg, greet) {
var result = from + ' says ' + msg
return greet === true ? 'Welcome, hello from Dart!' : result
}
```
## Options
You pass the name of the input Dart file as a positional.
- `--func` The name of the function from the Dart build to extract (required)
- `--out` The name of the build file for `dartscript` to produce (defaults to `func.js`)
- `--module` The module system type, `es | cjs` (defaults to `es`)
- `--default` Whether to export the function using a default export for the module system (defaults to `false`)
You can run `dartscript --help`:
```console
Usage: dartscript [options]
Options:
--func, -f [string] The name of the dart function to extract into a module. Required.
--out, -o [path] Where to save the output file. Defaults to func.js.
--module, -m What module system to use. Defaults to es. [es | cjs].
--default, -d Whether to use a default export. Defaults to named export.
--help, -h Print this message.
```