Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃殌 HTML Template Tag Transform to eliminate poor transpilation issues #19704

Merged
merged 6 commits into from Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,68 @@
/**
* Copyright 2018 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.
*/

const {minify} = require('html-minifier');

function optimizeLiteralOutput(templateLiteral) {
if (templateLiteral.quasis.length !== 1) {
console/* OK */.log('Improperly formatted `html` tagged template literal' +
', more than one template element present.');
return null;
}
return minify(templateLiteral.quasis[0].value.cooked, {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a fancy minifier.

});
}

module.exports = function(babel) {
const {types: t} = babel;

return {
name: 'transform-html-templates',
visitor: {
TaggedTemplateExpression(path) {
if (t.isIdentifier(path.node.tag, {name: 'html'})) {
const template = optimizeLiteralOutput(path.node.quasi);

if (template !== null) {
const outputArguments = [
t.arrayExpression([t.stringLiteral(template)]),
];
path.replaceWith(
t.callExpression(t.identifier('html'), outputArguments)
);
}
} else if (t.isCallExpression(path.node.tag) &&
t.isIdentifier(path.node.tag.callee, {name: 'htmlFor'})) {
const template = optimizeLiteralOutput(path.node.quasi);

if (template !== null) {
const wrapperMethodArguments = path.node.tag.arguments;
const wrapperMethod = t.callExpression(
t.identifier('htmlFor'), wrapperMethodArguments
);
const outputArguments = [
t.arrayExpression([t.stringLiteral(template)]),
];
path.replaceWith(t.callExpression(wrapperMethod, outputArguments));
}
}
},
},
};
};
@@ -0,0 +1,86 @@
/**
* Copyright 2018 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.
*/

console.log(html`html usage`);
console.log(html`html usage
multiline`);
console.log(html`<p active="true">Attribute Quote Removal</p>`);
console.log(html`<p data-order-state="new 'thing'">Impossible to Remove Quotes</p>`);
console.log(html`<p>
<span>html usage</span>
<span>more spans</span>
</p>`);
console.log(html` <p>
<span>html usage</span>
<span>whitespace before entry tag</span>
</p>`);
console.log(html`<p>
<span>html usage</span>
<span>whitespace after entry tag</span>
</p> `);
console.log(html` <p>
<span>html usage</span>
<span>whitespace before and after entry tag</span>
</p> `);
console.log(html`<!-- Test Comment to Remove -->
<p>
<span>html usage</span>
<span>comment before entry tag</span>
</p>`);
console.log(html`<p>
<span>html usage</span>
<span>comment after entry tag</span>
</p><!-- Test Comment to Remove -->`);
console.log(html`<p>
<!-- Test Comment to Remove -->
jridgewell marked this conversation as resolved.
Show resolved Hide resolved
<span>html usage</span>
<span>with comment sibling</span>
</p>`);
console.log(htmlFor(element)`html usage`);
console.log(htmlFor(element)`html usage
multiline`);
console.log(htmlFor(element)`<p active="true">Attribute Quote Removal</p>`);
console.log(htmlFor(element)`<p data-order-state="new 'thing'">Impossible to Remove Quotes</p>`);
console.log(htmlFor(element)`<p>
<span>html usage</span>
<span>more spans</span>
</p>`);
console.log(htmlFor(element)` <p>
<span>html usage</span>
<span>whitespace before entry tag</span>
</p>`);
console.log(htmlFor(element)`<p>
<span>html usage</span>
<span>whitespace after entry tag</span>
</p> `);
console.log(htmlFor(element)` <p>
<span>html usage</span>
<span>whitespace before and after entry tag</span>
</p> `);
console.log(htmlFor(element)`<!-- Test Comment to Remove -->
<p>
<span>html usage</span>
<span>comment before entry tag</span>
</p>`);
console.log(htmlFor(element)`<p>
<span>html usage</span>
<span>comment after entry tag</span>
</p><!-- Test Comment to Remove -->`);
console.log(htmlFor(element)`<p>
jridgewell marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need a test for an element with an attribute using both kinds of quotes (you'll have to use a multi word attribute to get it to persist).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed with c262f65.

<!-- Test Comment to Remove -->
<span>html usage</span>
<span>with comment sibling</span>
</p>`);
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-transform-html-template"]
}
@@ -0,0 +1,37 @@
/**
* Copyright 2018 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.
*/
console.log(html(["html usage"]));
console.log(html(["html usage multiline"]));
console.log(html(["<p active=true>Attribute Quote Removal</p>"]));
console.log(html(["<p data-order-state=\"new 'thing'\">Impossible to Remove Quotes</p>"]));
console.log(html(["<p><span>html usage</span> <span>more spans</span></p>"]));
console.log(html(["<p><span>html usage</span> <span>whitespace before entry tag</span></p>"]));
console.log(html(["<p><span>html usage</span> <span>whitespace after entry tag</span></p>"]));
console.log(html(["<p><span>html usage</span> <span>whitespace before and after entry tag</span></p>"]));
console.log(html(["<p><span>html usage</span> <span>comment before entry tag</span></p>"]));
console.log(html(["<p><span>html usage</span> <span>comment after entry tag</span></p>"]));
console.log(html(["<p><span>html usage</span> <span>with comment sibling</span></p>"]));
console.log(htmlFor(element)(["html usage"]));
console.log(htmlFor(element)(["html usage multiline"]));
console.log(htmlFor(element)(["<p active=true>Attribute Quote Removal</p>"]));
console.log(htmlFor(element)(["<p data-order-state=\"new 'thing'\">Impossible to Remove Quotes</p>"]));
console.log(htmlFor(element)(["<p><span>html usage</span> <span>more spans</span></p>"]));
console.log(htmlFor(element)(["<p><span>html usage</span> <span>whitespace before entry tag</span></p>"]));
console.log(htmlFor(element)(["<p><span>html usage</span> <span>whitespace after entry tag</span></p>"]));
console.log(htmlFor(element)(["<p><span>html usage</span> <span>whitespace before and after entry tag</span></p>"]));
console.log(htmlFor(element)(["<p><span>html usage</span> <span>comment before entry tag</span></p>"]));
console.log(htmlFor(element)(["<p><span>html usage</span> <span>comment after entry tag</span></p>"]));
console.log(htmlFor(element)(["<p><span>html usage</span> <span>with comment sibling</span></p>"]));
@@ -0,0 +1,20 @@
/**
* Copyright 2018 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.
*/

