OLD | NEW |
---|---|
(Empty) | |
1 library angular2.transformer; | |
2 | |
3 import 'dart:async'; | |
4 import 'package:barback/barback.dart'; | |
5 import 'package:html5lib/dom.dart' as dom; | |
6 import 'package:html5lib/parser.dart' show parse; | |
7 import 'package:path/path.dart' as path; | |
8 | |
9 import 'options.dart'; | |
10 | |
11 Future transformHtmlEntryPoint( | |
12 TransformerOptions options, Transform transform) { | |
13 // For now at least, [options.htmlEntryPoint], [options.entryPoint], and | |
14 // [options.newEntryPoint] need to be in the same folder. | |
15 // TODO(jakemac): support package urls with [options.entryPoint] or | |
jakemac
2015/02/18 16:18:50
The initialize package now handles this, up to you
tjblasi
2015/02/18 21:18:25
Acknowledged.
| |
16 // [options.newEntryPoint] in `lib`, and [options.htmlEntryPoint] in another | |
17 // directory. | |
18 var _expectedDir = path.split(options.htmlEntryPoint)[0]; | |
19 if (!options.inSameTopLevelDir()) { | |
20 transform.logger.error( | |
21 '${options.htmlEntryPointParam}, ${options.entryPointParam}, and ' | |
22 '${options.newEntryPointParam} (if supplied) all must be in the ' | |
23 'same top level directory.'); | |
24 } | |
25 | |
26 // The relative path from [options.htmlEntryPoint] to [dartEntry]. You must | |
27 // ensure that neither of these is null before calling this function. | |
28 String _relativeDartEntryPath(String dartEntry) => | |
29 path.relative(dartEntry, from: path.dirname(options.htmlEntryPoint)); | |
30 | |
31 // Checks if the src of this script tag is pointing at `options.entryPoint`. | |
32 bool _isEntryPointScript(dom.Element script) => | |
33 path.normalize(script.attributes['src']) == | |
34 _relativeDartEntryPath(options.entryPoint); | |
35 | |
36 return transform.primaryInput.readAsString().then((String html) { | |
37 var found = false; | |
38 var doc = parse(html); | |
39 var scripts = doc.querySelectorAll('script[type="application/dart"]'); | |
40 for (dom.Element script in scripts) { | |
41 if (!_isEntryPointScript(script)) continue; | |
42 script.attributes['src'] = _relativeDartEntryPath(options.newEntryPoint); | |
43 found = true; | |
44 } | |
45 if (!found) { | |
46 transform.logger.error('Unable to find script for ${options.entryPoint} ' | |
47 'in ${options.htmlEntryPoint}.'); | |
48 } | |
49 return transform.addOutput( | |
50 new Asset.fromString(transform.primaryInput.id, doc.outerHtml)); | |
51 }); | |
52 } | |
OLD | NEW |