dart-web-development
dart-web-js-interop
Goal
Configures Dart web applications for modern JavaScript interoperability using dart:js_interop and package:web, and manages the build, serve, and test lifecycle using webdev. Assumes a standard Dart web environment requiring integration with external JavaScript libraries or browser APIs.
Decision Logic
Evaluate the user's current objective to determine the appropriate action path:
- Project Setup: If the project lacks web build tools, proceed to dependency configuration.
- JS Binding Creation: If the user needs to call JS from Dart, implement
@JSannotations with Extension Types. - Local Development: If the user is actively developing, utilize
webdev servewith hot-reload capabilities. - Production Deployment: If the user is preparing for release, utilize
webdev build. - Testing: If the user needs to validate web components, utilize
build_runner test.
Instructions
-
Configure Web Dependencies Ensure the project has the required development and web dependencies. Modify
pubspec.yamlto includebuild_runner,build_web_compilers, andpackage:web.dependencies: web: ^0.5.0 # Use latest compatible version dev_dependencies: build_runner: ^2.4.0 build_web_compilers: ^4.0.0 build_test: ^3.0.0 # If testing is requiredRun
dart pub getto resolve dependencies. Validate-and-Fix: If dependency resolution fails, verify SDK constraints inpubspec.yamlsupport Dart 3.3+ (required for modern JS interop). -
Implement JavaScript Interoperability Define JavaScript boundaries using
dart:js_interopand Extension Types. Do not use legacydart:htmlor older interop libraries.import 'dart:js_interop'; import 'package:web/web.dart' as web; // Bind to a global JavaScript function ('console.log') external void logToConsole(JSAny? obj); // Bind to a JavaScript object/class using Extension Types ('MyJSClass') extension type MyDartWrapper._(JSObject _) implements JSObject { external MyDartWrapper(JSString name); external JSString get name; external set name(JSString value); external void doSomething(); } void main() { // Example usage interacting with the DOM via package:web final div = web.document.createElement('div') as web.HTMLDivElement; div.text = 'Hello from Dart!'; web.document.body?.append(div); // Example usage of custom JS interop final myObj = MyDartWrapper('Test'.toJS); myObj.doSomething(); } -
Initialize Local Development Server STOP AND ASK THE USER: "Do you need to run the development server on a specific port, or enable Dart DevTools debugging?" If standard development is requested, start the server using
webdev:# Standard serve (defaults to localhost:8080) webdev serve # Serve with Dart DevTools enabled webdev serve --debug # Serve on a custom port webdev serve web:8083 -
Execute Production Build Compile the application for production deployment. This generates minified JavaScript.
# Build the 'web' directory into the 'build' directory webdev build --output web:buildValidate-and-Fix: Inspect the output directory. If the build fails due to missing builders, ensure
build_web_compilersis correctly listed indev_dependencies. -
Run Web Tests Execute component or unit tests in a browser environment.
# Run tests on the Chrome platform dart run build_runner test -- -p chrome
Constraints
- DO NOT use
dart:html,dart:js, ordart:js_util. Strictly usepackage:webanddart:js_interop. - DO NOT use
dart:mirrorsunder any circumstances; it is unsupported in Dart web applications. - MUST use
extension typefor defining complex JavaScript object bindings. - MUST annotate external JS declarations with
@JS(). - MUST convert Dart types to JS types explicitly (e.g.,
'string'.toJS) when passing arguments to JS interop functions. - Related Skills:
dart-migration-versioning,dart-compilation-deployment.