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.build.web_components; | |
7 | |
8 import 'dart:async'; | |
9 import 'package:barback/barback.dart'; | |
10 import 'package:code_transformers/assets.dart'; | |
11 import 'package:code_transformers/messages/build_logger.dart'; | |
12 import 'package:code_transformers/resolver.dart'; | |
13 import 'package:code_transformers/src/dart_sdk.dart' as dart_sdk; | |
14 import 'package:html/dom.dart' as dom; | |
15 import 'package:initialize/transformer.dart' show generateBootstrapFile; | |
16 import 'package:initialize/build/initializer_plugin.dart'; | |
17 import 'package:path/path.dart' as path; | |
18 import 'package:web_components/transformer.dart'; | |
19 import 'common.dart'; | |
20 | |
21 /// Public method that can be used inside any [Transformer] which already has a | |
22 /// [Resolver] and [Transform] to generate a bootstrap file for the | |
23 /// web_components package. | |
24 Asset generateWebComponentsBootstrap(Resolver resolver, Transform transform, | |
25 dom.Document document, AssetId scriptId, AssetId newScriptId, | |
26 {List<InitializerPlugin> extraPlugins: const []}) { | |
27 var htmlImportRecorder = new HtmlImportAnnotationRecorder(); | |
28 var plugins = [htmlImportRecorder]..addAll(extraPlugins); | |
29 | |
30 // Bootstrap the application using the `initialize` package and our | |
31 // plugins. | |
32 var initializeBootstrap = generateBootstrapFile( | |
33 resolver, transform, scriptId, newScriptId, | |
34 errorIfNotFound: false, plugins: plugins); | |
35 | |
36 // Add all seen imports to the document, before the first dart script tag if | |
37 // it exists. | |
38 var dartScript = | |
39 document.head.querySelector('script[type="application/dart"]'); | |
40 for (var importPath in htmlImportRecorder.importPaths) { | |
41 var import = new dom.Element.tag('link') | |
42 ..attributes = {'rel': 'import', 'href': importPath,}; | |
43 document.head.insertBefore(import, dartScript); | |
44 } | |
45 | |
46 return initializeBootstrap; | |
47 } | |
48 | |
49 /// A [Transformer] which runs the `initialize` transformer with | |
50 /// some special plugins and also inlines the html imports. | |
51 class WebComponentsTransformer extends Transformer { | |
52 final Resolvers _resolvers; | |
53 TransformOptions options; | |
54 | |
55 WebComponentsTransformer(this.options) | |
56 : _resolvers = new Resolvers.fromMock(dart_sdk.mockSdkSources); | |
57 | |
58 bool isPrimary(AssetId id) { | |
59 if (options.entryPoints != null) { | |
60 return options.entryPoints.contains(id.path); | |
61 } | |
62 if (id.path == 'web/index.bootstrap.dart') return true; | |
63 // If no entry point is supplied, then any html file under web/ or test/ is | |
64 // an entry point. | |
65 return (id.path.startsWith('web/') || id.path.startsWith('test/')) && | |
66 id.path.endsWith('.html'); | |
67 } | |
68 | |
69 Future apply(Transform transform) { | |
70 var logger = new BuildLogger(transform); | |
71 var primaryInput = transform.primaryInput; | |
72 return primaryInput.readAsString().then((html) { | |
73 // Find the dart script in the page. | |
74 var doc = parseHtml(html, primaryInput.id.path); | |
75 var mainScriptTag = doc.querySelector('script[type="$dartType"]'); | |
76 var scriptId = uriToAssetId(primaryInput.id, | |
77 mainScriptTag.attributes['src'], logger, mainScriptTag.sourceSpan); | |
78 | |
79 return _resolvers.get(transform, [scriptId]).then((resolver) { | |
80 var newScriptId = new AssetId(scriptId.package, | |
81 '${path.url.withoutExtension(scriptId.path)}.initialize.dart'); | |
82 | |
83 var bootstrap = generateWebComponentsBootstrap( | |
84 resolver, transform, doc, scriptId, newScriptId); | |
85 | |
86 // Swap out the main script tag for the bootstrap version. | |
87 mainScriptTag.attributes['src'] = path.url.relative(bootstrap.id.path, | |
88 from: path.url.dirname(primaryInput.id.path)); | |
89 | |
90 // Output the new document and bootstrap file. | |
91 transform | |
92 .addOutput(new Asset.fromString(primaryInput.id, doc.outerHtml)); | |
93 transform.addOutput(bootstrap); | |
94 }); | |
95 }); | |
96 } | |
97 } | |
OLD | NEW |