Skip to content

Commit

Permalink
Add AutoExtensionImporter (#569)
Browse files Browse the repository at this point in the history
* Add AutoExtensionImporter

* addressed comments

* Adressed comments:

- Support multiple formats
- Add validador-rules dependency to optimizer

* renamed method

* array restructuring ftw

* use configured format instead of default
  • Loading branch information
sebastianbenz authored Jan 16, 2020
1 parent 4d475ff commit e507e9f
Show file tree
Hide file tree
Showing 18 changed files with 559 additions and 16 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/optimizer/lib/AmpConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ module.exports = {
'custom-element': ['amp-geo'],
'custom-template': [],
},
AMP_FORMATS: ['AMP', 'AMP4EMAIL', 'AMP4ADS'],
appendRuntimeVersion: (prefix, version) => prefix + '/rtv/' + version,
};
10 changes: 8 additions & 2 deletions packages/optimizer/lib/DomTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
const treeParser = require('./TreeParser.js');
const log = require('./log.js');
const {oneBehindFetch} = require('@ampproject/toolbox-core');
const validatorRules = require('@ampproject/toolbox-validator-rules');
const runtimeVersion = require('@ampproject/toolbox-runtime-version');

/**
* AMP Optimizer Configuration only applying AMP validity perserving transformations.
*/
const TRANSFORMATIONS_AMP_FIRST = [
// Adds missing AMP extensions
'AutoExtensionImporter',
// Applies server-side-rendering optimizations
'ServerSideRendering',
// Removes the boilerplate
Expand All @@ -47,6 +50,8 @@ const TRANSFORMATIONS_AMP_FIRST = [
* @deprecated
*/
const TRANSFORMATIONS_PAIRED_AMP = [
// Adds missing AMP extensions
'AutoExtensionImporter',
// Adds a link to the valid AMP version
'AddAmpLink',
// Applies server-side-rendering optimizations
Expand All @@ -71,10 +76,11 @@ const TRANSFORMATIONS_PAIRED_AMP = [

const DEFAULT_CONFIG = {
fetch: oneBehindFetch,
log,
runtimeVersion,
log: log,
verbose: false,
transformations: TRANSFORMATIONS_AMP_FIRST,
validatorRules,
verbose: false,
};

/**
Expand Down
47 changes: 47 additions & 0 deletions packages/optimizer/lib/RuntimeHostHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';

const {
AMP_CACHE_HOST,
appendRuntimeVersion,
} = require('./AmpConstants.js');

function calculateHost({
ampUrlPrefix = AMP_CACHE_HOST,
ampRuntimeVersion,
rewriteDynamicComponents,
}) {
if (ampRuntimeVersion && ampUrlPrefix === AMP_CACHE_HOST) {
ampUrlPrefix = appendRuntimeVersion(ampUrlPrefix, ampRuntimeVersion);
}
let dynamicAmpUrlPrefix = ampUrlPrefix;
if (rewriteDynamicComponents === false) {
dynamicAmpUrlPrefix = AMP_CACHE_HOST;
if (ampRuntimeVersion) {
dynamicAmpUrlPrefix = appendRuntimeVersion(dynamicAmpUrlPrefix, ampRuntimeVersion);
}
}
return {
dynamicAmpUrlPrefix,
ampUrlPrefix,
};
}

module.exports = {
calculateHost,
};

Loading

0 comments on commit e507e9f

Please sign in to comment.