streamline asset builder middleware a bit
This commit is contained in:
@@ -59,28 +59,29 @@ export class AssetBuilder {
|
|||||||
const extension = extname(file)
|
const extension = extname(file)
|
||||||
|
|
||||||
const stats = await stat(file).catch(() => undefined)
|
const stats = await stat(file).catch(() => undefined)
|
||||||
if (stats?.isFile()) {
|
if (!stats?.isFile()) {
|
||||||
res
|
const transformResult = await this.transform(asset)
|
||||||
.status(200)
|
if (!transformResult) return next()
|
||||||
.type(extension.endsWith("tsx") ? "text/javascript" : extension)
|
|
||||||
.header("Cache-Control", "public, max-age=604800, immutable")
|
await mkdir(dirname(file), { recursive: true })
|
||||||
.sendFile(file)
|
await writeFile(file, transformResult.content)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const transformer of this.transformers) {
|
res
|
||||||
const result = await transformer.transform(asset)
|
.status(200)
|
||||||
if (result) {
|
.type(extension.endsWith("tsx") ? "text/javascript" : extension)
|
||||||
await mkdir(dirname(file), { recursive: true })
|
.header("Cache-Control", "public, max-age=604800, immutable")
|
||||||
await writeFile(file, result.content)
|
.sendFile(file)
|
||||||
return res.type(extension).send(result.content)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
next()
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error)
|
next(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async transform(asset: Asset) {
|
||||||
|
for (const transformer of this.transformers) {
|
||||||
|
const result = await transformer.transform(asset)
|
||||||
|
if (result) return result
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user