feat: support require.resolve for lookup, see #168

This commit is contained in:
harttle
2019-11-07 18:46:37 +08:00
parent 5492fb0b17
commit 2dd43559b5
5 changed files with 17 additions and 5 deletions
+1
View File
@@ -4,4 +4,5 @@ export default interface IFS {
existsSync: (filepath: string) => boolean;
readFileSync: (filepath: string) => string;
resolve: (root: string, file: string, ext: string) => string;
fallback?: (file: string) => string | undefined;
}
+5
View File
@@ -27,6 +27,11 @@ const fs: IFS = {
resolve: (root: string, file: string, ext: string) => {
if (!extname(file)) file += ext
return resolve(root, file)
},
fallback: (file: string) => {
try {
return require.resolve(file)
} catch (e) {}
}
}
+4
View File
@@ -67,6 +67,10 @@ 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 (fs.fallback !== undefined) {
const filepath = fs.fallback(file)
if (filepath !== undefined) paths.push(filepath)
}
for (const filepath of paths) {
if (this.options.cache && this.cache[filepath]) return this.cache[filepath]