-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
365 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
import { readFile } from 'node:fs/promises' | ||
import { readFile, stat } from 'node:fs/promises' | ||
import path from 'node:path'; | ||
|
||
export async function resolve(resource) { | ||
if (/^\w+:\/\//.test(resource)) { | ||
// seems to be an URI, fetch it | ||
/** | ||
* Read a file from the input dir or from the internet. | ||
* @param {string[]} paths | ||
* @returns | ||
*/ | ||
export async function resolve(...paths) { | ||
const last = paths.slice(-1)[0]; | ||
if (/^\w+:\/\//.test(last)) { | ||
// seems to be an URL, fetch it | ||
const resource = last; | ||
const response = await fetch(resource); | ||
const contentType = response.headers.get('Content-Type'); | ||
if (!contentType || !contentType.startsWith('text')) { | ||
return await response.buffer(); | ||
} | ||
return await response.text(); | ||
} | ||
|
||
// otherwise, readFile it. | ||
return await readFile(path.resolve(resource), 'utf8'); | ||
const resource = path.normalize(path.join(...paths)); | ||
const absResource = path.resolve(resource); | ||
if ((await stat(absResource)).isDirectory()) { | ||
return null; | ||
} | ||
return await readFile(absResource, 'utf8'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const SYNTAXES = { | ||
html: /<html-include[\s\r\n]*src="([\w\-\.]+)"[\s\r\n]*\/?>/g, | ||
css: /@import [\"\']([\w:\/\\]+\.css)[\"\'](?: layer\((\w+)\))?;/g, | ||
}; | ||
|
||
/** | ||
* Bundle assets into one file. | ||
* | ||
* @param {string} inputContent | ||
* @param {(resource: string) => Promise<string>} resolve | ||
* @param {'html'|'css'} syntax | ||
* @returns {Promise<string>} return the bundled resource | ||
*/ | ||
async function bundle(inputContent, resolve, syntax, processor) { | ||
const includes = new Map(); | ||
let content = inputContent, matches; | ||
const includePattern = SYNTAXES[syntax]; | ||
|
||
while ((matches = Array.from(content.matchAll(includePattern))).length > 0) { | ||
for (const [, file] of matches) { | ||
|
||
const fullPath = path.join(config.dir.input, config.dir.includes, file); | ||
try { | ||
const content = await resolve(fullPath); | ||
|
||
includes.set(file, await (await processor(content, file)).compile(data)); | ||
} catch (err) { | ||
console.error('error processing file:', fullPath, err); | ||
// silently fail if there is no include | ||
includes.set(file, `<!-- html-include src="${file}" -->`); | ||
} | ||
} | ||
content = content.replace(includePattern, (_, file) => { | ||
return includes.get(file); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.