Polymer + dart2js:输出太大

时间:2015-11-09 22:04:46

标签: dart polymer dart-polymer polymer-1.0

我正在使用dart2js(通过pubspec),一个简单的例子在生成的build(文件夹)中输出太大,11.4MB。

例如,我仅使用代码paper-input元素,但build folder中的内容具有各种聚合物元素。

生成小代码需要做些什么特定的事情吗?

遵循代码示例(Webstorms中的默认值):

的index.html

<!DOCTYPE html>  
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="scaffolded-by" content="https://github.com/google/stagehand">
  <title>untitled12</title>

  <!-- Add to homescreen for Chrome on Android -->
  <meta name="mobile-web-app-capable" content="yes">
  <link rel="icon" sizes="192x192" href="images/touch/chrome-touch-icon-192x192.png">

  <!-- Add to homescreen for Safari on iOS -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="Web Starter Kit">
  <link rel="apple-touch-icon-precomposed" href="apple-touch-icon-precomposed.png">

  <!-- Tile icon for Win8 (144x144 + tile color) -->
  <meta name="msapplication-TileImage" content="images/touch/ms-touch-icon-144x144-precomposed.png">
  <meta name="msapplication-TileColor" content="#3372DF">

  <!-- example of using a paper element -->
  <link rel="import" href="packages/polymer_elements/roboto.html">

  <link rel="stylesheet" href="styles.css">
</head>

<body unresolved>

  <main-app></main-app>

  <script type="application/dart" src="index.dart"></script>
  <script src="packages/browser/dart.js"></script>
</body>
</html>

main_app.html

<dom-module id="main-app">
  <style>
    :host {
      display: block;
    }
  </style>

  <template>
    <paper-input label="Type something..." value="{{text}}"></paper-input>
    <p>
      Text: <span>{{text}}</span><br />
      Reversed: <span>{{reverseText(text)}}</span>
    </p>
  </template>
</dom-module>

main_app.dart

@HtmlImport('main_app.html')
library untitled12.lib.main_app;

import 'dart:html';

import 'package:polymer_elements/paper_input.dart';
import 'package:polymer/polymer.dart';
import 'package:web_components/web_components.dart';

/// Uses [PaperInput]
@PolymerRegister('main-app')
class MainApp extends PolymerElement {
  @property
  String text;

  /// Constructor used to create instance of MainApp.
  MainApp.created() : super.created();

  @reflectable
  String reverseText(String text) {
    return text.split('').reversed.join('');
  }
}

pubspec.yaml

name: 'untitled12'
version: 0.0.1
description: A web app built using polymer.dart.
#author: Your Name <email@example.com>
#homepage: https://www.example.com

environment:
  sdk: '>=1.9.0 <2.0.0'

dependencies:
  browser: ^0.10.0
  polymer_elements: ^1.0.0-rc.1
  polymer: ^1.0.0-rc.2
  reflectable: ^0.3.1
  web_components: ^0.12.0

transformers:
- web_components:
    entry_points: web/index.html
- reflectable:
    entry_points: web/index.bootstrap.initialize.dart
- $dart2js:
    $include: '**/*.bootstrap.initialize.dart'
    minify: true
    commandLineOptions:
    - --trust-type-annotations
    - --trust-primitives

1 个答案:

答案 0 :(得分:3)

这可能是因为polymer_elements包。今天只有dart文件可以被树形图像所以所有来自该包的html和js文件都将出现在构建输出中。您的实际应用程序虽然在用户访问时不会那么大,但通常不是问题。

基本上,今天的限制因素是变换器的工作方式,只有polymer_elements包中的变换器才能在构建期间删除文件。但是,该转换器无法访问您的应用程序以确定实际使用的文件。

今天唯一真正的解决方案是拥有自己独立的构建步骤,删除不必要的文件,但这很少值得付出努力。