add transformer and update initialize version
diff --git a/.gitignore b/.gitignore
index e979170..6e8cd5c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,6 @@
# Don’t commit the following directories created by pub.
.pub
-build/
+/build/
packages
# Or the files created by dart2js.
diff --git a/lib/build/html_import_annotation_inliner.dart b/lib/build/html_import_annotation_inliner.dart
new file mode 100644
index 0000000..416e162
--- /dev/null
+++ b/lib/build/html_import_annotation_inliner.dart
@@ -0,0 +1,127 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+library web_components.build.html_import_annotation_inliner;
+
+import 'dart:async';
+import 'package:barback/barback.dart';
+import 'package:html5lib/dom.dart' as dom;
+import 'package:html5lib/parser.dart';
+import 'package:path/path.dart' as path;
+
+/// Given an html entry point with a single dart bootstrap file created by the
+/// `initialize` transformer, this will open that dart file and remove all
+/// `HtmlImport` initializers from it. Then it appends those imports to the head
+/// of the html entry point.
+/// Note: Does not inline the import, it just puts the <link rel="import"> tag.
+class HtmlImportAnnotationInliner extends AggregateTransformer {
+ final String _bootstrapFile;
+ final String _htmlEntryPoint;
+ TransformLogger _logger;
+
+ HtmlImportAnnotationInliner(this._bootstrapFile, this._htmlEntryPoint);
+
+ factory HtmlImportAnnotationInliner.asPlugin(BarbackSettings settings) {
+ var bootstrapFile = settings.configuration['bootstrap_file'];
+ if (bootstrapFile is! String || !bootstrapFile.endsWith('.dart')) {
+ throw new ArgumentError(
+ '`bootstrap_file` should be a string path to a dart file');
+ }
+ var htmlEntryPoint = settings.configuration['html_entry_point'];
+ if (htmlEntryPoint is! String || !htmlEntryPoint.endsWith('.html')) {
+ throw new ArgumentError(
+ '`html_entry_point` should be a string path to an html file');
+ }
+ return new HtmlImportAnnotationInliner(bootstrapFile, htmlEntryPoint);
+ }
+
+ classifyPrimary(AssetId id) {
+ if (_bootstrapFile == id.path || _htmlEntryPoint == id.path) return ' ';
+ return null;
+ }
+
+ Future apply(AggregateTransform transform) {
+ _logger = transform.logger;
+ Asset dartEntryPoint;
+ Asset htmlEntryPoint;
+ return transform.primaryInputs.take(2).listen((Asset asset) {
+ if (asset.id.path == _bootstrapFile) dartEntryPoint = asset;
+ if (asset.id.path == _htmlEntryPoint) htmlEntryPoint = asset;
+ }).asFuture().then((_) {
+ // Will be populated with all import paths found in HtmlImport
+ // constructors.
+ var importPaths;
+ return dartEntryPoint.readAsString().then((dartCode) {
+ var matches = _htmlImportWithRawString.allMatches(dartCode);
+ importPaths = new Set.from(matches.map((match) => _normalizePath(
+ match.group(1), match.group(2), match.group(3),
+ htmlEntryPoint.id.package)));
+
+ var newDartCode = dartCode.replaceAll(_htmlImportWithRawString, '');
+ var leftoverMatches = _htmlImportGeneral.allMatches(newDartCode);
+ for (var match in leftoverMatches) {
+ _logger.warning('Found HtmlImport constructor which was supplied an '
+ 'expression. Only raw strings are currently supported: '
+ '${match.group(0)}');
+ }
+ transform.addOutput(
+ new Asset.fromString(dartEntryPoint.id, newDartCode));
+ }).then((_) => htmlEntryPoint.readAsString()).then((html) {
+ var doc = parse(html);
+ for (var importPath in importPaths) {
+ var import = new dom.Element.tag('link')..attributes = {
+ 'rel': 'import',
+ 'href': importPath,
+ };
+ doc.head.append(import);
+ }
+ transform.addOutput(
+ new Asset.fromString(htmlEntryPoint.id, doc.outerHtml));
+ });
+ });
+ }
+
+ String _normalizePath(String htmlPath, String dartFilePackage,
+ String dartFilePath, String rootDartPackage) {
+ // If they already supplied a packages path, just return that.
+ if (htmlPath.startsWith('package:')) {
+ return htmlPath.replaceFirst('package:', 'packages/');
+ }
+
+ var dartFileDir = path.url.dirname(dartFilePath);
+ var dartFileDirSegments = path.url.split(dartFileDir);
+ var inLibDir = dartFileDirSegments[0] == 'lib';
+ // The dartFileDir without the leading dir.
+ var dartFileSubDir = path.url.joinAll(
+ dartFileDirSegments.getRange(1, dartFileDirSegments.length));
+
+ // Relative path to something not under lib. All other paths will be turned
+ // into a packages/ path.
+ if (dartFilePackage == 'null' && !inLibDir) {
+ return path.url.normalize(path.url.join(dartFileSubDir, htmlPath));
+ }
+
+ if (!inLibDir) {
+ _logger.error('Can only import assets from a folder other than `lib` if '
+ 'they are in the same package as the entry point. Found '
+ '$dartFilePackage:$dartFilePath');
+ }
+
+ // Only option left is a packages/ path.
+ var package = dartFilePackage == 'null' ? rootDartPackage : dartFilePackage;
+ return path.url.normalize(
+ path.url.join('packages/', package, dartFileSubDir, htmlPath));
+ }
+
+ // Matches HtmlImport constructors which are supplied a raw string. These are
+ // the only ones currently supported for inlining.
+ final RegExp _htmlImportWithRawString = new RegExp(
+ r"\n\s*new InitEntry\(const i[\d]*\.HtmlImport\('([\w\d\/\.:]*\.html)'\),"
+ r"\sconst\sLibraryIdentifier\(#[\w\.]*, '?([\w_]*)'?, '([\w\d\/\.]*)'\)\)"
+ r",");
+
+ // Matches HtmlImport initializers which are supplied any arguments. This
+ // is used to detect if any were left over and not inlined.
+ final RegExp _htmlImportGeneral = new RegExp(
+ r"\n\s*new InitEntry\(const i[\d]*\.HtmlImport\('([\w\.].*)'\),\s.*\),");
+}
diff --git a/pubspec.yaml b/pubspec.yaml
index ca97552..d761671 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -10,13 +10,23 @@
elements, by hiding DOM subtrees under shadow roots. HTML Imports let authors
bundle code and HTML as if they were libraries.
dependencies:
+ html5lib: '>=0.12.0 <0.13.0'
initialize: '>=0.2.0 <0.3.0'
+ path: '>=1.3.0 <2.0.0'
dev_dependencies:
unittest: '>0.11.0 <0.12.0'
browser: '>0.10.0 <0.11.0'
+dependency_overrides:
+ initialize:
+ git:
+ url: git@github.com:dart-lang/initialize.git
+ ref: library_path
transformers:
- initialize:
entry_point: test/custom_element_proxy_test.dart
html_entry_point: test/custom_element_proxy_test.html
+- initialize:
+ entry_point: test/html_import_annotation_test.dart
+ html_entry_point: test/html_import_annotation_test.html
environment:
sdk: ">=1.4.0-dev.6.6 <2.0.0"
diff --git a/test/build/common.dart b/test/build/common.dart
new file mode 100644
index 0000000..1fbd5db
--- /dev/null
+++ b/test/build/common.dart
@@ -0,0 +1,17 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+library web_components.test.build.common;
+
+import 'package:barback/barback.dart';
+import 'package:code_transformers/src/test_harness.dart';
+import 'package:unittest/unittest.dart';
+
+testPhases(String testName, List<List<Transformer>> phases,
+ Map<String, String> inputFiles, Map<String, String> expectedFiles,
+ [List<String> expectedMessages]) {
+ test(testName, () {
+ var helper = new TestHelper(phases, inputFiles, expectedMessages)..run();
+ return helper.checkAll(expectedFiles).whenComplete(() => helper.tearDown());
+ });
+}
diff --git a/test/build/html_import_annotation_inliner_test.dart b/test/build/html_import_annotation_inliner_test.dart
new file mode 100644
index 0000000..3badb2f
--- /dev/null
+++ b/test/build/html_import_annotation_inliner_test.dart
@@ -0,0 +1,70 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+library web_components.test.build.html_import_annotation_inliner_test;
+
+import 'common.dart';
+import 'package:web_components/build/html_import_annotation_inliner.dart';
+import 'package:unittest/compact_vm_config.dart';
+
+main() {
+ useCompactVMConfiguration();
+
+ var transformer = new HtmlImportAnnotationInliner(
+ 'web/index.bootstrap.dart', 'web/index.html');
+
+ testPhases('basic', [[transformer]], {
+ 'foo|web/index.html': '''
+ <html><head></head><body>
+ <script type="application/dart" src="index.bootstrap.dart"></script>
+ </body></html>
+ '''.replaceAll(' ', ''),
+ 'foo|web/index.bootstrap.dart': '''
+ import 'package:initialize/src/static_loader.dart';
+ import 'package:initialize/src/initializer.dart';
+ import 'index.dart' as i0;
+ import 'package:web_components/html_import_annotation.dart' as i1;
+ import 'package:baz/baz.dart' as i2;
+
+ main() {
+ initializers.addAll([
+ new InitEntry(const i2.initMethod, i0.baz),
+ new InitEntry(const i1.HtmlImport('foo.html'), const LibraryIdentifier(#foo, null, 'web/foo.dart')),
+ new InitEntry(const i1.HtmlImport('foo.html'), const LibraryIdentifier(#foo, null, 'web/foo/foo.dart')),
+ new InitEntry(const i1.HtmlImport('foo.html'), const LibraryIdentifier(#foo, null, 'lib/foo.dart')),
+ new InitEntry(const i1.HtmlImport('foo.html'), const LibraryIdentifier(#foo, null, 'lib/foo/foo.dart')),
+ new InitEntry(const i1.HtmlImport('../foo.html'), const LibraryIdentifier(#foo, null, 'lib/foo/foo.dart')),
+ new InitEntry(const i1.HtmlImport('package:foo/foo.html'), const LibraryIdentifier(#foo, null, 'lib/foo.dart')),
+ new InitEntry(const i1.HtmlImport('package:foo/foo/foo.html'), const LibraryIdentifier(#foo, null, 'lib/foo/foo.dart')),
+ new InitEntry(const i1.HtmlImport('bar.html'), const LibraryIdentifier(#bar, 'bar', 'lib/bar.dart')),
+ new InitEntry(const i1.HtmlImport('bar.html'), const LibraryIdentifier(#bar.Bar, 'bar', 'lib/bar/bar.dart')),
+ new InitEntry(const i1.HtmlImport('package:bar/bar.html'), const LibraryIdentifier(#bar, 'bar', 'lib/bar.dart')),
+ new InitEntry(const i1.HtmlImport('package:bar/bar/bar.html'), const LibraryIdentifier(#bar.Bar, 'bar', 'lib/bar/bar.dart')),
+ ]);
+
+ i0.main();
+ }
+ ''',
+ }, {
+ 'foo|web/index.html': '''
+ <html><head><link rel="import" href="foo.html"><link rel="import" href="foo/foo.html"><link rel="import" href="packages/foo/foo.html"><link rel="import" href="packages/foo/foo/foo.html"><link rel="import" href="packages/bar/bar.html"><link rel="import" href="packages/bar/bar/bar.html"></head><body>
+ <script type="application/dart" src="index.bootstrap.dart"></script>
+
+ </body></html>'''.replaceAll(' ', ''),
+ 'foo|web/index.bootstrap.dart': '''
+ import 'package:initialize/src/static_loader.dart';
+ import 'package:initialize/src/initializer.dart';
+ import 'index.dart' as i0;
+ import 'package:web_components/html_import_annotation.dart' as i1;
+ import 'package:baz/baz.dart' as i2;
+
+ main() {
+ initializers.addAll([
+ new InitEntry(const i2.initMethod, i0.baz),
+ ]);
+
+ i0.main();
+ }
+ '''
+ }, []);
+}