From 4e82da689e694aff893d228f046eae78a5769cf4 Mon Sep 17 00:00:00 2001 From: harttle Date: Fri, 12 Feb 2021 14:31:54 +0800 Subject: [PATCH] fix: respect `fs` in parser options, for #233 --- rollup.config.ts | 6 +++--- src/liquid-options.ts | 3 +++ src/liquid.ts | 17 ++++++----------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/rollup.config.ts b/rollup.config.ts index a73e82d6f..e0f1a9d79 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -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' }), diff --git a/src/liquid-options.ts b/src/liquid-options.ts index 01dbdadb6..831c97c3e 100644 --- a/src/liquid-options.ts +++ b/src/liquid-options.ts @@ -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; 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, diff --git a/src/liquid.ts b/src/liquid.ts index f34308eae..21405bdfb 100644 --- a/src/liquid.ts +++ b/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' @@ -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) }