Skip to content

Commit 556be16

Browse files
samourircebulko
andauthored
compiler.js add in build instructions (ampproject#35170)
* bento-compiler: hook up to the compiler lib * Make alias for #compiler, add unit test * add extraGlobs * bump compiler dep * typecheck * remove TODO * fix test * Update src/compiler/types.extern.js Co-authored-by: Ryan Cebulko <[email protected]> * type fixes * rcebulko comments Co-authored-by: Ryan Cebulko <[email protected]>
1 parent ed8188e commit 556be16

File tree

10 files changed

+171
-16
lines changed

10 files changed

+171
-16
lines changed

build-system/compile/bundles.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ exports.jsBundles = {
119119
minifiedDestDir: './dist',
120120
options: {
121121
minifiedName: 'compiler.js',
122+
extraGlobs: ['src/builtins/**/*.js', 'extensions/amp-fit-text/**/*.js'],
122123
},
123124
},
124125
'amp-viewer-host.max.js': {

build-system/compile/sources.js

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ const COMMON_GLOBS = [
5555
'node_modules/web-activities/activity-ports.js',
5656
'node_modules/@ampproject/animations/package.json',
5757
'node_modules/@ampproject/animations/dist/animations.mjs',
58+
'node_modules/@ampproject/bento-compiler/package.json',
59+
'node_modules/@ampproject/bento-compiler/dist/index.js',
5860
'node_modules/@ampproject/toolbox-cache-url/package.json',
5961
'node_modules/@ampproject/toolbox-cache-url/dist/amp-toolbox-cache-url.esm.js',
6062
'node_modules/@ampproject/viewer-messaging/package.json',

package-lock.json

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"dependencies": {
1818
"@ampproject/animations": "0.2.2",
19+
"@ampproject/bento-compiler": "0.0.2",
1920
"@ampproject/toolbox-cache-url": "2.8.0",
2021
"@ampproject/viewer-messaging": "1.1.2",
2122
"@ampproject/worker-dom": "0.30.1",

src/compiler/builders.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import {buildDom as ampLayoutClassic} from '#builtins/amp-layout/amp-layout';
17+
18+
import {buildDom as ampFitTextClassic} from '../../extensions/amp-fit-text/0.1/amp-fit-text';
19+
20+
const builderMap = {
21+
'v0': {
22+
'amp-layout': ampLayoutClassic,
23+
},
24+
'0.1': {
25+
'amp-fit-text': ampFitTextClassic,
26+
},
27+
};
28+
29+
/**
30+
* Returns the set of component builders needed to server-render an AMP Document.
31+
*
32+
* @param {!./types.VersionsDef} versions
33+
* @return {Object<string, !./types.BuildDomDef>} builders
34+
*/
35+
export function getBuilders(versions) {
36+
const builders = {};
37+
38+
for (const tag of Object.keys(versions)) {
39+
const version = versions[tag];
40+
const builder = builderMap?.[version]?.[tag];
41+
if (builder) {
42+
builders[tag] = builder;
43+
}
44+
}
45+
46+
return builders;
47+
}

src/compiler/index.js

+9-15
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,19 @@
1414
* limitations under the License.
1515
*/
1616

17-
/**
18-
* Returns the HTML for an AMP Document with eligible components server-rendered.
19-
*
20-
* @param {string} html
21-
* @return {string}
22-
*/
23-
function compileHtml(html) {
24-
return html;
25-
}
17+
import * as compiler from '@ampproject/bento-compiler';
18+
19+
import {getBuilders} from './builders';
2620

2721
/**
2822
* Returns the AST for an AMP Document with eligible components server-rendered.
2923
*
30-
* @param {*} ast
31-
* @return {*}
24+
* @param {!./types.TreeProtoDef} ast
25+
* @param {!./types.VersionsDef} versions
26+
* @return {!./types.TreeProtoDef}
3227
*/
33-
function compileAst(ast) {
34-
return ast;
28+
function compileAst(ast, versions) {
29+
return compiler.renderAst(ast, getBuilders(versions));
3530
}
3631

37-
globalThis.compileHtml = compileHtml;
38-
globalThis.compileAst = compileAst;
32+
globalThis['compileAst'] = compileAst;

src/compiler/shame.extern.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* @fileoverview The junk-drawer of externs that haven't yet been sorted well.
19+
* Shame! Shame! Shame! Avoid adding to this.
20+
*
21+
* It's okay for some things to start off here, since moving them doesn't
22+
* require any other file changes (unlike real code, which requires updating)
23+
* imports throughout the repo).
24+
*
25+
* @externs
26+
*/
27+
28+
/**
29+
* @type {!./types.BuildDomDef}
30+
*/
31+
var buildDom$$module$src$builtins$amp_layout$amp_layout;
32+
33+
/**
34+
* @type {!./types.BuildDomDef}
35+
*/
36+
var buildDom$$module$extensions$amp_fit_text$0_1$amp_fit_text;
37+
38+
/** @type {{renderAst: function(!./types.TreeProtoDef, Object<string, !./types.BuildDomDef>): !./types.TreeProtoDef}} */
39+
var module$$ampproject$bento_compiler;

src/compiler/types.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,19 @@
2525
* The return value is set to *, so that we may optionally return any DOM nodes
2626
* created during a client-side render. These nodes are often needed for ivars.
2727
*
28-
* @typedef {function(!Element):*} BuildDom
28+
* @typedef {function(!Element):*}
2929
*/
30+
export let BuildDomDef;
31+
32+
/**
33+
* See @ampproject/bento-compiler for the definition.
34+
* @typedef {*}
35+
*/
36+
export let TreeProtoDef;
37+
38+
/**
39+
* Contains component versioning data via a map from tagName --> version.
40+
*
41+
* @typedef {Object<string, string>}}
42+
*/
43+
export let VersionsDef;

test/unit/compiler/test-builders.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import {getBuilders} from '#compiler/builders';
17+
18+
describes.sandboxed('getBuilders', {}, () => {
19+
it('should return an empty list for empty component list', () => {
20+
const components = {};
21+
expect(getBuilders(components)).to.eql({});
22+
});
23+
24+
it('should return eligible builtins when provided them as components', () => {
25+
const components = {'amp-layout': 'v0'};
26+
const builders = getBuilders(components);
27+
expect(builders).have.all.keys(['amp-layout']);
28+
});
29+
30+
it('eligible component with ineligible version is not used', () => {
31+
const components = {'amp-fit-text': '1.0'};
32+
const builders = getBuilders(components);
33+
expect(builders).to.eql({});
34+
});
35+
36+
it('should return eligible components', () => {
37+
const components = {'amp-fit-text': '0.1', 'amp-layout': 'v0'};
38+
const builders = getBuilders(components);
39+
expect(builders).have.all.keys(['amp-layout', 'amp-fit-text']);
40+
});
41+
});

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"#build/*": ["./build/*"],
1111

1212
"#builtins/*": ["./src/builtins/*"],
13+
"#compiler/*": ["./src/compiler/*"],
1314
"#core/*": ["./src/core/*"],
1415
"#experiments/*": ["./src/experiments/*"],
1516
"#inabox/*": ["./src/inabox/*"],

0 commit comments

Comments
 (0)