【问题标题】:What is the /packages/$sdk/ folder inside a Google Dart build for?Google Dart 构建中的 /packages/$sdk/ 文件夹是做什么用的?
【发布时间】:2014-11-07 11:59:45
【问题描述】:

在 Dart 编辑器(Pub Build (Generates JS) 选项)中构建 Google Dart 网络应用程序后,文件夹布局类似于 this。我的应用程序同时导入了dart:htmldart:async,但似乎我可以将除$sdk 文件夹之外的所有内容上传到我的服务器,并且该应用程序在Dartium 和其他浏览器中都可以正常运行。有什么原因我需要上传$sdk 文件夹,它有什么用?尝试谷歌搜索,但我没有看到任何答案,提前谢谢!

编辑:Here's a working example of my project from DartEditor 1.5.8 以下是它的代码。

hexclock.dart:

import 'dart:html';
import 'dart:async';
DateTime theTime;
String theHour, theMinute, theSecond;
Timer theTimer;
void main() {
  theTimer = new Timer.periodic(new Duration(seconds: 1), getTime);
}

void updateColours(){
  String bgcol = "#" + theHour + theMinute + theSecond;
  querySelector("body").style.backgroundColor = bgcol;
}

void printTime() {
  querySelector("#hour").text = theHour;
  querySelector("#minute").text = theMinute;
  querySelector("#second").text = theSecond;
}

void getTimeInit(){
  theTime = new DateTime.now();
  theHour = "${checkZero(theTime.hour)}";
  theMinute = "${checkZero(theTime.minute)}";
  theSecond = "${checkZero(theTime.second)}";
  printTime();
  updateColours();
}

void getTime(Timer t){
  theTime = new DateTime.now();
  theHour = "${checkZero(theTime.hour)}";
  theMinute = "${checkZero(theTime.minute)}";
  theSecond = "${checkZero(theTime.second)}";
  querySelector("#title").style.opacity = "0";
  querySelector("#clock").style.opacity = "1";
  printTime();
  updateColours();
}

String checkZero(int anInt){
  if (anInt < 10)
    return "0${anInt}";
  else
    return "${anInt}";
}

hexclock.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>HexClock</title>
    <script async type="application/dart" src="hexclock.dart"></script>
    <script async src="packages/browser/dart.js"></script>
    <link href='http://fonts.googleapis.com/css?family=Droid+Sans+Mono' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="hexclock.css">
  </head>
  <body> 
    <div id="title" class="centre">hexclock</div>
    <div id="clock" class="centre">#<span id="hour">&nbsp;&nbsp;</span><span id="minute">&nbsp;&nbsp;</span><span id="second">&nbsp;&nbsp;</span></div>
  </body>
</html>

pubspec.yaml:

name: HexClock
description: A sample web application
dependencies:
  browser: any

【问题讨论】:

  • 我没有这个文件夹。你用的是什么 Dart 版本?
  • 我有 Dart Editor 1.5.8

标签: dart dart-editor dart2js dartium


【解决方案1】:

pub source code我发现了这个:

  // "$sdk" is a pseudo-package that allows the dart2js transformer to find
  // the Dart core libraries without hitting the file system directly. This
  // ensures they work with source maps.
  var libPath = path.join(sdk.rootDirectory, "lib");
  var sdkSources = listDir(libPath, recursive: true)
      .where((file) => path.extension(file) == ".dart")
      .map((file) {
    var idPath = path.join("lib",
        path.relative(file, from: sdk.rootDirectory));
    return new AssetId('\$sdk', path.toUri(idPath).toString());
  });

我不知道这是否与您所看到的完全匹配,但我猜它是相关的。这在理论上甚至可以通过 SDK 函数进行客户端调试,这可能非常有用。

【讨论】:

    【解决方案2】:

    基本上整个构建文件夹都将被部署。
    有一些冗余文件,例如 *.dart.pecompiled.js,仅在 CSP 环境(例如 Chrome 应用程序)中是必需的。
    我认为最近发生了变化,因此不再生成这些文件。

    可能还有其他多余的东西,但我还没有看到任何关于它的信息。

    您可以尝试使用 Dart 1.6 版本(稳定版应该已经发布)并检查它是否已被删除或者是否必须有其他解释。
    我总是使用目前 1.7+ 的bleeding_edge。

    另一种解释是,这仅在您以debug 模式构建时创建。
    DartEditor 默认执行此操作。

    从命令行尝试pub build,看看是否仍然生成。

    【讨论】:

    • 好的,我明白了。我已经更新到 1.6,现在当我从 Dart 编辑器运行 build 时,/packages/ 中的 /$sdk/ 文件夹仍然出现,但 /browser/ 文件夹没有出现(并且构建的应用程序不起作用!)。当我从命令行运行pub build 时,/packages/ 文件夹根本不会出现。浏览器在依赖文件中,所以我不知道这里发生了什么。
    • 你说这是一个简单的例子。你能把代码贴出来(index.htmlindex.dartpubspec.yaml),让我们看看。
    • 也许您正在寻找的(浏览器目录)只是内​​联到 *.html*.dart 文件中。您是否尝试过在浏览器中打开入口页面?我没有对构建输出进行大量调查,但通常如果它在从 DartEditor 构建时运行,它也会在从命令行构建时运行(只要你不使用反射/镜像 ;-))
    • 您的pubspec.yaml 依赖项中有browser 包吗?也许这是之前的传递依赖,现在不再是。您还可以尝试使用参数 --allow-file-access-from-files 启动 Chrome
    • 太棒了!这不应该是必要的,但事实上它是必要的。感谢您的反馈。
    猜你喜欢
    • 2014-02-22
    • 2018-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 2020-04-14
    • 2018-07-02
    相关资源
    最近更新 更多