update mirror based implementation to work with new LibraryIdentifier and unify url normalization logic
diff --git a/lib/build/html_import_annotation_inliner.dart b/lib/build/html_import_annotation_inliner.dart
index 416e162..491b4c7 100644
--- a/lib/build/html_import_annotation_inliner.dart
+++ b/lib/build/html_import_annotation_inliner.dart
@@ -7,7 +7,7 @@
import 'package:barback/barback.dart';
import 'package:html5lib/dom.dart' as dom;
import 'package:html5lib/parser.dart';
-import 'package:path/path.dart' as path;
+import '../src/normalize_path.dart';
/// 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
@@ -53,9 +53,11 @@
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)));
+ importPaths = new Set.from(matches.map((match) =>
+ normalizeHtmlImportPath(
+ match.group(1),
+ match.group(2) == 'null' ? null : match.group(2),
+ match.group(3))));
var newDartCode = dartCode.replaceAll(_htmlImportWithRawString, '');
var leftoverMatches = _htmlImportGeneral.allMatches(newDartCode);
@@ -81,38 +83,6 @@
});
}
- 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(
diff --git a/lib/html_import_annotation.dart b/lib/html_import_annotation.dart
index 7039064..098a1ee 100644
--- a/lib/html_import_annotation.dart
+++ b/lib/html_import_annotation.dart
@@ -6,23 +6,26 @@
import 'dart:async';
import 'dart:html';
import 'package:initialize/initialize.dart';
+import 'src/normalize_path.dart';
/// Annotation for a dart library which injects an html import into the
/// current html document. The imported file must not contain any dart script
/// tags, as they cannot be dynamically loaded.
-class HtmlImport implements Initializer<Symbol> {
- final String source;
+class HtmlImport implements Initializer<LibraryIdentifier> {
+ final String filePath;
- const HtmlImport(this.source);
+ const HtmlImport(this.filePath);
- Future initialize(Symbol library) {
- var element = new LinkElement()..rel = 'import'..href = source;
+ Future initialize(LibraryIdentifier library) {
+ var element = new LinkElement()
+ ..rel = 'import'
+ ..href = normalizeHtmlImportPath(filePath, library.package, library.path);
document.head.append(element);
var completer = new Completer();
var listeners = [
element.on['load'].listen((_) => completer.complete()),
element.on['error'].listen((_) {
- print('Error loading html import from path `$source`');
+ print('Error loading html import from path `$filePath`');
completer.complete();
}),
];
@@ -31,3 +34,5 @@
});
}
}
+
+
diff --git a/lib/src/normalize_path.dart b/lib/src/normalize_path.dart
new file mode 100644
index 0000000..37a44fb
--- /dev/null
+++ b/lib/src/normalize_path.dart
@@ -0,0 +1,28 @@
+// 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.src.normalizePath;
+
+import 'package:path/path.dart' as path;
+
+String normalizeHtmlImportPath(String filePath, String dartFilePackage,
+ String dartFilePath) {
+ // If they already supplied a packages path, just return that.
+ if (filePath.startsWith('package:')) {
+ return filePath.replaceFirst('package:', 'packages/');
+ }
+
+ var dartFileDir = path.url.dirname(dartFilePath);
+ var segments = path.url.split(dartFileDir);
+ // The dartFileDir without the leading dir (web, lib, test, etc).
+ var dartFileSubDir = path.url.joinAll(segments.getRange(1, segments.length));
+
+ // Relative paths have no package supplied.
+ if (dartFilePackage == null) {
+ return path.url.normalize(path.url.join(dartFileSubDir, filePath));
+ }
+
+ // Only option left is a packages/ path.
+ return path.url.normalize(
+ path.url.join('packages/', dartFilePackage, dartFileSubDir, filePath));
+}
diff --git a/test/build/html_import_annotation_inliner_test.dart b/test/build/html_import_annotation_inliner_test.dart
index 3badb2f..bee6668 100644
--- a/test/build/html_import_annotation_inliner_test.dart
+++ b/test/build/html_import_annotation_inliner_test.dart
@@ -31,9 +31,7 @@
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('../foo.html'), const LibraryIdentifier(#foo, null, 'web/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')),