dart-web-development
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesedart-web-js-interop
dart-web-js-interop
Goal
目标
Configures Dart web applications for modern JavaScript interoperability using and , and manages the build, serve, and test lifecycle using . Assumes a standard Dart web environment requiring integration with external JavaScript libraries or browser APIs.
dart:js_interoppackage:webwebdev使用和为Dart Web应用配置现代化JavaScript互操作能力,并通过管理构建、启动服务和测试生命周期。适用于需要与外部JavaScript库或浏览器API集成的标准Dart Web环境。
dart:js_interoppackage:webwebdevDecision 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 annotations with Extension Types.
@JS - Local Development: If the user is actively developing, utilize with hot-reload capabilities.
webdev serve - 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
评估用户当前目标以确定合适的操作路径:
- 项目初始化: 如果项目缺少Web构建工具,进入依赖配置流程。
- JS绑定创建: 如果用户需要从Dart调用JS代码,使用扩展类型(Extension Types)实现注解。
@JS - 本地开发: 如果用户正处于活跃开发阶段,使用支持热重载能力的。
webdev serve - 生产部署: 如果用户正在准备发布版本,使用。
webdev build - 测试: 如果用户需要验证Web组件,使用。
build_runner test
Instructions
操作指引
-
Configure Web Dependencies Ensure the project has the required development and web dependencies. Modifyto include
pubspec.yaml,build_runner, andbuild_web_compilers.package:webyamldependencies: 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 requiredRunto resolve dependencies. Validate-and-Fix: If dependency resolution fails, verify SDK constraints indart pub getsupport Dart 3.3+ (required for modern JS interop).pubspec.yaml -
Implement JavaScript Interoperability Define JavaScript boundaries usingand Extension Types. Do not use legacy
dart:js_interopor older interop libraries.dart:htmldartimport '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:
webdevbash# 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.bash
# 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, ensureis correctly listed inbuild_web_compilers.dev_dependencies -
Run Web Tests Execute component or unit tests in a browser environment.bash
# Run tests on the Chrome platform dart run build_runner test -- -p chrome
-
配置Web依赖 确保项目包含所需的开发依赖和Web依赖。修改引入
pubspec.yaml、build_runner和build_web_compilers。package:webyamldependencies: 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 required运行解析依赖。 验证与修复: 如果依赖解析失败,检查dart pub get中的SDK约束是否支持Dart 3.3+(现代化JS互操作的必要条件)。pubspec.yaml -
实现JavaScript互操作 使用和扩展类型定义JavaScript边界。不要使用旧版的
dart:js_interop或更早的互操作库。dart:htmldartimport '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(); } -
初始化本地开发服务器 请先询问用户: "你是否需要在指定端口运行开发服务器,或者启用Dart DevTools调试功能?" 如果用户需要标准开发模式,使用启动服务器:
webdevbash# 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 -
执行生产构建 编译应用用于生产部署,该操作会生成压缩后的JavaScript代码。bash
# Build the 'web' directory into the 'build' directory webdev build --output web:build验证与修复: 检查输出目录。如果构建因缺少构建器失败,确认已正确添加到build_web_compilers中。dev_dependencies -
运行Web测试 在浏览器环境中执行组件或单元测试。bash
# Run tests on the Chrome platform dart run build_runner test -- -p chrome
Constraints
约束条件
- DO NOT use ,
dart:html, ordart:js. Strictly usedart:js_utilandpackage:web.dart:js_interop - DO NOT use under any circumstances; it is unsupported in Dart web applications.
dart:mirrors - MUST use for defining complex JavaScript object bindings.
extension type - MUST annotate external JS declarations with .
@JS() - MUST convert Dart types to JS types explicitly (e.g., ) when passing arguments to JS interop functions.
'string'.toJS - Related Skills: ,
dart-migration-versioning.dart-compilation-deployment
- 禁止使用、
dart:html或dart:js,必须严格使用dart:js_util和package:web。dart:js_interop - 任何情况下都禁止使用,Dart Web应用不支持该库。
dart:mirrors - 必须使用定义复杂的JavaScript对象绑定。
extension type - 必须为外部JS声明添加注解。
@JS() - 向JS互操作函数传递参数时,必须显式将Dart类型转换为JS类型(例如)。
'string'.toJS - 相关技能:、
dart-migration-versioning。dart-compilation-deployment