{"action":"created","comment":{"url":"https://api.github.com/repos/Polymer/polymer-cli/pulls/comments/109239422","pull_request_review_id":30329999,"id":109239422,"diff_hunk":"@@ -49,24 +78,73 @@ export class UseES5WebcomponentsLoader extends stream.Transform {\n         stream.on('error', reject);\n       });\n     }\n-    if (!/webcomponents-loader\\.js/.test(contents)) {\n+\n+    if (!webcomponentsLoaderRegex.test(contents)) {\n       callback(null, file);\n       return;\n     }\n+\n     const parsed = parse5.parse(contents);\n-    const script = dom5.nodeWalk(parsed, scriptIncludeWebcomponentsLoader);\n+    const script = dom5.nodeWalk(parsed, webcomponentsLoaderMatcher);\n     if (!script) {\n       callback(null, file);\n       return;\n     }\n \n-    const scriptPath = dom5.getAttribute(script, 'src')!;\n-    const scriptUrl = url.resolve(scriptPath, 'custom-elements-es5-adapter.js');\n-    const es5AdapterScript = parse5.parseFragment(`<script src=\"${scriptUrl}\"></script>`);\n-    dom5.insertBefore(script.parentNode!, script, es5AdapterScript);\n-\n+    // Collect important dom references & create fragment for injection\n     const correctedFile = file.clone();\n+    const bodyElement = dom5.query(parsed, bodyMatcher)!;\n+    const headElement = dom5.query(parsed, headMatcher)!;\n+    const loaderScriptUrl = dom5.getAttribute(script, 'src')!;\n+    const adapterScriptUrl =\n+        url.resolve(loaderScriptUrl, 'custom-elements-es5-adapter.js');\n+    const loaderMovedComment = dom5.constructors.comment(\n+        ' AUTO-GENERATED BY JS-COMPILE: \"webcomponents-loader.js\" and ' +\n+        'related resources have been moved to <body> so that ' +\n+        '\"custom-elements-es5-adapter.js\" polyfill could be properly ' +\n+        'injected into this file. ');\n+    const es5AdapterScript = parse5.parseFragment(`\n+<div id=\"autogenerated-ce-es5-shim\">\n+  <script type=\"text/javascript\">\n+    // This prevents custom-elements-es5-adapter.js from parsing & running\n+    // on browsers without native support for ES2015+ & custom elements.\n+    if (!window.customElements) {\n+      var ceShimContainer = document.querySelector('#autogenerated-ce-es5-shim');\n+      ceShimContainer.parentElement.removeChild(ceShimContainer);\n+    }\n+  </script>\n+  <script type=\"text/javascript\" src=\"${adapterScriptUrl}\"></script>\n+</div>\n+`);\n+\n+    // If script is in the body, just insert the es5 adapter script before it\n+    if (dom5.nodeWalkAncestors(script, bodyMatcher)) {\n+      dom5.insertBefore(bodyElement, script, es5AdapterScript);\n+      correctedFile.contents = new Buffer(parse5.serialize(parsed), 'utf-8');\n+      callback(null, correctedFile);\n+      return;\n+    }\n+\n+    // otherwise we need to move the webcomponents-loader/webcomponents-lite\n+    // loader down to the body so that the es5 adaperter script shim will work\n+    const scriptSiblings = dom5.queryAll(\n+        script.parentNode!,\n+        dom5.predicates.OR(scriptMatcher, styleMatcher, linkMatcher));\n+    const scriptSiblingsFollowing =\n+        scriptSiblings.splice(scriptSiblings.indexOf(script));","path":"src/build/rewrite-webcomponents-loader.ts","position":121,"original_position":121,"commit_id":"d6e4c60b0b10474e593921ca4ae826a58e1f91c4","original_commit_id":"d6e4c60b0b10474e593921ca4ae826a58e1f91c4","user":{"login":"rictic","id":1659,"avatar_url":"https://avatars0.githubusercontent.com/u/1659?v=3","gravatar_id":"","url":"https://api.github.com/users/rictic","html_url":"https://github.com/rictic","followers_url":"https://api.github.com/users/rictic/followers","following_url":"https://api.github.com/users/rictic/following{/other_user}","gists_url":"https://api.github.com/users/rictic/gists{/gist_id}","starred_url":"https://api.github.com/users/rictic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rictic/subscriptions","organizations_url":"https://api.github.com/users/rictic/orgs","repos_url":"https://api.github.com/users/rictic/repos","events_url":"https://api.github.com/users/rictic/events{/privacy}","received_events_url":"https://api.github.com/users/rictic/received_events","type":"User","site_admin":false},"body":"I think you want `slice` rather than `splice` here, as the latter actually mutates the array in place(!)","created_at":"2017-03-31T20:03:04Z","updated_at":"2017-03-31T20:11:12Z","html_url":"https://github.com/Polymer/polymer-cli/pull/635#discussion_r109239422","pull_request_url":"https://api.github.com/repos/Polymer/polymer-cli/pulls/635","_links":{"self":{"href":"https://api.github.com/repos/Polymer/polymer-cli/pulls/comments/109239422"},"html":{"href":"https://github.com/Polymer/polymer-cli/pull/635#discussion_r109239422"},"pull_request":{"href":"https://api.github.com/repos/Polymer/polymer-cli/pulls/635"}}},"pull_request":{"url":"https://api.github.com/repos/Polymer/polymer-cli/pulls/635","id":113087197,"html_url":"https://github.com/Polymer/polymer-cli/pull/635","diff_url":"https://github.com/Polymer/polymer-cli/pull/635.diff","patch_url":"https://github.com/Polymer/polymer-cli/pull/635.patch","issue_url":"https://api.github.com/repos/Polymer/polymer-cli/issues/635","number":635,"state":"open","locked":false,"title":"properly inject es5 adapter for browsers without es5 support","user":{"login":"FredKSchott","id":622227,"avatar_url":"https://avatars2.githubusercontent.com/u/622227?v=3","gravatar_id":"","url":"https://api.github.com/users/FredKSchott","html_url":"https://github.com/FredKSchott","followers_url":"https://api.github.com/users/FredKSchott/followers","following_url":"https://api.github.com/users/FredKSchott/following{/other_user}","gists_url":"https://api.github.com/users/FredKSchott/gists{/gist_id}","starred_url":"https://api.github.com/users/FredKSchott/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/FredKSchott/subscriptions","organizations_url":"https://api.github.com/users/FredKSchott/orgs","repos_url":"https://api.github.com/users/FredKSchott/repos","events_url":"https://api.github.com/users/FredKSchott/events{/privacy}","received_events_url":"https://api.github.com/users/FredKSchott/received_events","type":"User","site_admin":false},"body":"- [X] CHANGELOG.md has been updated\r\n- [X] Fixes #627 #634\r\n\r\n/cc @rictic @valdrinkoshi @azakus ","created_at":"2017-03-28T23:14:33Z","updated_at":"2017-03-31T20:11:12Z","closed_at":null,"merged_at":null,"merge_commit_sha":"79a17cf15e946d1dfa647a718f1759a6d5f9fb7a","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/Polymer/polymer-cli/pulls/635/commits","review_comments_url":"https://api.github.com/repos/Polymer/polymer-cli/pulls/635/comments","review_comment_url":"https://api.github.com/repos/Polymer/polymer-cli/pulls/comments{/number}","comments_url":"https://api.github.com/repos/Polymer/polymer-cli/issues/635/comments","statuses_url":"https://api.github.com/repos/Polymer/polymer-cli/statuses/d6e4c60b0b10474e593921ca4ae826a58e1f91c4","head":{"label":"Polymer:es5-webcomponents-loader-polyfill","ref":"es5-webcomponents-loader-polyfill","sha":"d6e4c60b0b10474e593921ca4ae826a58e1f91c4","user":{"login":"Polymer","id":2159051,"avatar_url":"https://avatars0.githubusercontent.com/u/2159051?v=3","gravatar_id":"","url":"https://api.github.com/users/Polymer","html_url":"https://github.com/Polymer","followers_url":"https://api.github.com/users/Polymer/followers","following_url":"https://api.github.com/users/Polymer/following{/other_user}","gists_url":"https://api.github.com/users/Polymer/gists{/gist_id}","starred_url":"https://api.github.com/users/Polymer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Polymer/subscriptions","organizations_url":"https://api.github.com/users/Polymer/orgs","repos_url":"https://api.github.com/users/Polymer/repos","events_url":"https://api.github.com/users/Polymer/events{/privacy}","received_events_url":"https://api.github.com/users/Polymer/received_events","type":"Organization","site_admin":false},"repo":{"id":51951003,"name":"polymer-cli","full_name":"Polymer/polymer-cli","owner":{"login":"Polymer","id":2159051,"avatar_url":"https://avatars0.githubusercontent.com/u/2159051?v=3","gravatar_id":"","url":"https://api.github.com/users/Polymer","html_url":"https://github.com/Polymer","followers_url":"https://api.github.com/users/Polymer/followers","following_url":"https://api.github.com/users/Polymer/following{/other_user}","gists_url":"https://api.github.com/users/Polymer/gists{/gist_id}","starred_url":"https://api.github.com/users/Polymer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Polymer/subscriptions","organizations_url":"https://api.github.com/users/Polymer/orgs","repos_url":"https://api.github.com/users/Polymer/repos","events_url":"https://api.github.com/users/Polymer/events{/privacy}","received_events_url":"https://api.github.com/users/Polymer/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Polymer/polymer-cli","description":"A command-line tool for Polymer projects","fork":false,"url":"https://api.github.com/repos/Polymer/polymer-cli","forks_url":"https://api.github.com/repos/Polymer/polymer-cli/forks","keys_url":"https://api.github.com/repos/Polymer/polymer-cli/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Polymer/polymer-cli/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Polymer/polymer-cli/teams","hooks_url":"https://api.github.com/repos/Polymer/polymer-cli/hooks","issue_events_url":"https://api.github.com/repos/Polymer/polymer-cli/issues/events{/number}","events_url":"https://api.github.com/repos/Polymer/polymer-cli/events","assignees_url":"https://api.github.com/repos/Polymer/polymer-cli/assignees{/user}","branches_url":"https://api.github.com/repos/Polymer/polymer-cli/branches{/branch}","tags_url":"https://api.github.com/repos/Polymer/polymer-cli/tags","blobs_url":"https://api.github.com/repos/Polymer/polymer-cli/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Polymer/polymer-cli/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Polymer/polymer-cli/git/refs{/sha}","trees_url":"https://api.github.com/repos/Polymer/polymer-cli/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Polymer/polymer-cli/statuses/{sha}","languages_url":"https://api.github.com/repos/Polymer/polymer-cli/languages","stargazers_url":"https://api.github.com/repos/Polymer/polymer-cli/stargazers","contributors_url":"https://api.github.com/repos/Polymer/polymer-cli/contributors","subscribers_url":"https://api.github.com/repos/Polymer/polymer-cli/subscribers","subscription_url":"https://api.github.com/repos/Polymer/polymer-cli/subscription","commits_url":"https://api.github.com/repos/Polymer/polymer-cli/commits{/sha}","git_commits_url":"https://api.github.com/repos/Polymer/polymer-cli/git/commits{/sha}","comments_url":"https://api.github.com/repos/Polymer/polymer-cli/comments{/number}","issue_comment_url":"https://api.github.com/repos/Polymer/polymer-cli/issues/comments{/number}","contents_url":"https://api.github.com/repos/Polymer/polymer-cli/contents/{+path}","compare_url":"https://api.github.com/repos/Polymer/polymer-cli/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Polymer/polymer-cli/merges","archive_url":"https://api.github.com/repos/Polymer/polymer-cli/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Polymer/polymer-cli/downloads","issues_url":"https://api.github.com/repos/Polymer/polymer-cli/issues{/number}","pulls_url":"https://api.github.com/repos/Polymer/polymer-cli/pulls{/number}","milestones_url":"https://api.github.com/repos/Polymer/polymer-cli/milestones{/number}","notifications_url":"https://api.github.com/repos/Polymer/polymer-cli/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Polymer/polymer-cli/labels{/name}","releases_url":"https://api.github.com/repos/Polymer/polymer-cli/releases{/id}","deployments_url":"https://api.github.com/repos/Polymer/polymer-cli/deployments","created_at":"2016-02-17T19:57:15Z","updated_at":"2017-03-31T06:30:39Z","pushed_at":"2017-03-31T00:30:39Z","git_url":"git://github.com/Polymer/polymer-cli.git","ssh_url":"git@github.com:Polymer/polymer-cli.git","clone_url":"https://github.com/Polymer/polymer-cli.git","svn_url":"https://github.com/Polymer/polymer-cli","homepage":null,"size":1095,"stargazers_count":285,"watchers_count":285,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":81,"mirror_url":null,"open_issues_count":135,"forks":81,"open_issues":135,"watchers":285,"default_branch":"master"}},"base":{"label":"Polymer:master","ref":"master","sha":"8fe7fb3095a4f6f8b431f58d4357d0bbcd88d1f9","user":{"login":"Polymer","id":2159051,"avatar_url":"https://avatars0.githubusercontent.com/u/2159051?v=3","gravatar_id":"","url":"https://api.github.com/users/Polymer","html_url":"https://github.com/Polymer","followers_url":"https://api.github.com/users/Polymer/followers","following_url":"https://api.github.com/users/Polymer/following{/other_user}","gists_url":"https://api.github.com/users/Polymer/gists{/gist_id}","starred_url":"https://api.github.com/users/Polymer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Polymer/subscriptions","organizations_url":"https://api.github.com/users/Polymer/orgs","repos_url":"https://api.github.com/users/Polymer/repos","events_url":"https://api.github.com/users/Polymer/events{/privacy}","received_events_url":"https://api.github.com/users/Polymer/received_events","type":"Organization","site_admin":false},"repo":{"id":51951003,"name":"polymer-cli","full_name":"Polymer/polymer-cli","owner":{"login":"Polymer","id":2159051,"avatar_url":"https://avatars0.githubusercontent.com/u/2159051?v=3","gravatar_id":"","url":"https://api.github.com/users/Polymer","html_url":"https://github.com/Polymer","followers_url":"https://api.github.com/users/Polymer/followers","following_url":"https://api.github.com/users/Polymer/following{/other_user}","gists_url":"https://api.github.com/users/Polymer/gists{/gist_id}","starred_url":"https://api.github.com/users/Polymer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Polymer/subscriptions","organizations_url":"https://api.github.com/users/Polymer/orgs","repos_url":"https://api.github.com/users/Polymer/repos","events_url":"https://api.github.com/users/Polymer/events{/privacy}","received_events_url":"https://api.github.com/users/Polymer/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/Polymer/polymer-cli","description":"A command-line tool for Polymer projects","fork":false,"url":"https://api.github.com/repos/Polymer/polymer-cli","forks_url":"https://api.github.com/repos/Polymer/polymer-cli/forks","keys_url":"https://api.github.com/repos/Polymer/polymer-cli/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Polymer/polymer-cli/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Polymer/polymer-cli/teams","hooks_url":"https://api.github.com/repos/Polymer/polymer-cli/hooks","issue_events_url":"https://api.github.com/repos/Polymer/polymer-cli/issues/events{/number}","events_url":"https://api.github.com/repos/Polymer/polymer-cli/events","assignees_url":"https://api.github.com/repos/Polymer/polymer-cli/assignees{/user}","branches_url":"https://api.github.com/repos/Polymer/polymer-cli/branches{/branch}","tags_url":"https://api.github.com/repos/Polymer/polymer-cli/tags","blobs_url":"https://api.github.com/repos/Polymer/polymer-cli/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Polymer/polymer-cli/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Polymer/polymer-cli/git/refs{/sha}","trees_url":"https://api.github.com/repos/Polymer/polymer-cli/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Polymer/polymer-cli/statuses/{sha}","languages_url":"https://api.github.com/repos/Polymer/polymer-cli/languages","stargazers_url":"https://api.github.com/repos/Polymer/polymer-cli/stargazers","contributors_url":"https://api.github.com/repos/Polymer/polymer-cli/contributors","subscribers_url":"https://api.github.com/repos/Polymer/polymer-cli/subscribers","subscription_url":"https://api.github.com/repos/Polymer/polymer-cli/subscription","commits_url":"https://api.github.com/repos/Polymer/polymer-cli/commits{/sha}","git_commits_url":"https://api.github.com/repos/Polymer/polymer-cli/git/commits{/sha}","comments_url":"https://api.github.com/repos/Polymer/polymer-cli/comments{/number}","issue_comment_url":"https://api.github.com/repos/Polymer/polymer-cli/issues/comments{/number}","contents_url":"https://api.github.com/repos/Polymer/polymer-cli/contents/{+path}","compare_url":"https://api.github.com/repos/Polymer/polymer-cli/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Polymer/polymer-cli/merges","archive_url":"https://api.github.com/repos/Polymer/polymer-cli/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Polymer/polymer-cli/downloads","issues_url":"https://api.github.com/repos/Polymer/polymer-cli/issues{/number}","pulls_url":"https://api.github.com/repos/Polymer/polymer-cli/pulls{/number}","milestones_url":"https://api.github.com/repos/Polymer/polymer-cli/milestones{/number}","notifications_url":"https://api.github.com/repos/Polymer/polymer-cli/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Polymer/polymer-cli/labels{/name}","releases_url":"https://api.github.com/repos/Polymer/polymer-cli/releases{/id}","deployments_url":"https://api.github.com/repos/Polymer/polymer-cli/deployments","created_at":"2016-02-17T19:57:15Z","updated_at":"2017-03-31T06:30:39Z","pushed_at":"2017-03-31T00:30:39Z","git_url":"git://github.com/Polymer/polymer-cli.git","ssh_url":"git@github.com:Polymer/polymer-cli.git","clone_url":"https://github.com/Polymer/polymer-cli.git","svn_url":"https://github.com/Polymer/polymer-cli","homepage":null,"size":1095,"stargazers_count":285,"watchers_count":285,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":81,"mirror_url":null,"open_issues_count":135,"forks":81,"open_issues":135,"watchers":285,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/Polymer/polymer-cli/pulls/635"},"html":{"href":"https://github.com/Polymer/polymer-cli/pull/635"},"issue":{"href":"https://api.github.com/repos/Polymer/polymer-cli/issues/635"},"comments":{"href":"https://api.github.com/repos/Polymer/polymer-cli/issues/635/comments"},"review_comments":{"href":"https://api.github.com/repos/Polymer/polymer-cli/pulls/635/comments"},"review_comment":{"href":"https://api.github.com/repos/Polymer/polymer-cli/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/Polymer/polymer-cli/pulls/635/commits"},"statuses":{"href":"https://api.github.com/repos/Polymer/polymer-cli/statuses/d6e4c60b0b10474e593921ca4ae826a58e1f91c4"}}}}
{"actor":{"display_login":"rictic"}}