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

Side by Side Diff: pkg/polymer/lib/src/build/import_inliner.dart

Issue 662013004: fix duplicate script issue (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review updates Created 6 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 /// Transfomer that inlines polymer-element definitions from html imports. 5 /// Transfomer that inlines polymer-element definitions from html imports.
6 library polymer.src.build.import_inliner; 6 library polymer.src.build.import_inliner;
7 7
8 import 'dart:async'; 8 import 'dart:async';
9 import 'dart:convert'; 9 import 'dart:convert';
10 import 'dart:collection' show LinkedHashSet;
10 11
11 import 'package:analyzer/analyzer.dart'; 12 import 'package:analyzer/analyzer.dart';
12 import 'package:analyzer/src/generated/ast.dart'; 13 import 'package:analyzer/src/generated/ast.dart';
13 import 'package:barback/barback.dart'; 14 import 'package:barback/barback.dart';
14 import 'package:code_transformers/assets.dart'; 15 import 'package:code_transformers/assets.dart';
15 import 'package:code_transformers/messages/build_logger.dart'; 16 import 'package:code_transformers/messages/build_logger.dart';
16 import 'package:path/path.dart' as path; 17 import 'package:path/path.dart' as path;
17 import 'package:html5lib/dom.dart' show 18 import 'package:html5lib/dom.dart' show
18 Document, DocumentFragment, Element, Node; 19 Document, DocumentFragment, Element, Node;
19 import 'package:html5lib/dom_parsing.dart' show TreeVisitor; 20 import 'package:html5lib/dom_parsing.dart' show TreeVisitor;
20 import 'package:source_maps/refactor.dart' show TextEditTransaction; 21 import 'package:source_maps/refactor.dart' show TextEditTransaction;
21 import 'package:source_span/source_span.dart'; 22 import 'package:source_span/source_span.dart';
22 23
23 import 'common.dart'; 24 import 'common.dart';
24 import 'messages.dart'; 25 import 'messages.dart';
25 26
26 // TODO(sigmund): move to web_components package (dartbug.com/18037). 27 // TODO(sigmund): move to web_components package (dartbug.com/18037).
27 class _HtmlInliner extends PolymerTransformer { 28 class _HtmlInliner extends PolymerTransformer {
28 final TransformOptions options; 29 final TransformOptions options;
29 final Transform transform; 30 final Transform transform;
30 final BuildLogger logger; 31 final BuildLogger logger;
31 final AssetId docId; 32 final AssetId docId;
32 final seen = new Set<AssetId>(); 33 final seen = new Set<AssetId>();
33 final scriptIds = <AssetId>[]; 34 final scriptIds = new LinkedHashSet<AssetId>();
34 final inlinedStylesheetIds = new Set<AssetId>(); 35 final inlinedStylesheetIds = new Set<AssetId>();
35 final extractedFiles = new Set<AssetId>(); 36 final extractedFiles = new Set<AssetId>();
36 bool experimentalBootstrap = false; 37 bool experimentalBootstrap = false;
37 final Element importsWrapper = new Element.html('<div hidden></div>'); 38 final Element importsWrapper = new Element.html('<div hidden></div>');
38 39
39 /// The number of extracted inline Dart scripts. Used as a counter to give 40 /// The number of extracted inline Dart scripts. Used as a counter to give
40 /// unique-ish filenames. 41 /// unique-ish filenames.
41 int inlineScriptCounter = 0; 42 int inlineScriptCounter = 0;
42 43
43 _HtmlInliner(TransformOptions options, Transform transform) 44 _HtmlInliner(TransformOptions options, Transform transform)
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 88
88 var output = transform.primaryInput; 89 var output = transform.primaryInput;
89 if (changed) output = new Asset.fromString(docId, document.outerHtml); 90 if (changed) output = new Asset.fromString(docId, document.outerHtml);
90 transform.addOutput(output); 91 transform.addOutput(output);
91 92
92 // We produce a secondary asset with extra information for later phases. 93 // We produce a secondary asset with extra information for later phases.
93 transform.addOutput(new Asset.fromString( 94 transform.addOutput(new Asset.fromString(
94 docId.addExtension('._data'), 95 docId.addExtension('._data'),
95 JSON.encode({ 96 JSON.encode({
96 'experimental_bootstrap': experimentalBootstrap, 97 'experimental_bootstrap': experimentalBootstrap,
97 'script_ids': scriptIds, 98 'script_ids': scriptIds.toList(),
98 }, toEncodable: (id) => id.serialize()))); 99 }, toEncodable: (id) => id.serialize())));
99 100
100 // Write out the logs collected by our [BuildLogger]. 101 // Write out the logs collected by our [BuildLogger].
101 if (options.injectBuildLogsInOutput) { 102 if (options.injectBuildLogsInOutput) {
102 return logger.writeOutput(); 103 return logger.writeOutput();
103 } 104 }
104 }); 105 });
105 } 106 }
106 107
107 /// Visits imports in [document] and add the imported documents to documents. 108 /// Visits imports in [document] and add the imported documents to documents.
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 /// the script tags. Instead we remove them entirely. 235 /// the script tags. Instead we remove them entirely.
235 Future<bool> _removeScripts(Document doc) { 236 Future<bool> _removeScripts(Document doc) {
236 bool changed = false; 237 bool changed = false;
237 return Future.forEach(doc.querySelectorAll('script'), (script) { 238 return Future.forEach(doc.querySelectorAll('script'), (script) {
238 if (script.attributes['type'] == TYPE_DART) { 239 if (script.attributes['type'] == TYPE_DART) {
239 changed = true; 240 changed = true;
240 script.remove(); 241 script.remove();
241 var src = script.attributes['src']; 242 var src = script.attributes['src'];
242 var srcId = uriToAssetId(docId, src, logger, script.sourceSpan); 243 var srcId = uriToAssetId(docId, src, logger, script.sourceSpan);
243 244
245 // No duplicates allowed!
246 if (scriptIds.contains(srcId)) {
247 logger.warning(SCRIPT_INCLUDED_MORE_THAN_ONCE.create({'url': src}),
248 span: script.sourceSpan);
249 return true;
250 }
251
244 // We check for extractedFiles because 'hasInput' below is only true for 252 // We check for extractedFiles because 'hasInput' below is only true for
245 // assets that existed before this transformer runs (hasInput is false 253 // assets that existed before this transformer runs (hasInput is false
246 // for files created by [_extractScripts]). 254 // for files created by [_extractScripts]).
247 if (extractedFiles.contains(srcId)) { 255 if (extractedFiles.contains(srcId)) {
248 scriptIds.add(srcId); 256 scriptIds.add(srcId);
249 return true; 257 return true;
250 } 258 }
251 259
252 return transform.hasInput(srcId).then((exists) { 260 return transform.hasInput(srcId).then((exists) {
253 if (!exists) { 261 if (!exists) {
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 /// style tag except these ones. 560 /// style tag except these ones.
553 const IGNORED_LINKED_STYLE_ATTRS = 561 const IGNORED_LINKED_STYLE_ATTRS =
554 const ['charset', 'href', 'href-lang', 'rel', 'rev']; 562 const ['charset', 'href', 'href-lang', 'rel', 'rev'];
555 563
556 /// Global RegExp objects. 564 /// Global RegExp objects.
557 final _INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]'); 565 final _INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]');
558 final _NUM_REGEX = new RegExp('[0-9]'); 566 final _NUM_REGEX = new RegExp('[0-9]');
559 final _BINDING_REGEX = new RegExp(r'(({{.*}})|(\[\[.*\]\]))'); 567 final _BINDING_REGEX = new RegExp(r'(({{.*}})|(\[\[.*\]\]))');
560 568
561 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); 569 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end);
OLDNEW
« no previous file with comments | « pkg/polymer/lib/src/build/generated/messages.html ('k') | pkg/polymer/lib/src/build/messages.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698