mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 05:50:43 -07:00
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:
@@ -76,15 +76,12 @@ describe('tags/render', function () {
|
||||
})
|
||||
|
||||
it('should be able to access globals', async function () {
|
||||
liquid = new Liquid({ root: '/', extname: '.html', globals: { name: 'Harttle' } })
|
||||
mock({
|
||||
'/hash.html': 'InParent: {{name}} {% render "user.html" %}',
|
||||
'/user.html': 'InChild: {{name}}'
|
||||
})
|
||||
const html = await liquid.renderFile('hash.html', {
|
||||
name: 'harttle'
|
||||
}, {
|
||||
globals: { name: 'Harttle' }
|
||||
})
|
||||
const html = await liquid.renderFile('hash', { name: 'harttle' })
|
||||
expect(html).to.equal('InParent: harttle InChild: Harttle')
|
||||
})
|
||||
|
||||
|
||||
@@ -132,34 +132,6 @@ describe('LiquidOptions#cache', function () {
|
||||
const y = await engine.renderFile('foo')
|
||||
expect(y).to.equal('foo')
|
||||
})
|
||||
it('should respect passed in cache=false option', async function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
const x = await engine.renderFile('files/foo')
|
||||
expect(x).to.equal('foo')
|
||||
mock({ '/root/files/foo.html': 'bar' })
|
||||
const y = await engine.renderFile('files/foo')
|
||||
expect(y).to.equal('foo')
|
||||
const z = await engine.renderFile('files/foo', undefined, { cache: false })
|
||||
expect(z).to.equal('bar')
|
||||
})
|
||||
it('should use cache when passing in other options', async function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
const x = await engine.renderFile('files/foo')
|
||||
expect(x).to.equal('foo')
|
||||
mock({ '/root/files/foo.html': 'bar' })
|
||||
const y = await engine.renderFile('files/foo', undefined, { greedy: true })
|
||||
expect(y).to.equal('foo')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#renderFileSync', function () {
|
||||
@@ -202,33 +174,5 @@ describe('LiquidOptions#cache', function () {
|
||||
const y = await engine.renderFile('foo')
|
||||
expect(y).to.equal('foo')
|
||||
})
|
||||
it('should respect passed in cache=false option', async function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
const x = engine.renderFileSync('files/foo')
|
||||
expect(x).to.equal('foo')
|
||||
mock({ '/root/files/foo.html': 'bar' })
|
||||
const y = engine.renderFileSync('files/foo')
|
||||
expect(y).to.equal('foo')
|
||||
const z = engine.renderFileSync('files/foo', undefined, { cache: false })
|
||||
expect(z).to.equal('bar')
|
||||
})
|
||||
it('should use cache when passing in other options', async function () {
|
||||
const engine = new Liquid({
|
||||
root: '/root/',
|
||||
extname: '.html',
|
||||
cache: true
|
||||
})
|
||||
mock({ '/root/files/foo.html': 'foo' })
|
||||
const x = engine.renderFileSync('files/foo')
|
||||
expect(x).to.equal('foo')
|
||||
mock({ '/root/files/foo.html': 'bar' })
|
||||
const y = engine.renderFileSync('files/foo', undefined, { greedy: true })
|
||||
expect(y).to.equal('foo')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -91,7 +91,7 @@ describe('Liquid', function () {
|
||||
root: ['/root/'],
|
||||
extname: '.html'
|
||||
})
|
||||
const tpls = await engine.getTemplate('mocha')
|
||||
const tpls = await engine.parseFileSync('mocha')
|
||||
expect(tpls.length).to.gte(1)
|
||||
expect(tpls[0].token.getText()).to.contain('module.exports')
|
||||
})
|
||||
@@ -126,7 +126,7 @@ describe('Liquid', function () {
|
||||
root: ['/boo', '/root/'],
|
||||
extname: '.html'
|
||||
})
|
||||
return expect(() => engine.getTemplateSync('/not/exist.html'))
|
||||
return expect(() => engine.parseFileSync('/not/exist.html'))
|
||||
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+4
-3
@@ -4,8 +4,8 @@ import { expect } from 'chai'
|
||||
|
||||
export const liquid = new Liquid()
|
||||
|
||||
export function render (src: string, ctx?: object, opts?: LiquidOptions) {
|
||||
return liquid.parseAndRender(src, ctx, opts)
|
||||
export function render (src: string, ctx?: object) {
|
||||
return liquid.parseAndRender(src, ctx)
|
||||
}
|
||||
|
||||
export async function test (src: string, ctx: object | string, dst?: string, opts?: LiquidOptions) {
|
||||
@@ -13,5 +13,6 @@ export async function test (src: string, ctx: object | string, dst?: string, opt
|
||||
dst = ctx as string
|
||||
ctx = {}
|
||||
}
|
||||
return expect(await render(src, ctx as object, opts)).to.equal(dst)
|
||||
const engine = opts ? new Liquid(opts) : liquid
|
||||
return expect(await engine.parseAndRender(src, ctx as object)).to.equal(dst)
|
||||
}
|
||||
|
||||
@@ -210,8 +210,8 @@ describe('util/strftime', function () {
|
||||
expect(t(now, '%#P')).to.equal('PM')
|
||||
})
|
||||
it('should support : flag', () => {
|
||||
const date = new Date('2016-01-04T13:15:23.000Z')
|
||||
date.getTimezoneOffset = () => -480 // suppose we're in +8:00
|
||||
const date = new Date('2016-01-04T13:15:23.000Z');
|
||||
(timezoneOffset as any) = -480 // suppose we're in +8:00
|
||||
expect(t(date, '%:z')).to.equal('+08:00')
|
||||
expect(t(date, '%z')).to.equal('+0800')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user