mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-12 03:05:12 +00:00
fix some typescript stuff
This commit is contained in:
@@ -16,11 +16,7 @@ function fudge_mtime(file: string) {
|
||||
);
|
||||
}
|
||||
|
||||
function create_app({
|
||||
src,
|
||||
dev,
|
||||
entry
|
||||
}: {
|
||||
function create_app({ src, dev, entry }: {
|
||||
src: string;
|
||||
dev: boolean;
|
||||
entry: { client: string; server: string };
|
||||
|
||||
@@ -4,18 +4,29 @@ import glob from 'glob';
|
||||
import { create_templates, render } from './templates';
|
||||
import create_routes from './create_routes';
|
||||
|
||||
function ensure_array(thing) {
|
||||
function ensure_array(thing: any) {
|
||||
return Array.isArray(thing) ? thing : [thing]; // omg webpack what the HELL are you doing
|
||||
}
|
||||
|
||||
export default function create_assets({ src, dest, dev, client_info, server_info }) {
|
||||
type WebpackInfo = {
|
||||
assetsByChunkName: Record<string, string>;
|
||||
assets: Array<{ name: string }>
|
||||
}
|
||||
|
||||
export default function create_assets({ src, dest, dev, client_info, server_info }: {
|
||||
src: string;
|
||||
dest: string;
|
||||
dev: boolean;
|
||||
client_info: WebpackInfo;
|
||||
server_info: WebpackInfo;
|
||||
}) {
|
||||
create_templates(); // TODO refactor this...
|
||||
|
||||
const main_file = `/client/${ensure_array(client_info.assetsByChunkName.main)[0]}`;
|
||||
|
||||
const chunk_files = client_info.assets.map(chunk => `/client/${chunk.name}`);
|
||||
|
||||
const service_worker = generate_service_worker({ chunk_files, src });
|
||||
const service_worker = generate_service_worker(chunk_files, src);
|
||||
const index = generate_index(main_file);
|
||||
|
||||
const routes = create_routes({ src });
|
||||
@@ -31,13 +42,13 @@ export default function create_assets({ src, dest, dev, client_info, server_info
|
||||
chunk_files,
|
||||
|
||||
main: read(`${dest}${main_file}`),
|
||||
chunks: chunk_files.reduce((lookup, file) => {
|
||||
chunks: chunk_files.reduce((lookup: Record<string, string>, file) => {
|
||||
lookup[file] = read(`${dest}${file}`);
|
||||
return lookup;
|
||||
}, {}),
|
||||
|
||||
// TODO confusing that `routes` refers to an array *and* a lookup
|
||||
routes: routes.reduce((lookup, route) => {
|
||||
routes: routes.reduce((lookup: Record<string, string>, route) => {
|
||||
lookup[route.id] = `/client/${ensure_array(client_info.assetsByChunkName[route.id])[0]}`;
|
||||
return lookup;
|
||||
}, {}),
|
||||
@@ -54,7 +65,7 @@ export default function create_assets({ src, dest, dev, client_info, server_info
|
||||
};
|
||||
}
|
||||
|
||||
function generate_service_worker({ chunk_files, src }) {
|
||||
function generate_service_worker(chunk_files: string[], src: string) {
|
||||
const assets = glob.sync('**', { cwd: 'assets', nodir: true });
|
||||
|
||||
const routes = create_routes({ src });
|
||||
@@ -67,13 +78,13 @@ function generate_service_worker({ chunk_files, src }) {
|
||||
}]`;
|
||||
|
||||
return read('templates/service-worker.js')
|
||||
.replace(/__timestamp__/g, Date.now())
|
||||
.replace(/__timestamp__/g, String(Date.now()))
|
||||
.replace(/__assets__/g, JSON.stringify(assets))
|
||||
.replace(/__shell__/g, JSON.stringify(chunk_files.concat('/index.html')))
|
||||
.replace(/__routes__/g, route_code);
|
||||
}
|
||||
|
||||
function generate_index(main_file) {
|
||||
function generate_index(main_file: string) {
|
||||
return render(200, {
|
||||
styles: '',
|
||||
head: '',
|
||||
@@ -82,6 +93,6 @@ function generate_index(main_file) {
|
||||
});
|
||||
}
|
||||
|
||||
function read(file) {
|
||||
function read(file: string) {
|
||||
return fs.readFileSync(file, 'utf-8');
|
||||
}
|
||||
|
||||
@@ -6,11 +6,24 @@ export default function create_compilers() {
|
||||
|
||||
return {
|
||||
client: webpack(
|
||||
require(path.resolve('webpack.client.config.js'))
|
||||
require(path.resolve('webpack/client.config.js'))
|
||||
),
|
||||
|
||||
server: webpack(
|
||||
require(path.resolve('webpack.server.config.js'))
|
||||
require(path.resolve('webpack/server.config.js'))
|
||||
),
|
||||
|
||||
serviceWorker: webpack(
|
||||
tryRequire(path.resolve('webpack/server.config.js'))
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
function tryRequire(specifier: string) {
|
||||
try {
|
||||
return require(specifier);
|
||||
} catch (err) {
|
||||
if (err.code === 'MODULE_NOT_FOUND') return null;
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,23 @@
|
||||
import * as path from 'path';
|
||||
import glob from 'glob';
|
||||
|
||||
export default function create_routes({ src, files = glob.sync('**/*.+(html|js|mjs)', { cwd: src }) }) {
|
||||
const routes = files
|
||||
.map(file => {
|
||||
type Route = {
|
||||
id: string;
|
||||
type: 'page' | 'route';
|
||||
file: string;
|
||||
pattern: RegExp;
|
||||
test: (url: string) => boolean;
|
||||
exec: (url: string) => Record<string, string>;
|
||||
parts: string[];
|
||||
dynamic: string[];
|
||||
}
|
||||
|
||||
export default function create_routes({ src, files = glob.sync('**/*.+(html|js|mjs)', { cwd: src }) }: {
|
||||
src: string;
|
||||
files?: string[];
|
||||
}) {
|
||||
const routes: Route[] = files
|
||||
.map((file: string) => {
|
||||
if (/(^|\/|\\)_/.test(file)) return;
|
||||
|
||||
const parts = file.replace(/\.(html|js|mjs)$/, '').split('/'); // glob output is always posix-style
|
||||
@@ -34,13 +48,13 @@ export default function create_routes({ src, files = glob.sync('**/*.+(html|js|m
|
||||
|
||||
const pattern = new RegExp(`^${pattern_string}\\/?$`);
|
||||
|
||||
const test = url => pattern.test(url);
|
||||
const test = (url: string) => pattern.test(url);
|
||||
|
||||
const exec = url => {
|
||||
const exec = (url: string) => {
|
||||
const match = pattern.exec(url);
|
||||
if (!match) return;
|
||||
|
||||
const params = {};
|
||||
const params: Record<string, string> = {};
|
||||
dynamic.forEach((param, i) => {
|
||||
params[param] = match[i + 1];
|
||||
});
|
||||
@@ -60,7 +74,7 @@ export default function create_routes({ src, files = glob.sync('**/*.+(html|js|m
|
||||
};
|
||||
})
|
||||
.filter(Boolean)
|
||||
.sort((a, b) => {
|
||||
.sort((a: Route, b: Route) => {
|
||||
let same = true;
|
||||
|
||||
for (let i = 0; true; i += 1) {
|
||||
|
||||
Reference in New Issue
Block a user