• Dart 1 Web 应用迁移到 Dart 2
    • 工具
    • 代码
      • Pubspec
      • 带脚本元素的 HTML
    • 额外资源

    Dart 1 Web 应用迁移到 Dart 2

    本文将向你阐述迁移 Dart 1.x Web 应用到 Dart 2 的相关信息。这些迁移变化是必须的因为:

    • 相关工具变更:

      • Chrome 取代了 Dartium 和 content-shell。

      • 新的编译系统取代了 pub buildpub serve 以及 pub 变换器。

    • Dart 2 语言和库变化。

    你也可以查阅:Angular 迁移指南从 v4 到 v5

    工具

    Dart 2 的 Web 应用开发环境与 Dart 1.x 不同。以下是重点:

    Dart 1.xDart 2
    Dartium, content shellChrome and dartdevc
    Dartium, content shellChrome 和 dartdevc
    pub buildwebdev build
    pub servewebdev serve
    pub run angular_testpub run build_runner test — -p chrome. See: Running tests
    pub run angular_testpub run build_runner test — -p chrome. See: Running tests
    pub transformersbuild package transformers. See: Transforming code
    pub transformersbuild package transformers. See: Transforming code

    代码

    为了迁移到 Dart 2,你需要编辑你 Web 应用项目的一些文件:

    • pubspec.yaml, see details below.

    pubspec.yaml, see details below.

    • HTML files with <script src="foo.dart"…> elements,such as web/index.html. See details below.

    HTML files with <script src="foo.dart"…> elements,such as web/index.html. See details below.

    • Dart code, due to changes in the Dart language and libraries.

    Dart code, due to changes in the Dart language and libraries.

    你可以对比 4.xmaster 分支的任意 Angular 示例 应用来获取完整的迁移示例,比如这些:

    • 快速上手

    • 英雄之路,第五章

    Pubspec

    修改你 pubspec.yaml 文件的这些部分:

    • Add new dev_dependencies:

    • dev_dependencies 添加新的节点:

      • build_runner:
      • build_test: ,如果你正在运行测试

      • build_web_compilers:

    • 删除 dev_dependencies 中的下列节点:

      • browser
      • dart_to_js_script_rewriter
    • 更新到 测试 版本 0.12.30 或更高;其会默认运行 Chrome 测试。

    • 删除所有的 转换器:

      • angular
      • dart_to_js_script_rewriter
      • test/pub_serve

    例如,angular-examples/quickstart/pubspec.yaml 是一个应用了这些差异修改的示例。

    带脚本元素的 HTML

    web/index.html 文件是一个使用了 <script> 元素的最常见栗子。你需要作出以下修改:

    • 删除 <script defer src="packages/browser/dart.js"></script>

    • Replace <script defer src="foo.dart" type="application/dart"></script> by<script defer src="foo.dart.js"></script>

    • 替换为 <script defer src="foo.dart" type="application/dart"></script><script defer src="foo.dart.js"></script>

    angular-examples/quickstart/web/index.html 中向你展示了如何应用这些更改。

    额外资源

    • Dart 2 更新:关于 Dart 2 的变更信息,以及如何从 Dart 1.x 迁移你的代码。

    • 变更日志:列出对本网站文档和示例所做的更改。