OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// Transformer used for pub serve and pub build | |
6 library web_components.transformer; | |
7 | |
8 import 'package:barback/barback.dart'; | |
9 | |
10 export 'build/html_import_annotation_recorder.dart'; | |
11 import 'build/import_inliner.dart'; | |
12 export 'build/import_inliner.dart'; | |
13 import 'build/script_compactor.dart'; | |
14 export 'build/script_compactor.dart'; | |
15 import 'build/test_compatibility.dart'; | |
16 export 'build/test_compatibility.dart'; | |
17 import 'build/web_components.dart'; | |
18 export 'build/web_components.dart'; | |
19 | |
20 /// The Web Components transformer group, which internally runs several phases | |
21 /// that: | |
22 /// * Extract inlined script tags into separate files. | |
23 /// * Extract script tags from entry points and all html imports and add them | |
24 /// as dart imports in a new *.bootstrap.dart file. | |
25 /// * Run the `initialize` transformer. | |
26 /// * Inlines @HtmlImport annotations as html imports into the entry point. | |
27 /// * Inline imported html files and remove all but the main dart script tag. | |
28 /// | |
29 /// At the end of these phases, this tranformer produces a single entrypoint | |
30 /// HTML file with a single Dart script that can later be compiled with dart2js. | |
31 class WebComponentsTransformerGroup implements TransformerGroup { | |
32 final Iterable<Iterable> phases; | |
33 | |
34 WebComponentsTransformerGroup(TransformOptions options) | |
35 : phases = createDeployPhases(options); | |
36 | |
37 WebComponentsTransformerGroup.asPlugin(BarbackSettings settings) | |
38 : this(_parseSettings(settings)); | |
39 } | |
40 | |
41 /// Create deploy phases for web_components. | |
42 List<List<Transformer>> createDeployPhases(TransformOptions options, | |
43 {String sdkDir}) { | |
44 var phases = []; | |
45 | |
46 /// Must happen first, temporarily rewrites <link rel="x-dart-test"> tags to | |
47 /// <script type="application/dart" _was_test></script> tags. | |
48 phases.add([new RewriteXDartTestToScript(options.entryPoints)]); | |
49 | |
50 // Must happen before the WebComponents transformer, grabs all dart scripts | |
51 // and combines them into one bootstrap file. | |
52 phases.add([new ScriptCompactorTransformer(options.entryPoints)]); | |
53 | |
54 // Runs the customized version of the `initialize` transformer and inlines | |
55 // @HtmlImport annotations. | |
56 phases.add([new WebComponentsTransformer(options)]); | |
57 | |
58 // Inlines all html imports and removes all dart script tags in the process. | |
59 phases.add([new ImportInlinerTransformer(options.entryPoints)]); | |
60 | |
61 /// Must happen last, rewrites | |
62 /// <script type="application/dart" _was_test></script> tags to | |
63 /// <link rel="x-dart-test"> tags. | |
64 phases.add([new RewriteScriptToXDartTest(options.entryPoints)]); | |
65 return phases; | |
66 } | |
67 | |
68 /// Options used by web_components transformers | |
69 class TransformOptions { | |
70 /// List of entrypoints paths. The paths are relative to the package root and | |
71 /// are represented using posix style, which matches the representation used | |
72 /// in asset ids in barback. If null, any html file under 'web/' or 'test/' is | |
73 /// considered an entry point. | |
74 final List<String> entryPoints; | |
75 | |
76 /// Current Barback mode. | |
77 final bool releaseMode; | |
78 | |
79 TransformOptions(this.entryPoints, this.releaseMode); | |
80 | |
81 /// Whether an asset with [id] is an entry point HTML file. | |
82 bool isHtmlEntryPoint(AssetId id) { | |
83 if (id.extension != '.html') return false; | |
84 | |
85 // Note: [id.path] is a relative path from the root of a package. | |
86 if (entryPoints == null) { | |
87 return id.path.startsWith('web/') || id.path.startsWith('test/'); | |
88 } | |
89 | |
90 return entryPoints.contains(id.path); | |
91 } | |
92 } | |
93 | |
94 // Builds TransformOptions given some BarbackSettings | |
95 TransformOptions _parseSettings(BarbackSettings settings) { | |
96 var args = settings.configuration; | |
97 bool releaseMode = settings.mode == BarbackMode.RELEASE; | |
98 var entryPoints = readFileList(args['entry_points']); | |
99 return new TransformOptions(entryPoints, releaseMode); | |
100 } | |
101 | |
102 /// Reads a file list value from the [BarbackSettings] | |
103 /// TODO(jakemac): This should also move to code_transformers. | |
104 readFileList(value) { | |
105 if (value == null) return null; | |
106 var files = []; | |
107 bool error; | |
108 if (value is List) { | |
109 files = value; | |
110 error = value.any((e) => e is! String); | |
111 } else if (value is String) { | |
112 files = [value]; | |
113 error = false; | |
114 } else { | |
115 error = true; | |
116 } | |
117 if (error) { | |
118 print('Bad value for "entry_points" in the web_components transformer. ' | |
119 'Expected either one String or a list of Strings.'); | |
120 } | |
121 return files; | |
122 } | |
OLD | NEW |