const x = html;
console.log(x`html usage`);
jridgewell marked this conversation as resolved.
Show resolved Hide resolved
const html = htmlFor(element);
console.log(html`html usage`);
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-transform-html-template"]
}
@@ -0,0 +1,19 @@
/**
* Copyright 2018 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.
*/
const x = html;
console.log(x`html usage`);
const html = htmlFor(element);
console.log(html(["html usage"]));
@@ -0,0 +1,22 @@
/**
* Copyright 2018 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.
*/

console.log(html`html ${foo}`);
console.log(html`html ${foo} another`);
console.log(html`html ${foo} another ${bar} thing`);
console.log(htmlFor(element)`htmlFor ${foo}`);
console.log(htmlFor(element)`html ${foo} another`);
console.log(htmlFor(element)`html ${foo} another ${bar} thing`);
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-transform-html-template"]
}
@@ -0,0 +1,21 @@
/**
* Copyright 2018 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.
*/
console.log(html`html ${foo}`);
console.log(html`html ${foo} another`);
console.log(html`html ${foo} another ${bar} thing`);
console.log(htmlFor(element)`htmlFor ${foo}`);
console.log(htmlFor(element)`html ${foo} another`);
console.log(htmlFor(element)`html ${foo} another ${bar} thing`);
@@ -0,0 +1,25 @@
/**
* Copyright 2018 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.
*/

console.log(`template literal`);
const stringReplacement = 'yo';
console.log(`template (${stringReplacement}) literal`);
const numericReplacement = 1;
console.log(`template (${numericReplacement}) literal`);
function test(string) {
return string;
}
console.log(test`string`);
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../../babel-plugin-transform-html-template"]
}
@@ -0,0 +1,26 @@
/**
* Copyright 2018 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.
*/
console.log(`template literal`);
const stringReplacement = 'yo';
console.log(`template (${stringReplacement}) literal`);
const numericReplacement = 1;
console.log(`template (${numericReplacement}) literal`);

function test(string) {
return string;
}

console.log(test`string`);
@@ -0,0 +1,19 @@
/**
* Copyright 2018 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.
*/

const runner = require('@babel/helper-plugin-test-runner').default;

runner(__dirname);
2 changes: 2 additions & 0 deletions build-system/build.conf.js
Expand Up @@ -17,6 +17,8 @@
const defaultPlugins = [
require.resolve(
'./babel-plugins/babel-plugin-transform-amp-asserts'),
require.resolve(
'./babel-plugins/babel-plugin-transform-html-template'),
require.resolve(
'./babel-plugins/babel-plugin-transform-parenthesize-expression'),
];
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -110,6 +110,7 @@
"gulp-watch": "5.0.1",
"gulp-webserver": "0.9.1",
"gzip-size": "5.0.0",
"html-minifier": "3.5.21",
"is-running": "2.1.0",
"jest": "23.6.0",
"jest-dot-reporter": "1.0.7",
Expand Down