@bjornlu/svelte-router
Advanced tools
Comparing version 0.1.1 to 0.1.2
# Changelog | ||
## v0.1.2 - 2020-08-26 | ||
### Fixed | ||
- Route store is always defined even before `initRouter` | ||
- `navigate` will not throw error if called before `initRouter` | ||
- Fix base path detection | ||
## v0.1.1 - 2020-08-25 | ||
@@ -4,0 +12,0 @@ |
@@ -1,3 +0,3 @@ | ||
export const basePath = document.baseURI !== window.location.href | ||
export const basePath = document.getElementsByTagName('base').length > 0 | ||
? document.baseURI.replace(window.location.origin, '') | ||
: '/'; |
export const DUMMY_LOCATION = { | ||
path: '/', | ||
path: '', | ||
search: new URLSearchParams(), | ||
hash: '' | ||
}; |
import { route, routerMode, routerHistory } from './router'; | ||
let routeParams = undefined; | ||
export function navigate(to, replace = false) { | ||
if (routerHistory == null) { | ||
// TODO: Show warning if called before initRouter? | ||
return; | ||
} | ||
if (typeof to === 'number') { | ||
@@ -5,0 +9,0 @@ routerHistory.go(to); |
@@ -11,7 +11,7 @@ import { Readable } from 'svelte/store'; | ||
} | ||
export declare let routerMode: RouterMode; | ||
export declare let routerHistory: RouterHistory; | ||
export declare let routerMode: RouterMode | undefined; | ||
export declare let routerHistory: RouterHistory | undefined; | ||
/** The current route information */ | ||
export declare let route: Readable<Route>; | ||
export declare const route: Readable<Route>; | ||
/** Initialize the global router. Call this before mounting the app. */ | ||
export declare function initRouter(options: RouterOptions): void; |
@@ -1,15 +0,21 @@ | ||
import { derived } from 'svelte/store'; | ||
import { writable } from 'svelte/store'; | ||
import { HashHistory } from './history/hash'; | ||
import { HtmlHistory } from './history/html'; | ||
import { RouteMatcher } from './matcher'; | ||
let inited = false; | ||
export let routerMode; | ||
export let routerHistory; | ||
const writableRoute = writable({ | ||
path: '', | ||
params: {}, | ||
matched: [], | ||
search: new URLSearchParams(), | ||
hash: '' | ||
}); | ||
/** The current route information */ | ||
export let route; | ||
export const route = { subscribe: writableRoute.subscribe }; | ||
/** Initialize the global router. Call this before mounting the app. */ | ||
export function initRouter(options) { | ||
var _a; | ||
if (!inited) { | ||
inited = true; | ||
// Use `routerMode` to check if router is already initialized | ||
if (routerMode == null) { | ||
routerMode = (_a = options.mode) !== null && _a !== void 0 ? _a : 'hash'; | ||
@@ -25,8 +31,8 @@ switch (routerMode) { | ||
const matcher = new RouteMatcher(options.routes); | ||
route = derived(routerHistory.currentLocation, ($currentLocation) => { | ||
routerHistory.currentLocation.subscribe(($currentLocation) => { | ||
var _a, _b; | ||
const matchedRoute = matcher.matchRoute($currentLocation.path); | ||
return Object.assign(Object.assign({}, $currentLocation), { params: (_a = matchedRoute === null || matchedRoute === void 0 ? void 0 : matchedRoute.params) !== null && _a !== void 0 ? _a : {}, matched: (_b = matchedRoute === null || matchedRoute === void 0 ? void 0 : matchedRoute.matched) !== null && _b !== void 0 ? _b : [] }); | ||
writableRoute.set(Object.assign(Object.assign({}, $currentLocation), { params: (_a = matchedRoute === null || matchedRoute === void 0 ? void 0 : matchedRoute.params) !== null && _a !== void 0 ? _a : {}, matched: (_b = matchedRoute === null || matchedRoute === void 0 ? void 0 : matchedRoute.matched) !== null && _b !== void 0 ? _b : [] })); | ||
}); | ||
} | ||
} |
@@ -466,43 +466,2 @@ (function (global, factory) { | ||
} | ||
function derived(stores, fn, initial_value) { | ||
const single = !Array.isArray(stores); | ||
const stores_array = single | ||
? [stores] | ||
: stores; | ||
const auto = fn.length < 2; | ||
return readable(initial_value, (set) => { | ||
let inited = false; | ||
const values = []; | ||
let pending = 0; | ||
let cleanup = noop; | ||
const sync = () => { | ||
if (pending) { | ||
return; | ||
} | ||
cleanup(); | ||
const result = fn(single ? values[0] : values, set); | ||
if (auto) { | ||
set(result); | ||
} | ||
else { | ||
cleanup = is_function(result) ? result : noop; | ||
} | ||
}; | ||
const unsubscribers = stores_array.map((store, i) => subscribe(store, (value) => { | ||
values[i] = value; | ||
pending &= ~(1 << i); | ||
if (inited) { | ||
sync(); | ||
} | ||
}, () => { | ||
pending |= (1 << i); | ||
})); | ||
inited = true; | ||
sync(); | ||
return function stop() { | ||
run_all(unsubscribers); | ||
cleanup(); | ||
}; | ||
}); | ||
} | ||
@@ -572,3 +531,3 @@ /* | ||
const DUMMY_LOCATION = { | ||
path: '/', | ||
path: '', | ||
search: new URLSearchParams(), | ||
@@ -618,3 +577,3 @@ hash: '' | ||
const basePath = document.baseURI !== window.location.href | ||
const basePath = document.getElementsByTagName('base').length > 0 | ||
? document.baseURI.replace(window.location.origin, '') | ||
@@ -747,10 +706,18 @@ : '/'; | ||
let inited = false; | ||
let routerMode; | ||
let routerHistory; | ||
const writableRoute = writable({ | ||
path: '', | ||
params: {}, | ||
matched: [], | ||
search: new URLSearchParams(), | ||
hash: '' | ||
}); | ||
/** The current route information */ | ||
const route = { subscribe: writableRoute.subscribe }; | ||
/** Initialize the global router. Call this before mounting the app. */ | ||
function initRouter(options) { | ||
var _a; | ||
if (!inited) { | ||
inited = true; | ||
// Use `routerMode` to check if router is already initialized | ||
if (routerMode == null) { | ||
routerMode = (_a = options.mode) !== null && _a !== void 0 ? _a : 'hash'; | ||
@@ -766,6 +733,6 @@ switch (routerMode) { | ||
const matcher = new RouteMatcher(options.routes); | ||
exports.route = derived(routerHistory.currentLocation, ($currentLocation) => { | ||
routerHistory.currentLocation.subscribe(($currentLocation) => { | ||
var _a, _b; | ||
const matchedRoute = matcher.matchRoute($currentLocation.path); | ||
return Object.assign(Object.assign({}, $currentLocation), { params: (_a = matchedRoute === null || matchedRoute === void 0 ? void 0 : matchedRoute.params) !== null && _a !== void 0 ? _a : {}, matched: (_b = matchedRoute === null || matchedRoute === void 0 ? void 0 : matchedRoute.matched) !== null && _b !== void 0 ? _b : [] }); | ||
writableRoute.set(Object.assign(Object.assign({}, $currentLocation), { params: (_a = matchedRoute === null || matchedRoute === void 0 ? void 0 : matchedRoute.params) !== null && _a !== void 0 ? _a : {}, matched: (_b = matchedRoute === null || matchedRoute === void 0 ? void 0 : matchedRoute.matched) !== null && _b !== void 0 ? _b : [] })); | ||
}); | ||
@@ -777,2 +744,6 @@ } | ||
function navigate(to, replace = false) { | ||
if (routerHistory == null) { | ||
// TODO: Show warning if called before initRouter? | ||
return; | ||
} | ||
if (typeof to === 'number') { | ||
@@ -797,3 +768,3 @@ routerHistory.go(to); | ||
if (routeParams == null) { | ||
exports.route.subscribe(($route) => { | ||
route.subscribe(($route) => { | ||
routeParams = $route.params; | ||
@@ -1255,3 +1226,3 @@ }); | ||
let $route; | ||
component_subscribe($$self, exports.route, $$value => $$invalidate(8, $route = $$value)); | ||
component_subscribe($$self, route, $$value => $$invalidate(8, $route = $$value)); | ||
var _a, _b; | ||
@@ -1404,3 +1375,3 @@ | ||
let $route; | ||
component_subscribe($$self, exports.route, $$value => $$invalidate(17, $route = $$value)); | ||
component_subscribe($$self, route, $$value => $$invalidate(17, $route = $$value)); | ||
var _a, _b, _c, _d; | ||
@@ -1556,2 +1527,3 @@ | ||
exports.navigate = navigate; | ||
exports.route = route; | ||
@@ -1558,0 +1530,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
{ | ||
"name": "@bjornlu/svelte-router", | ||
"description": "Simple router for Svelte", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"main": "dist/svelte-router.umd.js", | ||
@@ -37,5 +37,5 @@ "module": "dist/index.js", | ||
"test": "yarn test:unit && yarn test:e2e", | ||
"test:unit": "TS_NODE_PROJECT=./tests/tsconfig.json mocha", | ||
"test:e2e": "server-test 'yarn cy:setup' '10001|10002' 'yarn cy:run --record'", | ||
"cy:setup": "rollup -c tests/test-app/rollup.config.js", | ||
"test:unit": "jest", | ||
"test:e2e": "server-test 'yarn cy:setup' '10001|10002|10003' 'yarn cy:run'", | ||
"cy:setup": "rollup -c cypress/test-app/rollup.config.js", | ||
"cy:open": "cypress open", | ||
@@ -57,7 +57,4 @@ "cy:run": "cypress run --headless", | ||
"@rollup/plugin-typescript": "^5.0.2", | ||
"@types/chai": "^4.2.12", | ||
"@types/mocha": "^8.0.3", | ||
"chai": "^4.2.0", | ||
"cypress": "^4.10.0", | ||
"mocha": "^8.1.1", | ||
"jest": "^26.4.2", | ||
"prettier": "^2.0.5", | ||
@@ -71,2 +68,3 @@ "prettier-plugin-svelte": "^1.1.0", | ||
"svelte-preprocess": "^4.0.8", | ||
"ts-jest": "^26.3.0", | ||
"ts-node": "^8.10.2", | ||
@@ -76,2 +74,2 @@ "tslib": "^2.0.0", | ||
} | ||
} | ||
} |
@@ -57,3 +57,3 @@ # Svelte Router | ||
{ | ||
path: '/home', | ||
path: '/', | ||
component: Home | ||
@@ -80,3 +80,3 @@ }, | ||
// Learn more in the Recipes section. | ||
redirect: '/home' | ||
redirect: '/' | ||
}, | ||
@@ -83,0 +83,0 @@ { |
17
80212
1911