perf: improve performance by 4x by simplified parseFile

- previously deprecated `getTemplate()` and `getTemplateSync()` not no longer supported
- `opts` no longer support dynamic set in `parseFile()`, `renderFile()` arguments
This commit is contained in:
harttle
2021-09-27 23:04:41 +08:00
parent 6b9f872bcc
commit 24f5346084
14 changed files with 103 additions and 151 deletions
+24 -16
View File
@@ -20,53 +20,61 @@ describe('LiquidOptions#strict*', function () {
})
it('should throw when strictVariables true', function () {
const tpl = engine.parse('before{{notdefined}}after')
const opts = {
engine = new Liquid({
root: '/root/',
extname: '.html',
strictVariables: true
}
return expect(engine.render(tpl, ctx, opts)).to
})
return expect(engine.render(tpl, ctx)).to
.be.rejectedWith(/undefined variable: notdefined/)
})
it('should pass strictVariables to render by parseAndRender', function () {
const html = 'before{{notdefined}}after'
const opts = {
engine = new Liquid({
root: '/root/',
extname: '.html',
strictVariables: true
}
return expect(engine.parseAndRender(html, ctx, opts)).to
})
return expect(engine.parseAndRender(html, ctx)).to
.be.rejectedWith(/undefined variable: notdefined/)
})
describe('with strictVariables and lenientIf', function () {
const strictLenientOpts = {
strictVariables: true,
lenientIf: true
}
beforeEach(() => {
engine = new Liquid({
root: '/root/',
extname: '.html',
strictVariables: true,
lenientIf: true
})
})
it('should not throw in `if` with a single variable', async function () {
const tpl = engine.parse('before{% if notdefined %}{{notdefined}}{% endif %}after')
const html = await engine.render(tpl, ctx, strictLenientOpts)
const html = await engine.render(tpl, ctx)
return expect(html).to.equal('beforeafter')
})
it('should support elsif with undefined variables', async function () {
const tpl = engine.parse('{% if notdefined1 %}a{% elsif notdefined2 %}b{% elsif defined3 %}{{defined3}}{% else %}d{% endif %}')
const html = await engine.render(tpl, { 'defined3': 'bla' }, strictLenientOpts)
const html = await engine.render(tpl, { 'defined3': 'bla' })
return expect(html).to.equal('bla')
})
it('should not throw in `unless` with a single variable', async function () {
const tpl = engine.parse('before{% unless notdefined %}X{% else %}{{notdefined}}{% endunless %}after')
const html = await engine.render(tpl, ctx, strictLenientOpts)
const html = await engine.render(tpl, ctx)
return expect(html).to.equal('beforeXafter')
})
it('should still throw with an undefined variable in a compound `if` expression', function () {
const tpl = engine.parse('{% if notdefined == 15 %}a{% endif %}')
const fhtml = engine.render(tpl, ctx, strictLenientOpts)
const fhtml = engine.render(tpl, ctx)
return expect(fhtml).to.be.rejectedWith(/undefined variable: notdefined/)
})
it('should allow an undefined variable when before the `default` filter', async function () {
const tpl = engine.parse('{{notdefined | default: "a" | tolower}}')
const html = await engine.render(tpl, ctx, strictLenientOpts)
const html = await engine.render(tpl, ctx)
return expect(html).to.equal('a')
})
it('should not allow undefined variable even if `lenientIf` set', async function () {
const tpl = engine.parse('{{notdefined | tolower}}')
return expect(() => engine.renderSync(tpl, ctx, strictLenientOpts)).to.throw('undefined variable: notdefined')
return expect(() => engine.renderSync(tpl, ctx)).to.throw('undefined variable: notdefined')
})
})
})