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 40d5ca58d197a51ebea3106ccf5fb457baad3470..07be8abff744d2020b0fa126d78d9f32b361d39f 100644 |
--- a/pkg/polymer/lib/src/build/import_inliner.dart |
+++ b/pkg/polymer/lib/src/build/import_inliner.dart |
@@ -8,14 +8,15 @@ library polymer.src.build.import_inliner; |
import 'dart:async'; |
import 'dart:convert'; |
+import 'package:analyzer/src/generated/ast.dart'; |
import 'package:barback/barback.dart'; |
import 'package:path/path.dart' as path; |
import 'package:html5lib/dom.dart' show |
Document, DocumentFragment, Element, Node; |
import 'package:html5lib/dom_parsing.dart' show TreeVisitor; |
+import 'package:source_maps/refactor.dart' show TextEditTransaction; |
import 'package:source_maps/span.dart'; |
-import 'code_extractor.dart'; // import just for documentation. |
import 'common.dart'; |
class _HtmlInliner extends PolymerTransformer { |
@@ -26,9 +27,6 @@ class _HtmlInliner extends PolymerTransformer { |
final seen = new Set<AssetId>(); |
final scriptIds = <AssetId>[]; |
- static const TYPE_DART = 'application/dart'; |
- static const TYPE_JS = 'text/javascript'; |
- |
_HtmlInliner(this.options, Transform transform) |
: transform = transform, |
logger = transform.logger, |
@@ -38,21 +36,26 @@ class _HtmlInliner extends PolymerTransformer { |
seen.add(docId); |
Document document; |
+ bool changed; |
- return readPrimaryAsHtml(transform).then((document) => |
- _visitImports(document, docId).then((importsFound) { |
+ return readPrimaryAsHtml(transform).then((doc) { |
+ document = doc; |
+ // Add the main script's ID, or null if none is present. |
+ // This will be used by ScriptCompactor. |
+ changed = _extractScripts(document); |
+ return _visitImports(document, docId); |
+ }).then((importsFound) { |
+ changed = changed || importsFound; |
var output = transform.primaryInput; |
- if (importsFound) { |
- output = new Asset.fromString(docId, document.outerHtml); |
- } |
+ if (changed) output = new Asset.fromString(docId, document.outerHtml); |
transform.addOutput(output); |
// We produce a secondary asset with extra information for later phases. |
transform.addOutput(new Asset.fromString( |
docId.addExtension('.scriptUrls'), |
JSON.encode(scriptIds, toEncodable: (id) => id.serialize()))); |
- })); |
+ }); |
} |
/// Visits imports in [document] and add the imported documents to documents. |
@@ -107,7 +110,7 @@ class _HtmlInliner extends PolymerTransformer { |
var insertionPoint = doc.body.firstChild; |
for (var node in doc.head.nodes.toList(growable: false)) { |
if (node is! Element) continue; |
- var tag = node.tagName; |
+ var tag = node.localName; |
var type = node.attributes['type']; |
var rel = node.attributes['rel']; |
if (tag == 'style' || tag == 'script' && |
@@ -125,7 +128,7 @@ class _HtmlInliner extends PolymerTransformer { |
readAsHtml(id, transform).then((doc) => _visitImports(doc, id).then((_) { |
new _UrlNormalizer(transform, id).visit(doc); |
- _extractScripts(doc); |
+ _extractScripts(doc, htmlImport: true); |
// TODO(jmesserly): figure out how this is working in vulcanizer. |
// Do they produce a <body> tag with a <head> and <body> inside? |
@@ -136,21 +139,31 @@ class _HtmlInliner extends PolymerTransformer { |
Future _inlineStylesheet(AssetId id, Element link) { |
return transform.readInputAsString(id).then((css) { |
- var url = spanUrlFor(id, transform); |
- css = new _UrlNormalizer(transform, id).visitCss(css, url); |
+ css = new _UrlNormalizer(transform, id).visitCss(css); |
link.replaceWith(new Element.tag('style')..text = css); |
}); |
} |
- /// Split Dart script tags from all the other elements. Now that Dartium |
- /// only allows a single script tag per page, we can't inline script |
- /// tags. Instead, we collect the urls of each script tag so we import |
- /// them directly from the Dart bootstrap code. |
- void _extractScripts(Document document) { |
+ /// Split Dart script tags from all the other elements. |
+ /// |
+ /// For inline scripts, we extract into a .dart file |
+ /// |
+ /// Dartium only allows a single script tag per page, we can't inline script |
+ /// tags. In [htmlImport]ed documents, we remove the script tag entirely so |
+ /// we can inline the HTML. |
+ /// |
+ /// However we leave in place the script tag in the entry point HTML file, |
+ /// so we run it at the correct point in time. |
+ bool _extractScripts(Document document, {bool htmlImport: false}) { |
Jennifer Messerly
2014/02/26 02:43:02
Meshing together these two functions didn't work q
|
+ bool changed = false; |
bool first = true; |
+ int count = 0; |
for (var script in document.querySelectorAll('script')) { |
if (script.attributes['type'] == TYPE_DART) { |
- script.remove(); |
+ if (htmlImport) { |
+ script.remove(); |
+ changed = true; |
+ } |
// only one Dart script per document is supported in Dartium. |
if (first) { |
@@ -158,14 +171,31 @@ class _HtmlInliner extends PolymerTransformer { |
var src = script.attributes['src']; |
if (src == null) { |
- logger.warning('unexpected script without a src url. The ' |
- 'ImportInliner transformer should run after running the ' |
- 'InlineCodeExtractor', span: script.sourceSpan); |
- continue; |
+ var filename = path.url.basename(docId.path); |
+ var code = script.text; |
+ // TODO(sigmund): ensure this path is unique (dartbug.com/12618). |
+ script.attributes['src'] = src = '$filename.$count.dart'; |
+ script.text = ''; |
+ changed = true; |
+ |
+ var newId = docId.addExtension('.$count.dart'); |
+ if (!_hasLibraryDirective(code)) { |
+ var libname = path.withoutExtension(newId.path) |
+ .replaceAll(new RegExp('[-./]'), '_'); |
+ code = "library $libname;\n$code"; |
+ } |
+ transform.addOutput(new Asset.fromString(newId, code)); |
} |
- scriptIds.add(resolve(docId, src, logger, script.sourceSpan)); |
+ if (htmlImport) { |
+ scriptIds.add(resolve(docId, src, logger, script.sourceSpan)); |
+ } |
} else { |
+ // Remove the script (if we didn't already). |
+ // 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.', |
@@ -173,9 +203,15 @@ class _HtmlInliner extends PolymerTransformer { |
} |
} |
} |
+ return changed; |
} |
} |
+/// Parse [code] and determine whether it has a library directive. |
+bool _hasLibraryDirective(String code) => |
+ parseCompilationUnit(code).directives.any((d) => d is LibraryDirective); |
+ |
+ |
/// Recursively inlines the contents of HTML imports. Produces as output a |
/// single HTML file that inlines the polymer-element definitions, and a text |
/// file that contains, in order, the URIs to each library that sourced in a |
@@ -197,6 +233,8 @@ class ImportInliner extends Transformer { |
new _HtmlInliner(options, transform).apply(); |
} |
+const TYPE_DART = 'application/dart'; |
+const TYPE_JS = 'text/javascript'; |
/// Internally adjusts urls in the html that we are about to inline. |
class _UrlNormalizer extends TreeVisitor { |
@@ -208,13 +246,20 @@ class _UrlNormalizer extends TreeVisitor { |
_UrlNormalizer(this.transform, this.sourceId); |
visitElement(Element node) { |
- for (var key in node.attributes.keys) { |
- if (_urlAttributes.contains(key)) { |
- var url = node.attributes[key]; |
- if (url != null && url != '' && !url.startsWith('{{')) { |
- node.attributes[key] = _newUrl(url, node.sourceSpan); |
+ node.attributes.forEach((name, value) { |
+ if (_urlAttributes.contains(name)) { |
+ if (value != '' && !value.trim().startsWith('{{')) { |
+ node.attributes[name] = _newUrl(value, node.sourceSpan); |
} |
} |
+ }); |
+ if (node.localName == 'style') { |
+ node.text = visitCss(node.text); |
+ } else if (node.localName == 'script' && |
+ node.attributes['type'] == TYPE_DART) { |
+ // TODO(jmesserly): we might need to visit JS too to handle ES Harmony |
+ // modules. |
+ node.text = visitInlineDart(node.text); |
} |
super.visitElement(node); |
} |
@@ -227,7 +272,8 @@ class _UrlNormalizer extends TreeVisitor { |
// https://github.com/Polymer/vulcanize/blob/c14f63696797cda18dc3d372b78aa3378acc691f/lib/vulcan.js#L149 |
// TODO(jmesserly): use csslib here instead? Parsing with RegEx is sadness. |
// Maybe it's reliable enough for finding URLs in CSS? I'm not sure. |
- String visitCss(String cssText, String url) { |
+ String visitCss(String cssText) { |
+ var url = spanUrlFor(sourceId, transform); |
var src = new SourceFile.text(url, cssText); |
return cssText.replaceAllMapped(_URL, (match) { |
// Extract the URL, without any surrounding quotes. |
@@ -238,7 +284,34 @@ class _UrlNormalizer extends TreeVisitor { |
}); |
} |
- _newUrl(String href, Span span) { |
+ String visitInlineDart(String code) { |
+ var unit = parseCompilationUnit(code); |
+ var file = new SourceFile.text(spanUrlFor(sourceId, transform), code); |
+ var output = new TextEditTransaction(code, file); |
+ |
+ for (Directive directive in unit.directives) { |
+ if (directive is UriBasedDirective) { |
+ var uri = directive.uri.stringValue; |
+ var span = _getSpan(file, directive.uri); |
+ |
+ // TODO(jmesserly): _newUrl supports some things we shouldn't allow |
+ // in Dart imports, such as assets/. |
+ var targetUri = _newUrl(uri, span); |
+ if (targetUri != uri) { |
+ output.edit(span.start.offset, span.end.offset, "'$targetUri'"); |
+ } |
+ } |
+ } |
+ |
+ if (!output.hasEdits) return code; |
+ |
+ // TODO(sigmund): emit source maps when barback supports it (see |
+ // dartbug.com/12340) |
+ return (output.commit()..build(file.url)).text; |
+ |
+ } |
+ |
+ String _newUrl(String href, Span span) { |
var uri = Uri.parse(href); |
if (uri.isAbsolute) return href; |
if (!uri.scheme.isEmpty) return href; |
@@ -289,3 +362,5 @@ const _urlAttributes = const [ |
'src', // in audio, embed, iframe, img, input, script, source, track, |
// video |
]; |
+ |
+_getSpan(SourceFile file, ASTNode node) => file.span(node.offset, node.end); |