merge master -> svelte-3, fix various typescript and webpack oddities

This commit is contained in:
Richard Harris
2019-02-01 08:21:45 -05:00
36 changed files with 794 additions and 200 deletions

View File

@@ -38,7 +38,7 @@ const root_props: RootProps = {
export let prefetching: {
href: string;
promise: Promise<{ redirect?: Redirect, data?: any, nullable_depth?: number }>;
promise: Promise<{ redirect?: Redirect, data?: any, nullable_depth?: number, new_segments?: any }>;
} = null;
export function set_prefetching(href, promise) {
prefetching = { href, promise };
@@ -141,10 +141,13 @@ export function navigate(target: Target, id: number, noscroll?: boolean, hash?:
const token = current_token = {};
return loaded.then(({ redirect, data, nullable_depth }) => {
return loaded.then(({ redirect, data, nullable_depth, new_segments }) => {
if (redirect) {
return goto(redirect.location, { replaceState: true });
}
if (new_segments) {
segments = new_segments;
}
render(data, nullable_depth, scroll_history[id], noscroll, hash, token);
if (document.activeElement) document.activeElement.blur();
});
@@ -231,6 +234,10 @@ export function prepare_page(target: Target): Promise<{
segments[changed_from] === new_segments[changed_from]
) changed_from += 1;
if (changed_from === new_segments.length) {
changed_from -= 1;
}
let redirect: Redirect = null;
let error: { statusCode: number, message: Error | string } = null;
@@ -297,11 +304,9 @@ export function prepare_page(target: Target): Promise<{
}
}).then(results => {
if (redirect) {
return { redirect };
return { redirect, new_segments };
}
segments = new_segments;
const get_params = page.parts[page.parts.length - 1].params || (() => ({}));
const params = get_params(target.match);
@@ -316,6 +321,7 @@ export function prepare_page(target: Target): Promise<{
return {
nullable_depth: 0,
new_segments,
data: Object.assign({}, props, {
child: {
component: ErrorComponent,
@@ -329,7 +335,7 @@ export function prepare_page(target: Target): Promise<{
const data = {
path,
child: Object.assign({}, root_props.child, {
segment: segments[0]
segment: new_segments[0]
})
};
if (changed(query, root_props.query)) data.query = query;
@@ -360,10 +366,10 @@ export function prepare_page(target: Target): Promise<{
}
level = level.props.child;
level.segment = segments[i + 1];
level.segment = new_segments[i + 1];
}
return { data, nullable_depth };
return { data, nullable_depth, new_segments };
});
}
@@ -400,4 +406,4 @@ function detach(node: Node) {
function changed(a: Record<string, string | true>, b: Record<string, string | true>) {
return JSON.stringify(a) !== JSON.stringify(b);
}
}

View File

@@ -1,9 +1,9 @@
import * as fs from 'fs';
import * as path from 'path';
import fs from 'fs';
import path from 'path';
import cookie from 'cookie';
import devalue from 'devalue';
import fetch from 'node-fetch';
import { URL, resolve } from 'url';
import URL from 'url';
import * as stores from '../../shared/stores';
import { build_dir, dev, src_dir, IGNORE } from '../placeholders';
import { Manifest, Page, Props, Req, Res } from './types';
@@ -36,6 +36,7 @@ export function get_page_handler(
}
async function handle_page(page: Page, req: Req, res: Res, status = 200, error: Error | string = null) {
const isSWIndexHtml = req.path === '/service-worker-index.html';
const build_info: {
bundler: 'rollup' | 'webpack',
shimport: string | null,
@@ -49,7 +50,7 @@ export function get_page_handler(
// preload main.js and current route
// TODO detect other stuff we can preload? images, CSS, fonts?
let preloaded_chunks = Array.isArray(build_info.assets.main) ? build_info.assets.main : [build_info.assets.main];
if (!error) {
if (!error && !isSWIndexHtml) {
page.parts.forEach(part => {
if (!part) return;
@@ -95,7 +96,7 @@ export function get_page_handler(
preload_error = { statusCode, message };
},
fetch: (url: string, opts?: any) => {
const parsed = new URL(url, `http://127.0.0.1:${process.env.PORT}${req.baseUrl ? req.baseUrl + '/' :''}`);
const parsed = new URL.URL(url, `http://127.0.0.1:${process.env.PORT}${req.baseUrl ? req.baseUrl + '/' :''}`);
if (opts) {
opts = Object.assign({}, opts);
@@ -106,7 +107,7 @@ export function get_page_handler(
);
if (include_cookies) {
if (!opts.headers) opts.headers = {};
opts.headers = Object.assign({}, opts.headers);
const cookies = Object.assign(
{},
@@ -146,17 +147,22 @@ export function get_page_handler(
match = error ? null : page.pattern.exec(req.path);
preloaded = await Promise.all([root_preloaded].concat(page.parts.map(part => {
if (!part) return null;
let toPreload = [root_preloaded];
if (!isSWIndexHtml) {
toPreload = toPreload.concat(page.parts.map(part => {
if (!part) return null;
return part.preload
? part.preload.call(preload_context, {
path: req.path,
query: req.query,
params: part.params ? part.params(match) : {}
})
: {};
})));
return part.preload
? part.preload.call(preload_context, {
path: req.path,
query: req.query,
params: part.params ? part.params(match) : {}
})
: {};
}))
}
preloaded = await Promise.all(toPreload);
} catch (err) {
preload_error = { statusCode: 500, message: err };
preloaded = []; // appease TypeScript
@@ -164,7 +170,7 @@ export function get_page_handler(
try {
if (redirect) {
const location = resolve(req.baseUrl || '/', redirect.location);
const location = URL.resolve(req.baseUrl || '/', redirect.location);
res.statusCode = redirect.statusCode;
res.setHeader('Location', location);
@@ -200,23 +206,29 @@ export function get_page_handler(
});
let level = data.child;
for (let i = 0; i < page.parts.length; i += 1) {
const part = page.parts[i];
if (!part) continue;
if (isSWIndexHtml) {
level.props = Object.assign({}, props, {
params: {}
})
} else {
for (let i = 0; i < page.parts.length; i += 1) {
const part = page.parts[i];
if (!part) continue;
const get_params = part.params || (() => ({}));
const get_params = part.params || (() => ({}));
Object.assign(level, {
component: part.component,
props: Object.assign({}, props, {
params: get_params(match)
}, preloaded[i + 1])
});
Object.assign(level, {
component: part.component,
props: Object.assign({}, props, {
params: get_params(match)
}, preloaded[i + 1])
});
level.props.child = <Props["child"]>{
segment: segments[i + 1]
};
level = level.props.child;
level.props.child = <Props["child"]>{
segment: segments[i + 1]
};
level = level.props.child;
}
}
stores.page.set({
@@ -314,6 +326,12 @@ export function get_page_handler(
return function find_route(req: Req, res: Res, next: () => void) {
if (req[IGNORE]) return next();
if (req.path === '/service-worker-index.html') {
const homePage = pages.find(page => page.pattern.test('/'));
handle_page(homePage, req, res);
return;
}
if (!server_routes.some(route => route.pattern.test(req.path))) {
for (const page of pages) {
if (page.pattern.test(req.path)) {

View File

@@ -1,5 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import fs from 'fs';
import path from 'path';
import { build_dir, dev, manifest, IGNORE } from '../placeholders';
import { Handler, Req, Res, Store } from './types';
import { get_server_route_handler } from './get_server_route_handler';