Skip to content

Commit 6f62e73

Browse files
authored
Merge pull request #1273 from erwinmombay/fix-auto-importer
fix auto fetching of "latestVersion" configuration from amphtml repo
2 parents f5728c5 + 13d910f commit 6f62e73

File tree

8 files changed

+29
-25
lines changed

8 files changed

+29
-25
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"jest": "27.0.6",
5656
"jimp": "0.16.1",
5757
"js-beautify": "1.14.0",
58+
"json5": "2.2.0",
5859
"karma": "6.3.4",
5960
"karma-chrome-launcher": "3.1.0",
6061
"karma-jasmine": "4.0.1",

packages/optimizer/build/downloadRuntimeParameters.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const fetch = require('node-fetch');
2020
const fs = require('fs').promises;
2121
const path = require('path');
2222
const fetchRuntimeParameters = require('../lib/fetchRuntimeParameters');
23+
const JSON5 = require('json5');
2324

2425
(async () => {
2526
const runtimeParameters = await fetchRuntimeParameters({
@@ -42,11 +43,28 @@ const fetchRuntimeParameters = require('../lib/fetchRuntimeParameters');
4243

4344
const extensionConfigUrl =
4445
'https://raw.githubusercontent.com/ampproject/amphtml/main/build-system/compile/bundles.config.extensions.json';
45-
const response = await fetch(extensionConfigUrl);
46-
if (!response.ok) {
47-
throw new Error(`Failed downloading ${extensionConfigUrl} with status ${response.status}`);
46+
const latestVersionsUrl = `https://raw.githubusercontent.com/ampproject/amphtml/main/build-system/compile/bundles.legacy-latest-versions.jsonc`;
47+
48+
const responses = await Promise.all([fetch(extensionConfigUrl), fetch(latestVersionsUrl)]);
49+
const [configResponse, latestVersionsConfigResponse] = responses;
50+
if (!configResponse.ok) {
51+
throw new Error(
52+
`Failed downloading ${extensionConfigUrl} with status ${configResponse.status}`
53+
);
54+
}
55+
if (!latestVersionsConfigResponse.ok) {
56+
throw new Error(
57+
`Failed fetching latest component versions from ${latestVersionsUrl} with status: ${latestVersionsConfigResponse.status}`
58+
);
4859
}
49-
const extensionConfig = await response.json();
60+
61+
const extensionConfig = await configResponse.json();
62+
const latestVersionsConfig = JSON5.parse(await latestVersionsConfigResponse.text());
63+
// We add back the "latestVersion" field so that the auto importer
64+
// code knows what "stable" version of the extension to use.
65+
extensionConfig.forEach((entry) => {
66+
entry['latestVersion'] = latestVersionsConfig[entry.name];
67+
});
5068
fs.writeFile(
5169
path.join(__dirname, '../lib/extensionConfig.json'),
5270
JSON.stringify(extensionConfig),

packages/optimizer/lib/extensionConfig.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/optimizer/lib/transformers/AutoExtensionImporter.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,7 @@ class AutoExtensionImporter {
141141
if (!this.componentVersions) {
142142
this.componentVersions = {};
143143
for (const component of params.componentVersions) {
144-
if (Array.isArray(component.version)) {
145-
// If version is an array then we need to find the highest value.
146-
// Note we need to preserve the "stringy"ness of these versions
147-
// so make sure we don't do any coercion or casting of the value
148-
// we assign.
149-
this.componentVersions[component.name] = component.version.reduce((prev, cur) => {
150-
return Number(prev) > Number(cur) ? prev : cur;
151-
});
152-
} else {
153-
this.componentVersions[component.name] =
154-
Number(component.version) >
155-
// Make sure to guard if the entry doesn't exist yet. We default
156-
// comparing to 0.
157-
Number(this.componentVersions[component.name] || 0)
158-
? component.version
159-
: this.componentVersions[component.name];
160-
}
144+
this.componentVersions[component.name] = component.latestVersion;
161145
}
162146
}
163147
if (!this.extensionSpec_) {

packages/optimizer/spec/transformers/valid/AutoExtensionImporter/amp-carousel/expected_output.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<link rel="canonical" href="self.html">
77
<meta name="viewport" content="width=device-width,minimum-scale=1">
88
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
9-
<script async src="https://cdn.ampproject.org/v0.js"></script><script async src="https://cdn.ampproject.org/v0/amp-carousel-0.2.js" custom-element="amp-carousel"></script>
9+
<script async src="https://cdn.ampproject.org/v0.js"></script><script async src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js" custom-element="amp-carousel"></script>
1010
</head>
1111
<body>
1212
<!-- this should only import amp-carousel and not amp-lightbox-gallery -->

packages/optimizer/spec/transformers/valid/AutoExtensionImporter/auto-imports-missing-extensions/expected_output.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<link rel="canonical" href="self.html">
77
<meta name="viewport" content="width=device-width,minimum-scale=1">
88
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
9-
<script async src="https://cdn.ampproject.org/v0.js"></script><script async src="https://cdn.ampproject.org/v0/amp-access-0.1.js" custom-element="amp-access"></script><script async src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js" custom-element="amp-analytics"></script><script async src="https://cdn.ampproject.org/v0/amp-twitter-0.1.js" custom-element="amp-twitter"></script><script async src="https://cdn.ampproject.org/v0/amp-bind-0.1.js" custom-element="amp-bind"></script><script async src="https://cdn.ampproject.org/v0/amp-fx-collection-0.1.js" custom-element="amp-fx-collection"></script><script async src="https://cdn.ampproject.org/v0/amp-video-0.1.js" custom-element="amp-video"></script><script async src="https://cdn.ampproject.org/v0/amp-video-docking-0.1.js" custom-element="amp-video-docking"></script><script async src="https://cdn.ampproject.org/v0/amp-lightbox-gallery-1.0.js" custom-element="amp-lightbox-gallery"></script><script async src="https://cdn.ampproject.org/v0/amp-inputmask-0.1.js" custom-element="amp-inputmask"></script><script async src="https://cdn.ampproject.org/v0/amp-list-0.1.js" custom-element="amp-list"></script><script async src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js" custom-template="amp-mustache"></script>
9+
<script async src="https://cdn.ampproject.org/v0.js"></script><script async src="https://cdn.ampproject.org/v0/amp-access-0.1.js" custom-element="amp-access"></script><script async src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js" custom-element="amp-analytics"></script><script async src="https://cdn.ampproject.org/v0/amp-twitter-0.1.js" custom-element="amp-twitter"></script><script async src="https://cdn.ampproject.org/v0/amp-bind-0.1.js" custom-element="amp-bind"></script><script async src="https://cdn.ampproject.org/v0/amp-fx-collection-0.1.js" custom-element="amp-fx-collection"></script><script async src="https://cdn.ampproject.org/v0/amp-video-0.1.js" custom-element="amp-video"></script><script async src="https://cdn.ampproject.org/v0/amp-video-docking-0.1.js" custom-element="amp-video-docking"></script><script async src="https://cdn.ampproject.org/v0/amp-lightbox-gallery-0.1.js" custom-element="amp-lightbox-gallery"></script><script async src="https://cdn.ampproject.org/v0/amp-inputmask-0.1.js" custom-element="amp-inputmask"></script><script async src="https://cdn.ampproject.org/v0/amp-list-0.1.js" custom-element="amp-list"></script><script async src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js" custom-template="amp-mustache"></script>
1010
<!-- imports amp-access based on JSON config -->
1111
<script id="amp-access" type="application/json">
1212
{

packages/optimizer/spec/transformers/valid/AutoExtensionImporter/uses-non-experimental-versions/expected_output.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<title>My AMP Page</title>
66
<link rel="canonical" href="self.html">
77
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
8-
<script async src="https://cdn.ampproject.org/v0.js"></script><script async src="https://cdn.ampproject.org/v0/amp-timeago-1.0.js" custom-element="amp-timeago"></script><script async src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js" custom-element="amp-youtube"></script><script async src="https://cdn.ampproject.org/v0/amp-date-display-1.0.js" custom-element="amp-date-display"></script><script async src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js" custom-template="amp-mustache"></script>
8+
<script async src="https://cdn.ampproject.org/v0.js"></script><script async src="https://cdn.ampproject.org/v0/amp-timeago-0.1.js" custom-element="amp-timeago"></script><script async src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js" custom-element="amp-youtube"></script><script async src="https://cdn.ampproject.org/v0/amp-date-display-0.1.js" custom-element="amp-date-display"></script><script async src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js" custom-template="amp-mustache"></script>
99
</head>
1010
<body>
1111
<!-- import non-bento component v0.1 instead of experimental bento v1.0 -->

0 commit comments

Comments
 (0)