mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-13 03:10:40 -07:00
fix: respect fs in parser options, for #233
This commit is contained in:
+3
-3
@@ -66,7 +66,7 @@ const browserEsm = {
|
||||
external: ['path', 'fs'],
|
||||
plugins: [
|
||||
replace({
|
||||
include: './src/liquid.ts',
|
||||
include: './src/liquid-options.ts',
|
||||
delimiters: ['', ''],
|
||||
'./fs/node': './fs/browser'
|
||||
}),
|
||||
@@ -95,7 +95,7 @@ const browserUmd = {
|
||||
}],
|
||||
plugins: [
|
||||
replace({
|
||||
include: './src/liquid.ts',
|
||||
include: './src/liquid-options.ts',
|
||||
delimiters: ['', ''],
|
||||
'./fs/node': './fs/browser'
|
||||
}),
|
||||
@@ -123,7 +123,7 @@ const browserMin = {
|
||||
}],
|
||||
plugins: [
|
||||
replace({
|
||||
include: './src/liquid.ts',
|
||||
include: './src/liquid-options.ts',
|
||||
delimiters: ['', ''],
|
||||
'./fs/node': './fs/browser'
|
||||
}),
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Template } from './template/template'
|
||||
import { Cache } from './cache/cache'
|
||||
import { LRU } from './cache/lru'
|
||||
import { FS } from './fs/fs'
|
||||
import * as fs from './fs/node'
|
||||
import { defaultOperators, Operators } from './render/operator'
|
||||
import { createTrie, Trie } from './util/operator-trie'
|
||||
|
||||
@@ -65,6 +66,7 @@ export interface NormalizedFullOptions extends NormalizedOptions {
|
||||
cache: undefined | Cache<Template[]>;
|
||||
jsTruthy: boolean;
|
||||
dynamicPartials: boolean;
|
||||
fs: FS;
|
||||
strictFilters: boolean;
|
||||
strictVariables: boolean;
|
||||
lenientIf: boolean;
|
||||
@@ -88,6 +90,7 @@ export const defaultOptions: NormalizedFullOptions = {
|
||||
root: ['.'],
|
||||
cache: undefined,
|
||||
extname: '',
|
||||
fs: fs,
|
||||
dynamicPartials: true,
|
||||
jsTruthy: false,
|
||||
trimTagRight: false,
|
||||
|
||||
+6
-11
@@ -1,5 +1,4 @@
|
||||
import { Context } from './context/context'
|
||||
import * as fs from './fs/node'
|
||||
import { forOwn, snakeCase } from './util/underscore'
|
||||
import { Template } from './template/template'
|
||||
import { Tokenizer } from './parser/tokenizer'
|
||||
@@ -13,7 +12,6 @@ import { TagMap } from './template/tag/tag-map'
|
||||
import { FilterMap } from './template/filter/filter-map'
|
||||
import { LiquidOptions, normalizeStringArray, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
|
||||
import { FilterImplOptions } from './template/filter/filter-impl-options'
|
||||
import { FS } from './fs/fs'
|
||||
import { toPromise, toValue } from './util/async'
|
||||
import { Emitter } from './render/emitter'
|
||||
|
||||
@@ -25,13 +23,11 @@ export class Liquid {
|
||||
public parser: Parser
|
||||
public filters: FilterMap
|
||||
public tags: TagMap
|
||||
private fs: FS
|
||||
|
||||
public constructor (opts: LiquidOptions = {}) {
|
||||
this.options = applyDefault(normalize(opts))
|
||||
this.parser = new Parser(this)
|
||||
this.renderer = new Render()
|
||||
this.fs = opts.fs || fs
|
||||
this.filters = new FilterMap(this.options.strictFilters, this)
|
||||
this.tags = new TagMap()
|
||||
|
||||
@@ -70,9 +66,9 @@ export class Liquid {
|
||||
|
||||
public * _parseFile (file: string, opts?: LiquidOptions, sync?: boolean) {
|
||||
const options = { ...this.options, ...normalize(opts) }
|
||||
const paths = options.root.map(root => this.fs.resolve(root, file, options.extname))
|
||||
if (this.fs.fallback !== undefined) {
|
||||
const filepath = this.fs.fallback(file)
|
||||
const paths = options.root.map(root => options.fs.resolve(root, file, options.extname))
|
||||
if (options.fs.fallback !== undefined) {
|
||||
const filepath = options.fs.fallback(file)
|
||||
if (filepath !== undefined) paths.push(filepath)
|
||||
}
|
||||
|
||||
@@ -82,8 +78,8 @@ export class Liquid {
|
||||
const tpls = yield cache.read(filepath)
|
||||
if (tpls) return tpls
|
||||
}
|
||||
if (!(sync ? this.fs.existsSync(filepath) : yield this.fs.exists(filepath))) continue
|
||||
const tpl = this.parse(sync ? this.fs.readFileSync(filepath) : yield this.fs.readFile(filepath), filepath)
|
||||
if (!(sync ? options.fs.existsSync(filepath) : yield options.fs.exists(filepath))) continue
|
||||
const tpl = this.parse(sync ? options.fs.readFileSync(filepath) : yield options.fs.readFile(filepath), filepath)
|
||||
if (cache) cache.write(filepath, tpl)
|
||||
return tpl
|
||||
}
|
||||
@@ -100,8 +96,7 @@ export class Liquid {
|
||||
return this.render(templates, ctx, opts)
|
||||
}
|
||||
public renderFileSync (file: string, ctx?: object, opts?: LiquidOptions) {
|
||||
const options = normalize(opts)
|
||||
const templates = this.parseFileSync(file, options)
|
||||
const templates = this.parseFileSync(file, opts)
|
||||
return this.renderSync(templates, ctx, opts)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user