Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* compiler: accept partials instead of whole doc * Extract compile() to make it testable. * forgot compile.js file
- Loading branch information
Showing
6 changed files
with
69 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}); | ||
}); | ||
}); |