mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 20:30:39 -07:00
feat: support require.resolve for lookup, see #168
This commit is contained in:
+2
-1
@@ -109,7 +109,8 @@
|
|||||||
"@semantic-release/git",
|
"@semantic-release/git",
|
||||||
{
|
{
|
||||||
"assets": [
|
"assets": [
|
||||||
"docs"
|
"docs",
|
||||||
|
"CHANGELOG.md"
|
||||||
],
|
],
|
||||||
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,4 +4,5 @@ export default interface IFS {
|
|||||||
existsSync: (filepath: string) => boolean;
|
existsSync: (filepath: string) => boolean;
|
||||||
readFileSync: (filepath: string) => string;
|
readFileSync: (filepath: string) => string;
|
||||||
resolve: (root: string, file: string, ext: string) => string;
|
resolve: (root: string, file: string, ext: string) => string;
|
||||||
|
fallback?: (file: string) => string | undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,6 +27,11 @@ const fs: IFS = {
|
|||||||
resolve: (root: string, file: string, ext: string) => {
|
resolve: (root: string, file: string, ext: string) => {
|
||||||
if (!extname(file)) file += ext
|
if (!extname(file)) file += ext
|
||||||
return resolve(root, file)
|
return resolve(root, file)
|
||||||
|
},
|
||||||
|
fallback: (file: string) => {
|
||||||
|
try {
|
||||||
|
return require.resolve(file)
|
||||||
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,6 +67,10 @@ export class Liquid {
|
|||||||
public * _parseFile (file: string, opts?: LiquidOptions, sync?: boolean) {
|
public * _parseFile (file: string, opts?: LiquidOptions, sync?: boolean) {
|
||||||
const options = { ...this.options, ...normalize(opts) }
|
const options = { ...this.options, ...normalize(opts) }
|
||||||
const paths = options.root.map(root => this.fs.resolve(root, file, options.extname))
|
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) {
|
for (const filepath of paths) {
|
||||||
if (this.options.cache && this.cache[filepath]) return this.cache[filepath]
|
if (this.options.cache && this.cache[filepath]) return this.cache[filepath]
|
||||||
|
|||||||
@@ -86,13 +86,14 @@ describe('Liquid', function () {
|
|||||||
return expect(engine.parseFile('/not/exist.html')).to
|
return expect(engine.parseFile('/not/exist.html')).to
|
||||||
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||||
})
|
})
|
||||||
it('should throw with lookup list when file not exist', function () {
|
it('should fallback to require.resolve in Node.js', async function () {
|
||||||
const engine = new Liquid({
|
const engine = new Liquid({
|
||||||
root: ['/boo', '/root/'],
|
root: ['/root/'],
|
||||||
extname: '.html'
|
extname: '.html'
|
||||||
})
|
})
|
||||||
return expect(engine.getTemplate('/not/exist.html')).to
|
const tpls = await engine.getTemplate('mocha')
|
||||||
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
expect(tpls.length).to.gte(1)
|
||||||
|
expect(tpls[0].token.raw).to.contain('module.exports')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
describe('#evalValue', function () {
|
describe('#evalValue', function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user