feat: with & for in render tag, closes #195

This commit is contained in:
harttle
2020-03-04 07:08:54 +08:00
parent aa27a6cd7b
commit 6ea6881f08
67 changed files with 1108 additions and 724 deletions
+5 -8
View File
@@ -1,5 +1,4 @@
import { last } from '../util/underscore'
import IFS from './ifs'
function domResolve (root: string, path: string) {
const base = document.createElement('base')
@@ -16,7 +15,7 @@ function domResolve (root: string, path: string) {
return resolved
}
function resolve (root: string, filepath: string, ext: string) {
export function resolve (root: string, filepath: string, ext: string) {
if (root.length && last(root) !== '/') root += '/'
const url = domResolve(root, filepath)
return url.replace(/^(\w+:\/\/[^/]+)(\/[^?]+)/, (str, origin, path) => {
@@ -26,7 +25,7 @@ function resolve (root: string, filepath: string, ext: string) {
})
}
async function readFile (url: string): Promise<string> {
export async function readFile (url: string): Promise<string> {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.onload = () => {
@@ -44,7 +43,7 @@ async function readFile (url: string): Promise<string> {
})
}
function readFileSync (url: string): string {
export function readFileSync (url: string): string {
const xhr = new XMLHttpRequest()
xhr.open('GET', url, false)
xhr.send()
@@ -54,12 +53,10 @@ function readFileSync (url: string): string {
return xhr.responseText as string
}
async function exists () {
export async function exists (filepath: string) {
return true
}
function existsSync () {
export function existsSync (filepath: string) {
return true
}
export default { readFile, resolve, exists, existsSync, readFileSync } as IFS
+1 -1
View File
@@ -1,4 +1,4 @@
export default interface IFS {
export interface FS {
exists: (filepath: string) => Promise<boolean>;
readFile: (filepath: string) => Promise<string>;
existsSync: (filepath: string) => boolean;
+27 -32
View File
@@ -1,38 +1,33 @@
import * as _ from '../util/underscore'
import { resolve, extname } from 'path'
import { stat, statSync, readFile, readFileSync } from 'fs'
import IFS from './ifs'
import { resolve as nodeResolve, extname } from 'path'
import { stat, statSync, readFile as nodeReadFile, readFileSync as nodeReadFileSync } from 'fs'
const statAsync = _.promisify(stat)
const readFileAsync = _.promisify<string, string, string>(readFile)
const readFileAsync = _.promisify<string, string, string>(nodeReadFile)
const fs: IFS = {
exists: (filepath: string) => {
return statAsync(filepath).then(() => true).catch(() => false)
},
readFile: filepath => {
return readFileAsync(filepath, 'utf8')
},
existsSync: (filepath: string) => {
try {
statSync(filepath)
return true
} catch (err) {
return false
}
},
readFileSync: filepath => {
return readFileSync(filepath, 'utf8')
},
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) {}
export function exists (filepath: string) {
return statAsync(filepath).then(() => true).catch(() => false)
}
export function readFile (filepath: string) {
return readFileAsync(filepath, 'utf8')
}
export function existsSync (filepath: string) {
try {
statSync(filepath)
return true
} catch (err) {
return false
}
}
export default fs
export function readFileSync (filepath: string) {
return nodeReadFileSync(filepath, 'utf8')
}
export function resolve (root: string, file: string, ext: string) {
if (!extname(file)) file += ext
return nodeResolve(root, file)
}
export function fallback (file: string) {
try {
return require.resolve(file)
} catch (e) {}
}