mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-14 20:14:39 +00:00
make route handling more explicit
This commit is contained in:
@@ -4,7 +4,7 @@ import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import mkdirp from 'mkdirp';
|
||||
import rimraf from 'rimraf';
|
||||
import * as compilers from './utils/compilers.js';
|
||||
import get_compilers from './get_compilers.js';
|
||||
import create_app from './utils/create_app.js';
|
||||
import generate_asset_cache from './generate_asset_cache.js';
|
||||
|
||||
@@ -28,7 +28,7 @@ export default function build({ dest, dev, entry, src }) {
|
||||
}
|
||||
}
|
||||
|
||||
const { client, server } = compilers.get_compilers(); // TODO refactor
|
||||
const { client, server } = get_compilers();
|
||||
|
||||
client.run((err, client_stats) => {
|
||||
handleErrors(err, client_stats);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import * as path from 'path';
|
||||
import glob from 'glob';
|
||||
|
||||
export default function create_routes(files) {
|
||||
export default function create_routes({ src, files = glob.sync('**/*.+(html|js|mjs)', { cwd: src }) }) {
|
||||
const routes = files
|
||||
.map(file => {
|
||||
if (/(^|\/|\\)_/.test(file)) return;
|
||||
@@ -2,7 +2,7 @@ import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import glob from 'glob';
|
||||
import { create_templates, render } from './templates.js';
|
||||
import * as route_manager from './route_manager.js';
|
||||
import create_routes from './create_routes.js';
|
||||
|
||||
function ensure_array(thing) {
|
||||
return Array.isArray(thing) ? thing : [thing]; // omg webpack what the HELL are you doing
|
||||
@@ -18,6 +18,8 @@ export default function generate_asset_cache({ src, dest, dev, client_info, serv
|
||||
const service_worker = generate_service_worker({ chunk_files, src });
|
||||
const index = generate_index(main_file);
|
||||
|
||||
const routes = create_routes({ src }); // TODO rename update
|
||||
|
||||
if (dev) {
|
||||
fs.writeFileSync(path.join(dest, 'service-worker.js'), service_worker);
|
||||
fs.writeFileSync(path.join(dest, 'index.html'), index);
|
||||
@@ -34,7 +36,8 @@ export default function generate_asset_cache({ src, dest, dev, client_info, serv
|
||||
return lookup;
|
||||
}, {}),
|
||||
|
||||
routes: route_manager.routes.reduce((lookup, route) => {
|
||||
// TODO confusing that `routes` refers to an array *and* a lookup
|
||||
routes: routes.reduce((lookup, route) => {
|
||||
lookup[route.id] = `/client/${ensure_array(client_info.assetsByChunkName[route.id])[0]}`;
|
||||
return lookup;
|
||||
}, {}),
|
||||
@@ -54,10 +57,10 @@ export default function generate_asset_cache({ src, dest, dev, client_info, serv
|
||||
function generate_service_worker({ chunk_files, src }) {
|
||||
const assets = glob.sync('**', { cwd: 'assets', nodir: true });
|
||||
|
||||
route_manager.update({ src }); // TODO refactor
|
||||
const routes = create_routes({ src }); // TODO refactor
|
||||
|
||||
const route_code = `[${
|
||||
route_manager.routes
|
||||
routes
|
||||
.filter(route => route.type === 'page')
|
||||
.map(route => `{ pattern: ${route.pattern} }`)
|
||||
.join(', ')
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import * as path from 'path';
|
||||
import relative from 'require-relative';
|
||||
|
||||
export function get_compilers() {
|
||||
export default function get_compilers() {
|
||||
const webpack = relative('webpack', process.cwd());
|
||||
|
||||
return {
|
||||
@@ -1,11 +1,10 @@
|
||||
import * as route_manager from './route_manager.js';
|
||||
import * as templates from './templates.js';
|
||||
import * as compilers from './utils/compilers.js';
|
||||
|
||||
export { default as build } from './build.js';
|
||||
export { default as export } from './export.js';
|
||||
export { default as generate_asset_cache } from './generate_asset_cache.js';
|
||||
export { default as get_compilers } from './get_compilers.js';
|
||||
export { default as create_routes } from './create_routes.js';
|
||||
export { default as create_app } from './utils/create_app.js';
|
||||
export { default as create_routes } from './utils/create_routes.js';
|
||||
|
||||
export { compilers, route_manager, templates };
|
||||
export { templates };
|
||||
@@ -1,12 +0,0 @@
|
||||
import glob from 'glob';
|
||||
import create_routes from './utils/create_routes.js';
|
||||
|
||||
export let routes;
|
||||
|
||||
export function update({ src }) {
|
||||
routes = create_routes(
|
||||
glob.sync('**/*.+(html|js|mjs)', { cwd: src })
|
||||
);
|
||||
|
||||
return routes;
|
||||
}
|
||||
@@ -1,16 +1,14 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as route_manager from '../route_manager.js';
|
||||
import { create_templates } from '../templates.js';
|
||||
import create_routes from '../create_routes.js';
|
||||
// import { create_templates } from '../templates.js';
|
||||
|
||||
function posixify(file) {
|
||||
return file.replace(/[/\\]/g, '/');
|
||||
}
|
||||
|
||||
function create_app({ src, dev, entry }) {
|
||||
// const { routes } = route_manager;
|
||||
route_manager.update({ src });
|
||||
const { routes } = route_manager;
|
||||
const routes = create_routes({ src });
|
||||
|
||||
function create_client_main() {
|
||||
const template = fs.readFileSync('templates/main.js', 'utf-8');
|
||||
@@ -68,31 +66,31 @@ function create_app({ src, dev, entry }) {
|
||||
create_server_routes();
|
||||
}
|
||||
|
||||
export function start_watching({ src }) {
|
||||
const chokidar = require('chokidar');
|
||||
// export function start_watching({ src }) {
|
||||
// const chokidar = require('chokidar');
|
||||
|
||||
const watch = (glob, callback) => {
|
||||
const watcher = chokidar.watch(glob, {
|
||||
ignoreInitial: true,
|
||||
persistent: false
|
||||
});
|
||||
// const watch = (glob, callback) => {
|
||||
// const watcher = chokidar.watch(glob, {
|
||||
// ignoreInitial: true,
|
||||
// persistent: false
|
||||
// });
|
||||
|
||||
watcher.on('add', callback);
|
||||
watcher.on('change', callback);
|
||||
watcher.on('unlink', callback);
|
||||
};
|
||||
// watcher.on('add', callback);
|
||||
// watcher.on('change', callback);
|
||||
// watcher.on('unlink', callback);
|
||||
// };
|
||||
|
||||
watch('templates/main.js', create_app);
|
||||
// watch('templates/main.js', create_app);
|
||||
|
||||
watch('routes/**/*.+(html|js|mjs)', () => {
|
||||
route_manager.update({ src });
|
||||
create_app();
|
||||
});
|
||||
// watch('routes/**/*.+(html|js|mjs)', () => {
|
||||
// route_manager.update({ src });
|
||||
// create_app();
|
||||
// });
|
||||
|
||||
watch('templates/**.html', () => {
|
||||
create_templates();
|
||||
// TODO reload current page?
|
||||
});
|
||||
}
|
||||
// watch('templates/**.html', () => {
|
||||
// create_templates();
|
||||
// // TODO reload current page?
|
||||
// });
|
||||
// }
|
||||
|
||||
export default create_app;
|
||||
|
||||
Reference in New Issue
Block a user