OLD | NEW |
| (Empty) |
1 library dromaeo.transformer; | |
2 | |
3 import 'dart:async'; | |
4 import 'package:barback/barback.dart'; | |
5 | |
6 /// Transformer used by `pub build` and `pub serve` to rewrite dromaeo html | |
7 /// files to run performance tests. | |
8 class DromaeoTransformer extends Transformer { | |
9 | |
10 final BarbackSettings settings; | |
11 | |
12 DromaeoTransformer.asPlugin(this.settings); | |
13 | |
14 /// The index.html file and the tests/dom-*-html.html files are the ones we | |
15 /// apply this transform to. | |
16 Future<bool> isPrimary(AssetId id) { | |
17 var reg = new RegExp('(tests/dom-.+-html\.html\$)|(index\.html\$)'); | |
18 return new Future.value(reg.hasMatch(id.path)); | |
19 } | |
20 | |
21 Future apply(Transform transform) { | |
22 Asset primaryAsset = transform.primaryInput; | |
23 AssetId primaryAssetId = primaryAsset.id; | |
24 | |
25 return primaryAsset.readAsString().then((String fileContents) { | |
26 var filename = primaryAssetId.toString(); | |
27 var outputFileContents = fileContents; | |
28 | |
29 if (filename.endsWith('index.html')) { | |
30 var index = outputFileContents.indexOf( | |
31 '<script src="packages/browser/dart.js">'); | |
32 outputFileContents = outputFileContents.substring(0, index) + | |
33 '<script src="packages/browser_controller' + | |
34 '/perf_test_controller.js"></script>\n' + | |
35 outputFileContents.substring(index); | |
36 transform.addOutput(new Asset.fromString(new AssetId.parse( | |
37 primaryAssetId.toString().replaceAll('.html', '-dart.html')), | |
38 outputFileContents)); | |
39 } | |
40 | |
41 outputFileContents = _sourceJsNotDart(outputFileContents); | |
42 // Rename the script to take the JavaScript source. | |
43 transform.addOutput(new Asset.fromString(new AssetId.parse( | |
44 _appendJs(primaryAssetId.toString())), | |
45 outputFileContents)); | |
46 }); | |
47 } | |
48 | |
49 String _appendJs(String path) => path.replaceAll('.html', '-js.html'); | |
50 | |
51 /// Given an html file that sources a Dart file, rewrite the html to instead | |
52 /// source the compiled JavaScript file. | |
53 String _sourceJsNotDart(String fileContents) { | |
54 var dartScript = new RegExp( | |
55 '<script type="application/dart" src="([\\w-]+)\.dart">'); | |
56 var match = dartScript.firstMatch(fileContents); | |
57 return fileContents.replaceAll(dartScript, '<script type="text/javascript"' | |
58 ' src="${match.group(1)+ ".dart.js"}" defer>'); | |
59 } | |
60 } | |
OLD | NEW |