mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-15 12:24:47 +00:00
Support being mounted on a path — fixes #180
This commit is contained in:
@@ -10,9 +10,11 @@ import prettyBytes from 'pretty-bytes';
|
||||
import { minify_html } from './utils/minify_html';
|
||||
import { locations } from '../config';
|
||||
|
||||
export async function exporter(export_dir: string) {
|
||||
export async function exporter(export_dir: string, { basepath = '' }) {
|
||||
const build_dir = locations.dest();
|
||||
|
||||
export_dir = path.join(export_dir, basepath);
|
||||
|
||||
// Prep output directory
|
||||
sander.rimrafSync(export_dir);
|
||||
|
||||
@@ -58,38 +60,43 @@ export async function exporter(export_dir: string) {
|
||||
|
||||
console.log(`${clorox.bold.cyan(file)} ${clorox.gray(`(${prettyBytes(body.length)})`)}`);
|
||||
|
||||
sander.writeFileSync(`${export_dir}/${file}`, body);
|
||||
sander.writeFileSync(export_dir, file, body);
|
||||
});
|
||||
|
||||
function handle(url: URL) {
|
||||
if (url.origin !== origin) return;
|
||||
async function handle(url: URL) {
|
||||
const r = await fetch(url.href);
|
||||
const range = ~~(r.status / 100);
|
||||
|
||||
if (seen.has(url.pathname)) return;
|
||||
seen.add(url.pathname);
|
||||
if (range >= 4) {
|
||||
console.log(`${clorox.red(`> Received ${r.status} response when fetching ${url.pathname}`)}`);
|
||||
return;
|
||||
}
|
||||
|
||||
return fetch(url.href)
|
||||
.then(r => {
|
||||
if (r.headers.get('Content-Type') === 'text/html') {
|
||||
return r.text().then((body: string) => {
|
||||
const $ = cheerio.load(body);
|
||||
const hrefs: string[] = [];
|
||||
if (range === 2) {
|
||||
if (r.headers.get('Content-Type') === 'text/html') {
|
||||
const body = await r.text();
|
||||
const $ = cheerio.load(body);
|
||||
const urls: URL[] = [];
|
||||
|
||||
$('a[href]').each((i: number, $a) => {
|
||||
hrefs.push($a.attribs.href);
|
||||
});
|
||||
const base = new URL($('base').attr('href') || '/', url.href);
|
||||
|
||||
return hrefs.reduce((promise, href) => {
|
||||
return promise.then(() => handle(new URL(href, url.href)));
|
||||
}, Promise.resolve());
|
||||
});
|
||||
$('a[href]').each((i: number, $a) => {
|
||||
const url = new URL($a.attribs.href, base.href);
|
||||
|
||||
if (url.origin === origin && !seen.has(url.pathname)) {
|
||||
seen.add(url.pathname);
|
||||
urls.push(url);
|
||||
}
|
||||
});
|
||||
|
||||
for (const url of urls) {
|
||||
await handle(url);
|
||||
}
|
||||
})
|
||||
.catch((err: Error) => {
|
||||
console.log(`${clorox.red(`> Error rendering ${url.pathname}: ${err.message}`)}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ports.wait(port)
|
||||
.then(() => handle(new URL(origin))) // TODO all static routes
|
||||
.then(() => handle(new URL(`/${basepath}`, origin))) // TODO all static routes
|
||||
.then(() => proc.kill());
|
||||
}
|
||||
Reference in New Issue
Block a user