Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Side by Side Diff: lib/build/web_components.dart

Issue 950883003: fix bug in scriptcompactors bootstrap and added transform function which can be used by other trans… (Closed) Base URL: git@github.com:dart-lang/web-components.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /// Transformer used for pub serve and pub build 5 /// Transformer used for pub serve and pub build
6 library web_components.build.web_components; 6 library web_components.build.web_components;
7 7
8 import 'dart:async'; 8 import 'dart:async';
9 import 'package:barback/barback.dart'; 9 import 'package:barback/barback.dart';
10 import 'package:code_transformers/assets.dart'; 10 import 'package:code_transformers/assets.dart';
11 import 'package:code_transformers/messages/build_logger.dart'; 11 import 'package:code_transformers/messages/build_logger.dart';
12 import 'package:code_transformers/resolver.dart'; 12 import 'package:code_transformers/resolver.dart';
13 import 'package:html5lib/dom.dart' as dom; 13 import 'package:html5lib/dom.dart' as dom;
14 import 'package:initialize/transformer.dart' show generateBootstrapFile; 14 import 'package:initialize/transformer.dart' show generateBootstrapFile;
15 import 'package:initialize/build/initializer_plugin.dart';
15 import 'package:path/path.dart' as path; 16 import 'package:path/path.dart' as path;
16 import 'package:web_components/transformer.dart'; 17 import 'package:web_components/transformer.dart';
17 import 'common.dart'; 18 import 'common.dart';
18 19
20 /// Public method that can be used inside any [Transformer] which already has a
21 /// [Resolver] and [Transform] to generate a bootstrap file for the
22 /// web_components package.
23 Asset generateWebComponentsBootstrap(Resolver resolver, Transform transform,
24 dom.Document document, AssetId scriptId, AssetId newScriptId,
25 {List<InitializerPlugin> extraPlugins: const []}) {
26 var htmlImportRecorder = new HtmlImportAnnotationRecorder();
27 var plugins = [htmlImportRecorder]..addAll(extraPlugins);
28
29 // Bootstrap the application using the `initialize` package and our
30 // plugins.
31 var initializeBootstrap = generateBootstrapFile(
32 resolver, transform, scriptId, newScriptId,
33 errorIfNotFound: false, plugins: plugins);
34
35 // Add all seen imports to the document.
36 for (var importPath in htmlImportRecorder.importPaths) {
37 document.head.append(new dom.Element.tag('link')
38 ..attributes = {'rel': 'import', 'href': importPath,});
39 }
40
41 return initializeBootstrap;
42 }
43
19 /// A [Transformer] which runs the `initialize` transformer with 44 /// A [Transformer] which runs the `initialize` transformer with
20 /// some special plugins and also inlines the html imports. 45 /// some special plugins and also inlines the html imports.
21 class WebComponentsTransformer extends Transformer { 46 class WebComponentsTransformer extends Transformer {
22 final Resolvers _resolvers; 47 final Resolvers _resolvers;
23 TransformOptions options; 48 TransformOptions options;
24 49
25 WebComponentsTransformer(this.options) : _resolvers = new Resolvers.fromMock({ 50 WebComponentsTransformer(this.options) : _resolvers = new Resolvers.fromMock({
26 // The list of types below is derived from: 51 // The list of types below is derived from:
27 // * types that are used internally by the resolver (see 52 // * types that are used internally by the resolver (see
28 // _initializeFrom in resolver.dart). 53 // _initializeFrom in resolver.dart).
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 // Find the dart script in the page. 109 // Find the dart script in the page.
85 var doc = parseHtml(html, primaryInput.id.path); 110 var doc = parseHtml(html, primaryInput.id.path);
86 var mainScriptTag = doc.querySelector('script[type="$dartType"]'); 111 var mainScriptTag = doc.querySelector('script[type="$dartType"]');
87 var scriptId = uriToAssetId(primaryInput.id, 112 var scriptId = uriToAssetId(primaryInput.id,
88 mainScriptTag.attributes['src'], logger, mainScriptTag.sourceSpan); 113 mainScriptTag.attributes['src'], logger, mainScriptTag.sourceSpan);
89 114
90 return _resolvers.get(transform, [scriptId]).then((resolver) { 115 return _resolvers.get(transform, [scriptId]).then((resolver) {
91 var newScriptId = new AssetId(scriptId.package, 116 var newScriptId = new AssetId(scriptId.package,
92 '${path.url.withoutExtension(scriptId.path)}.initialize.dart'); 117 '${path.url.withoutExtension(scriptId.path)}.initialize.dart');
93 118
94 // Bootstrap the application using the `initialize` package and the 119 var bootstrap = generateWebComponentsBootstrap(
95 // html import annotation recorder plugin. 120 resolver, transform, doc, scriptId, newScriptId);
96 var htmlImportRecorder = new HtmlImportAnnotationRecorder();
97 var initializeBootstrap = generateBootstrapFile(
98 resolver, transform, scriptId, newScriptId,
99 errorIfNotFound: false, plugins: [htmlImportRecorder]);
100 transform.addOutput(initializeBootstrap);
101
102 // Add all seen imports to the document.
103 for (var importPath in htmlImportRecorder.importPaths) {
104 doc.head.append(new dom.Element.tag('link')
105 ..attributes = {'rel': 'import', 'href': importPath,});
106 }
107 121
108 // Swap out the main script tag for the bootstrap version. 122 // Swap out the main script tag for the bootstrap version.
109 mainScriptTag.attributes['src'] = path.url.relative( 123 mainScriptTag.attributes['src'] = path.url.relative(bootstrap.id.path,
110 initializeBootstrap.id.path,
111 from: path.url.dirname(primaryInput.id.path)); 124 from: path.url.dirname(primaryInput.id.path));
112 125
126 // Output the new document and bootstrap file.
113 transform 127 transform
114 .addOutput(new Asset.fromString(primaryInput.id, doc.outerHtml)); 128 .addOutput(new Asset.fromString(primaryInput.id, doc.outerHtml));
129 transform.addOutput(bootstrap);
115 }); 130 });
116 }); 131 });
117 } 132 }
118 } 133 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698