@ampproject/toolbox-optimizer
Advanced tools
Comparing version 2.8.1 to 2.8.2
@@ -92,2 +92,3 @@ /** | ||
'AmpBoilerplateTransformer', | ||
// Needs to come after AmpBoilerplateTransformer. | ||
'RewriteAmpUrls', | ||
@@ -144,2 +145,9 @@ // Adds amp-onerror to disable boilerplate early | ||
log, | ||
profile: false, | ||
profiler: (label) => { | ||
console.time(label); | ||
return () => { | ||
console.timeEnd(label); | ||
}; | ||
}, | ||
transformations: TRANSFORMATIONS_AMP_FIRST, | ||
@@ -169,7 +177,25 @@ verbose: false, | ||
async transformHtml(html, params) { | ||
const tree = await treeParser.parse(html); | ||
await this.transformTree(tree, params); | ||
return treeParser.serialize(tree); | ||
async function transform() { | ||
const tree = await this.doProfile('parsing', () => treeParser.parse(html)); | ||
await this.doProfile('transform', () => this.transformTree(tree, params)); | ||
return this.doProfile('serialization', () => treeParser.serialize(tree)); | ||
} | ||
return await this.doProfile('overall', () => transform.call(this)); | ||
} | ||
async doProfile(name, f) { | ||
if (!this.config.profile) { | ||
return f(); | ||
} | ||
const endOfTimer = this.config.profiler(name); | ||
try { | ||
return await f(); | ||
} finally { | ||
endOfTimer(); | ||
} | ||
} | ||
/** | ||
@@ -184,9 +210,4 @@ * Transforms a DOM tree. | ||
for (const transformer of this.transformers_) { | ||
if (this.config.profile) { | ||
console.time(this.getTransformerId(transformer)); | ||
} | ||
await transformer.transform(tree, runtimeParameters); | ||
if (this.config.profile) { | ||
console.timeEnd(this.getTransformerId(transformer)); | ||
} | ||
const transformerId = this.getTransformerId(transformer); | ||
await this.doProfile(transformerId, () => transformer.transform(tree, runtimeParameters)); | ||
} | ||
@@ -193,0 +214,0 @@ } |
@@ -20,6 +20,6 @@ /** | ||
/** | ||
* Finds and returns the first 'meta charset' element in the head. | ||
* Finds and returns the first 'meta viewport' element in the head. | ||
* | ||
* @param {Node} head the section to search for the meta charset node. | ||
* @returns {Node} the '<meta charset>' node or null. | ||
* @param {Node} head the section to search for the meta viewport node. | ||
* @returns {Node} the '<meta viewport>' node or null. | ||
*/ | ||
@@ -36,2 +36,17 @@ function findMetaViewport(head) { | ||
/** | ||
* Finds and returns the first runtime script element in the head. | ||
* | ||
* @param {Node} head the section to search for the runtime script node. | ||
* @returns {Node} the runtime script node or null. | ||
*/ | ||
function findRuntimeScript(head) { | ||
for (let node = head.firstChild; node !== null; node = node.nextSibling) { | ||
if (node.tagName === 'script' && node.attribs.src.match(/^https:\/\/.+\/v0(\.js|\.mjs)$/)) { | ||
return node; | ||
} | ||
} | ||
return null; | ||
} | ||
/** | ||
* Skips the subtree that is descending from the current node. | ||
@@ -54,3 +69,4 @@ * @param {Node} the node that has its subtree being skipped | ||
findMetaViewport: findMetaViewport, | ||
findRuntimeScript: findRuntimeScript, | ||
skipNodeAndChildren: skipNodeAndChildren, | ||
}; |
@@ -18,3 +18,9 @@ /** | ||
const {appendChild, createElement, firstChildByTag, insertText} = require('../NodeUtils'); | ||
const { | ||
appendChild, | ||
createElement, | ||
hasAttribute, | ||
firstChildByTag, | ||
insertText, | ||
} = require('../NodeUtils'); | ||
@@ -52,3 +58,3 @@ /** | ||
if (html.attribs['i-amphtml-no-boilerplate'] !== undefined) { | ||
if (hasAttribute(html, 'i-amphtml-no-boilerplate')) { | ||
// Boilerplate was removed, so no need for the amp-onerror handler | ||
@@ -55,0 +61,0 @@ return; |
@@ -25,3 +25,3 @@ /** | ||
} = require('../NodeUtils'); | ||
const {findMetaViewport} = require('../HtmlDomHelper'); | ||
const {findMetaViewport, findRuntimeScript} = require('../HtmlDomHelper'); | ||
const {AMP_FORMATS, AMP_CACHE_HOST} = require('../AmpConstants'); | ||
@@ -36,6 +36,3 @@ | ||
// need to be defined manually here. | ||
const manualAttributeToExtensionMapping = new Map([ | ||
['mask', 'amp-inputmask'], | ||
['lightbox', 'amp-lightbox-gallery'], | ||
]); | ||
const manualAttributeToExtensionMapping = new Map([['lightbox', 'amp-lightbox-gallery']]); | ||
const manualExtensions = Array.from(manualAttributeToExtensionMapping.values()); | ||
@@ -187,3 +184,6 @@ | ||
// We use this for adding new import elements to the header | ||
let referenceNode = findMetaViewport(head); | ||
let referenceNode = findRuntimeScript(head); | ||
if (!referenceNode) { | ||
referenceNode = findMetaViewport(head); | ||
} | ||
@@ -310,2 +310,4 @@ // Use cdn.ampproject.org as default, RewriteUrlTransformer will change this in case of self-hosting | ||
allRequiredExtensions.add(node.attribs.template); | ||
} else if (node.tagName === 'input' && hasAttribute(node, 'mask')) { | ||
allRequiredExtensions.add('amp-inputmask'); | ||
} | ||
@@ -312,0 +314,0 @@ } |
@@ -18,3 +18,10 @@ /** | ||
const {createElement, firstChildByTag, insertAfter, insertBefore, remove} = require('../NodeUtils'); | ||
const { | ||
createElement, | ||
hasAttribute, | ||
firstChildByTag, | ||
insertAfter, | ||
insertBefore, | ||
remove, | ||
} = require('../NodeUtils'); | ||
const {AMP_CACHE_HOST} = require('../AmpConstants.js'); | ||
@@ -77,2 +84,3 @@ const {findMetaViewport} = require('../HtmlDomHelper'); | ||
params.esmModulesEnabled = esm; | ||
const preloadEnabled = !hasAttribute(html, 'i-amphtml-no-boilerplate'); | ||
const preloads = []; | ||
@@ -84,4 +92,7 @@ | ||
if (esm) { | ||
preloads.push(this._addEsm(node)); | ||
} else { | ||
const preload = this._addEsm(node, preloadEnabled); | ||
if (preloadEnabled && preload) { | ||
preloads.push(preload); | ||
} | ||
} else if (preloadEnabled) { | ||
preloads.push(this._createPreload(node.attribs.src, 'script')); | ||
@@ -95,3 +106,5 @@ } | ||
node.attribs.href = this._replaceUrl(node.attribs.href, host); | ||
preloads.push(this._createPreload(node.attribs.href, 'style')); | ||
if (preloadEnabled) { | ||
preloads.push(this._createPreload(node.attribs.href, 'style')); | ||
} | ||
} else if ( | ||
@@ -144,6 +157,6 @@ node.tagName === 'link' && | ||
_addEsm(scriptNode) { | ||
_addEsm(scriptNode, preloadEnabled) { | ||
let result = null; | ||
const esmScriptUrl = scriptNode.attribs.src.replace(/\.js$/, '.mjs'); | ||
if (this._shouldPreload(scriptNode.attribs.src)) { | ||
if (preloadEnabled && this._shouldPreload(scriptNode.attribs.src)) { | ||
const preload = createElement('link', { | ||
@@ -150,0 +163,0 @@ as: 'script', |
{ | ||
"name": "@ampproject/toolbox-optimizer", | ||
"version": "2.8.1", | ||
"version": "2.8.2", | ||
"description": "Server-side rendering for AMPs.", | ||
@@ -51,3 +51,3 @@ "main": "index.js", | ||
"homepage": "https://github.com/ampproject/amp-toolbox/tree/main/packages/optimizer", | ||
"gitHead": "f2c2de8e3699ef107aed1d7a5320ee340495d66e" | ||
"gitHead": "c71c9ad1180b982c1eb00c759b689a58ae5e7bc6" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
251552
5669