Skip to content

Commit

Permalink
compiler: accept partials (#36418)
Browse files Browse the repository at this point in the history
* compiler: accept partials instead of whole doc

* Extract compile() to make it testable.

* forgot compile.js file
  • Loading branch information
samouri committed Oct 20, 2021
1 parent a44b14e commit b7c6219
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 30 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -16,7 +16,7 @@
},
"dependencies": {
"@ampproject/animations": "0.2.2",
"@ampproject/bento-compiler": "0.0.10",
"@ampproject/bento-compiler": "0.0.11",
"@ampproject/toolbox-cache-url": "2.8.0",
"@ampproject/viewer-messaging": "1.1.2",
"@ampproject/worker-dom": "0.32.1",
Expand Down
28 changes: 28 additions & 0 deletions src/compiler/compile.js
@@ -0,0 +1,28 @@
import * as compiler from '@ampproject/bento-compiler';

import {getBuilders} from './builders';

/**
* Returns the AST for an AMP Document with eligible components server-rendered.
*
* @param {import('./types').CompilerRequest} request
* @return {import('./types').CompilerResponse}
*/
export function compile(request) {
if (!request || (!request.document && !request.nodes)) {
throw new Error('Must provide either document or nodes');
}

// TODO(samouri): remove the defaults.
const versions = request.versions ?? [
{component: 'amp-layout', version: 'v0'},
];

const {document, nodes} = request;
if (document) {
return {
document: compiler.renderAstDocument(document, getBuilders(versions)),
};
}
return {nodes: compiler.renderAstNodes(nodes, getBuilders(versions))};
}
24 changes: 1 addition & 23 deletions src/compiler/index.js
@@ -1,26 +1,4 @@
import './polyfills';
import * as compiler from '@ampproject/bento-compiler';

import {getBuilders} from './builders';

/**
* Returns the AST for an AMP Document with eligible components server-rendered.
*
* @param {import('./types').CompilerRequest} request
* @return {import('./types').CompilerResponse}
*/
function compile(request) {
// TODO(samouri): remove the defaults.
const document = request.document ?? {
root: 0,
tree: [{tagid: 92, children: []}],
'quirks_mode': false,
};
const versions = request.versions ?? [
{component: 'amp-layout', version: 'v0'},
];

return {document: compiler.renderAst(document, getBuilders(versions))};
}
import {compile} from './compile';

globalThis['compile'] = compile;
10 changes: 7 additions & 3 deletions src/compiler/types.d.ts
@@ -1,4 +1,4 @@
import {TreeProto} from '@ampproject/bento-compiler';
import {TreeProto, NodeProto} from '@ampproject/bento-compiler';

/**
* AMP Components must implement this "buildDom" function in order to be server-rendered.
Expand All @@ -18,8 +18,12 @@ export type BuildDom = (Element) => void;
*/
export type Versions = Array<{component: string; version: string}>;

export type CompilerRequest = {document: TreeProto; versions: Versions};
export type CompilerRequest = {
document?: TreeProto;
nodes?: NodeProto[];
versions: Versions;
};

export type CompilerResponse = {document: TreeProto};
export type CompilerResponse = {document: TreeProto} | {nodes: NodeProto[]};

export type BuilderMap = {[tagName: string]: BuildDom};
29 changes: 29 additions & 0 deletions test/unit/compiler/test-compile.js
@@ -0,0 +1,29 @@
import {compile} from '#compiler/compile';

describes.sandboxed('compile', {}, () => {
it('should throw if provided invalid input', () => {
const errorMsg = /Must provide either document or nodes/;

expect(() => compile()).throw(errorMsg);
expect(() => compile({})).throw(errorMsg);
expect(() => compile({unknown: {}})).throw(errorMsg);
expect(() => compile({versions: []})).throw(errorMsg);
});

it('should return compiled document', () => {
const document = {
root: 0,
tree: [{tagid: 92, children: []}],
'quirks_mode': false,
};
expect(compile({document})).to.deep.equal({document});
});

it('should return compiled nodes', () => {
const nodes = [
{tagid: 1, value: 'a', children: [], attributes: []},
{tagid: 7, value: 'b', children: [], attributes: []},
];
expect(compile({nodes})).to.deep.equal({nodes});
});
});

0 comments on commit b7c6219

Please sign in to comment.