Skip to content

Commit

Permalink
fix: respect fs in parser options, for #233
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Feb 12, 2021
1 parent e37824f commit 4e82da6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 14 deletions.
6 changes: 3 additions & 3 deletions rollup.config.ts
Expand Up @@ -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'
}),
Expand Down Expand Up @@ -95,7 +95,7 @@ const browserUmd = {
}],
plugins: [
replace({
include: './src/liquid.ts',
include: './src/liquid-options.ts',
delimiters: ['', ''],
'./fs/node': './fs/browser'
}),
Expand Down Expand Up @@ -123,7 +123,7 @@ const browserMin = {
}],
plugins: [
replace({
include: './src/liquid.ts',
include: './src/liquid-options.ts',
delimiters: ['', ''],
'./fs/node': './fs/browser'
}),
Expand Down
3 changes: 3 additions & 0 deletions src/liquid-options.ts
Expand Up @@ -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'

Expand Down Expand Up @@ -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;
Expand All @@ -88,6 +90,7 @@ export const defaultOptions: NormalizedFullOptions = {
root: ['.'],
cache: undefined,
extname: '',
fs: fs,
dynamicPartials: true,
jsTruthy: false,
trimTagRight: false,
Expand Down
17 changes: 6 additions & 11 deletions src/liquid.ts
@@ -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'
Expand All @@ -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'

Expand All @@ -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()

Expand Down Expand Up @@ -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)
}

Expand All @@ -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
}
Expand All @@ -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)
}

Expand Down

0 comments on commit 4e82da6

Please sign in to comment.