Index: pkg/polymer/lib/src/build/import_inliner.dart |
diff --git a/pkg/polymer/lib/src/build/import_inliner.dart b/pkg/polymer/lib/src/build/import_inliner.dart |
index 992ae78f2fe9770fb1e67347bb4aea9d5003dbda..f565068f69110ffe2959b4bda4763ec708e4e60f 100644 |
--- a/pkg/polymer/lib/src/build/import_inliner.dart |
+++ b/pkg/polymer/lib/src/build/import_inliner.dart |
@@ -20,6 +20,7 @@ import 'package:source_maps/span.dart'; |
import 'common.dart'; |
+// TODO(sigmund): move to web_components package (dartbug.com/18037). |
class _HtmlInliner extends PolymerTransformer { |
final TransformOptions options; |
final Transform transform; |
@@ -50,7 +51,8 @@ class _HtmlInliner extends PolymerTransformer { |
changed = _extractScripts(document, docId); |
return _visitImports(document); |
}).then((importsFound) { |
- changed = changed || importsFound; |
+ bool scriptsRemoved = _removeScripts(document); |
+ changed = changed || importsFound || scriptsRemoved; |
var output = transform.primaryInput; |
if (changed) output = new Asset.fromString(docId, document.outerHtml); |
@@ -120,7 +122,8 @@ class _HtmlInliner extends PolymerTransformer { |
var type = node.attributes['type']; |
var rel = node.attributes['rel']; |
if (tag == 'style' || tag == 'script' && |
- (type == null || type == TYPE_JS || type == TYPE_DART) || |
+ (type == null || type == TYPE_JS || type == TYPE_DART_APP || |
+ type == TYPE_DART_COMPONENT) || |
tag == 'link' && (rel == 'stylesheet' || rel == 'import')) { |
// Move the node into the body, where its contents will be placed. |
doc.body.insertBefore(node, insertionPoint); |
@@ -135,7 +138,6 @@ class _HtmlInliner extends PolymerTransformer { |
new _UrlNormalizer(transform, id).visit(doc); |
return _visitImports(doc).then((_) { |
_extractScripts(doc, id); |
- _removeScripts(doc); |
// TODO(jmesserly): figure out how this is working in vulcanizer. |
// Do they produce a <body> tag with a <head> and <body> inside? |
@@ -153,23 +155,22 @@ class _HtmlInliner extends PolymerTransformer { |
}); |
} |
- /// Remove scripts from HTML imports, and remember their [AssetId]s for later |
- /// use. |
+ /// Remove "application/dart;component=1" scripts and remember their |
+ /// [AssetId]s for later use. |
/// |
/// Dartium only allows a single script tag per page, so we can't inline |
/// the script tags. Instead we remove them entirely. |
- void _removeScripts(Document doc) { |
+ bool _removeScripts(Document doc) { |
+ bool changed = false; |
for (var script in doc.querySelectorAll('script')) { |
- if (script.attributes['type'] == TYPE_DART) { |
+ if (script.attributes['type'] == TYPE_DART_COMPONENT) { |
+ changed = true; |
script.remove(); |
var src = script.attributes['src']; |
scriptIds.add(uriToAssetId(docId, src, logger, script.sourceSpan)); |
- |
- // only the first script needs to be added. |
- // others are already removed by _extractScripts |
- return; |
} |
} |
+ return changed; |
} |
/// Split inline scripts into their own files. We need to do this for dart2js |
@@ -180,23 +181,15 @@ class _HtmlInliner extends PolymerTransformer { |
bool changed = false; |
bool first = true; |
for (var script in doc.querySelectorAll('script')) { |
- if (script.attributes['type'] != TYPE_DART) continue; |
+ var type = script.attributes['type']; |
+ if (type != TYPE_DART_COMPONENT && type != TYPE_DART_APP) continue; |
// only one Dart script per document is supported in Dartium. |
- if (!first) { |
- // Remove the script. It's invalid to have more than one in Dartium. |
- script.remove(); |
- changed = true; |
- |
- // TODO(jmesserly): remove this when we are running linter. |
- logger.warning('more than one Dart script per HTML ' |
- 'document is not supported. Script will be ignored.', |
- span: script.sourceSpan); |
- continue; |
+ if (type == TYPE_DART_APP) { |
+ if (!first) logger.warning(COMPONENT_WARNING, span: script.sourceSpan); |
+ first = false; |
} |
- first = false; |
- |
var src = script.attributes['src']; |
if (src != null) continue; |
@@ -222,7 +215,7 @@ class _HtmlInliner extends PolymerTransformer { |
'${path.extension(sourceId.path).substring(1)}'; |
if (libName.startsWith('lib/')) libName = libName.substring(4); |
libName = libName.replaceAll('/', '.').replaceAll('-', '_'); |
- libName = '${sourceId.package}.$libName'; |
+ libName = '${sourceId.package}.${libName}_$count'; |
code = "library $libName;\n$code"; |
} |
@@ -258,7 +251,8 @@ class ImportInliner extends Transformer { |
new _HtmlInliner(options, transform).apply(); |
} |
-const TYPE_DART = 'application/dart'; |
+const TYPE_DART_APP = 'application/dart'; |
+const TYPE_DART_COMPONENT = 'application/dart;component=1'; |
const TYPE_JS = 'text/javascript'; |
/// Internally adjusts urls in the html that we are about to inline. |
@@ -280,11 +274,13 @@ class _UrlNormalizer extends TreeVisitor { |
}); |
if (node.localName == 'style') { |
node.text = visitCss(node.text); |
- } else if (node.localName == 'script' && |
- node.attributes['type'] == TYPE_DART) { |
+ } else if (node.localName == 'script') { |
+ var type = node.attributes['type']; |
// TODO(jmesserly): we might need to visit JS too to handle ES Harmony |
// modules. |
- node.text = visitInlineDart(node.text); |
+ if (type == TYPE_DART_APP || type == TYPE_DART_COMPONENT) { |
+ node.text = visitInlineDart(node.text); |
+ } |
} |
super.visitElement(node); |
} |
@@ -391,3 +387,10 @@ const _urlAttributes = const [ |
]; |
_getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); |
+ |
+const COMPONENT_WARNING = |
+ 'More than one Dart script per HTML document is not supported, but in the ' |
+ 'near future Dartium will execute each tag as a separate isolate. If this ' |
+ 'code is meant to load definitions that are part of the same application ' |
+ 'you should switch it to use the "application/dart;component=1" mime-type ' |
+ 'instead.'; |