Skip to content

Commit

Permalink
Proper ES build #261
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasimir Tsonev committed Jan 13, 2021
1 parent a7367d0 commit c13a7d6
Show file tree
Hide file tree
Showing 22 changed files with 163 additions and 343 deletions.
19 changes: 19 additions & 0 deletions .babelrc.es
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"ignore": ["node_modules/**/*"],
"presets": [
["@babel/preset-typescript"],
[
"@babel/preset-env",
{
"loose": true,
"modules": false
}
],
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties",
"babel-plugin-add-module-exports",
"@babel/plugin-transform-classes"
]
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 8.6.1

Proper ES modules build.

## 8.6.0

Introducing `callHooks` flag to the `navigate` options.
Expand Down
13 changes: 3 additions & 10 deletions lib/es/Q.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
"use strict";

exports.__esModule = true;
exports.default = Q;

function Q(funcs, c, done) {
export default function Q(funcs, c, done) {
var context = c || {};
var idx = 0;

Expand Down Expand Up @@ -34,10 +29,8 @@ function Q(funcs, c, done) {
})();
}

Q.if = function (condition, one, two) {
Q["if"] = function (condition, one, two) {
if (!Array.isArray(one)) one = [one];
if (!Array.isArray(two)) two = [two];
return [condition, one, two];
};

module.exports = exports.default;
};
28 changes: 8 additions & 20 deletions lib/es/constants.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
"use strict";

exports.__esModule = true;
exports.MATCH_REGEXP_FLAGS = exports.START_BY_SLASH_REGEXP = exports.REPLACE_NOT_SURE = exports.NOT_SURE_REGEXP = exports.REPLACE_WILDCARD = exports.WILDCARD_REGEXP = exports.REPLACE_VARIABLE_REGEXP = exports.PARAMETER_REGEXP = void 0;
var PARAMETER_REGEXP = /([:*])(\w+)/g;
exports.PARAMETER_REGEXP = PARAMETER_REGEXP;
var REPLACE_VARIABLE_REGEXP = "([^/]+)";
exports.REPLACE_VARIABLE_REGEXP = REPLACE_VARIABLE_REGEXP;
var WILDCARD_REGEXP = /\*/g;
exports.WILDCARD_REGEXP = WILDCARD_REGEXP;
var REPLACE_WILDCARD = "(?:.*)";
exports.REPLACE_WILDCARD = REPLACE_WILDCARD;
var NOT_SURE_REGEXP = /\/\?/g;
exports.NOT_SURE_REGEXP = NOT_SURE_REGEXP;
var REPLACE_NOT_SURE = "/?([^/]+|)";
exports.REPLACE_NOT_SURE = REPLACE_NOT_SURE;
var START_BY_SLASH_REGEXP = "(?:/^|^)";
exports.START_BY_SLASH_REGEXP = START_BY_SLASH_REGEXP;
var MATCH_REGEXP_FLAGS = "";
exports.MATCH_REGEXP_FLAGS = MATCH_REGEXP_FLAGS;
export var PARAMETER_REGEXP = /([:*])(\w+)/g;
export var REPLACE_VARIABLE_REGEXP = "([^/]+)";
export var WILDCARD_REGEXP = /\*/g;
export var REPLACE_WILDCARD = "(?:.*)";
export var NOT_SURE_REGEXP = /\/\?/g;
export var REPLACE_NOT_SURE = "/?([^/]+|)";
export var START_BY_SLASH_REGEXP = "(?:/^|^)";
export var MATCH_REGEXP_FLAGS = "";
94 changes: 38 additions & 56 deletions lib/es/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
"use strict";

exports.__esModule = true;
exports.default = Navigo;

var _utils = require("./utils");

var _Q = _interopRequireDefault(require("./Q"));

var _setLocationPath = _interopRequireDefault(require("./middlewares/setLocationPath"));

var _matchPathToRegisteredRoutes = _interopRequireDefault(require("./middlewares/matchPathToRegisteredRoutes"));

var _checkForDeprecationMethods = _interopRequireDefault(require("./middlewares/checkForDeprecationMethods"));

var _checkForForceOp = _interopRequireDefault(require("./middlewares/checkForForceOp"));

var _updateBrowserURL = _interopRequireDefault(require("./middlewares/updateBrowserURL"));

var _processMatches = _interopRequireDefault(require("./middlewares/processMatches"));

var _lifecycles = require("./lifecycles");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function Navigo(appRoute, resolveOptions) {
import { pushStateAvailable, matchRoute, parseQuery, extractGETParameters, isFunction, isString, clean, parseNavigateOptions, windowAvailable, getCurrentEnvURL, accumulateHooks } from "./utils";
import Q from "./Q";
import setLocationPath from "./middlewares/setLocationPath";
import matchPathToRegisteredRoutes from "./middlewares/matchPathToRegisteredRoutes";
import checkForDeprecationMethods from "./middlewares/checkForDeprecationMethods";
import checkForForceOp from "./middlewares/checkForForceOp";
import updateBrowserURL from "./middlewares/updateBrowserURL";
import processMatches from "./middlewares/processMatches";
import { notFoundLifeCycle } from "./lifecycles";
export default function Navigo(appRoute, resolveOptions) {
var DEFAULT_RESOLVE_OPTIONS = resolveOptions || {
strategy: "ONE",
hash: false,
Expand All @@ -35,13 +19,13 @@ function Navigo(appRoute, resolveOptions) {
var routes = [];
var destroyed = false;
var genericHooks;
var isPushStateAvailable = (0, _utils.pushStateAvailable)();
var isWindowAvailable = (0, _utils.windowAvailable)();
var isPushStateAvailable = pushStateAvailable();
var isWindowAvailable = windowAvailable();

if (!appRoute) {
console.warn('Navigo requires a root path in its constructor. If not provided will use "/" as default.');
} else {
root = (0, _utils.clean)(appRoute);
root = clean(appRoute);
}

function _checkForAHash(url) {
Expand All @@ -57,16 +41,16 @@ function Navigo(appRoute, resolveOptions) {
}

function composePathWithRoot(path) {
return (0, _utils.clean)(root + "/" + (0, _utils.clean)(path));
return clean(root + "/" + clean(path));
}

function createRoute(path, handler, hooks, name) {
path = (0, _utils.isString)(path) ? composePathWithRoot(path) : path;
path = isString(path) ? composePathWithRoot(path) : path;
return {
name: name || (0, _utils.clean)(String(path)),
name: name || clean(String(path)),
path: path,
handler: handler,
hooks: (0, _utils.accumulateHooks)(hooks)
hooks: accumulateHooks(hooks)
};
} // public APIs

Expand Down Expand Up @@ -104,34 +88,34 @@ function Navigo(appRoute, resolveOptions) {
navigateOptions: {},
resolveOptions: options || DEFAULT_RESOLVE_OPTIONS
};
(0, _Q.default)([_setLocationPath.default, _matchPathToRegisteredRoutes.default, _Q.default.if(function (_ref) {
Q([setLocationPath, matchPathToRegisteredRoutes, Q["if"](function (_ref) {
var matches = _ref.matches;
// console.log(`${currentLocationPath} -> Matches: ${matches.length}`);
return matches && matches.length > 0;
}, _processMatches.default, _lifecycles.notFoundLifeCycle)], context);
}, processMatches, notFoundLifeCycle)], context);
return context.matches ? context.matches : false;
}

function navigate(to, navigateOptions) {
to = (0, _utils.clean)(root) + "/" + (0, _utils.clean)(to);
to = clean(root) + "/" + clean(to);
var context = {
instance: self,
to: to,
navigateOptions: navigateOptions || {},
resolveOptions: navigateOptions && navigateOptions.resolveOptions ? navigateOptions.resolveOptions : DEFAULT_RESOLVE_OPTIONS,
currentLocationPath: _checkForAHash(to)
};
(0, _Q.default)([_checkForDeprecationMethods.default, _checkForForceOp.default, _matchPathToRegisteredRoutes.default, _Q.default.if(function (_ref2) {
Q([checkForDeprecationMethods, checkForForceOp, matchPathToRegisteredRoutes, Q["if"](function (_ref2) {
var matches = _ref2.matches;
return matches && matches.length > 0;
}, _processMatches.default, _lifecycles.notFoundLifeCycle), _updateBrowserURL.default], context);
}, processMatches, notFoundLifeCycle), updateBrowserURL], context);
}

function off(what) {
this.routes = routes = routes.filter(function (r) {
if ((0, _utils.isString)(what)) {
return (0, _utils.clean)(r.path) !== (0, _utils.clean)(what);
} else if ((0, _utils.isFunction)(what)) {
if (isString(what)) {
return clean(r.path) !== clean(what);
} else if (isFunction(what)) {
return what !== r.handler;
}

Expand Down Expand Up @@ -198,12 +182,12 @@ function Navigo(appRoute, resolveOptions) {
} catch (err) {}
}

var options = (0, _utils.parseNavigateOptions)(link.getAttribute("data-navigo-options"));
var options = parseNavigateOptions(link.getAttribute("data-navigo-options"));

if (!destroyed) {
e.preventDefault();
e.stopPropagation();
self.navigate((0, _utils.clean)(location), options);
self.navigate(clean(location), options);
}
};

Expand All @@ -222,7 +206,7 @@ function Navigo(appRoute, resolveOptions) {
}

function link(path) {
return "/" + root + "/" + (0, _utils.clean)(path);
return "/" + root + "/" + clean(path);
}

function setGenericHooks(hooks) {
Expand Down Expand Up @@ -256,11 +240,11 @@ function Navigo(appRoute, resolveOptions) {
}

function pathToMatchObject(path) {
var _extractGETParameters = (0, _utils.extractGETParameters)((0, _utils.clean)(path)),
var _extractGETParameters = extractGETParameters(clean(path)),
url = _extractGETParameters[0],
queryString = _extractGETParameters[1];

var params = queryString === "" ? null : (0, _utils.parseQuery)(queryString);
var params = queryString === "" ? null : parseQuery(queryString);
var route = createRoute(url, function () {}, [genericHooks], url);
return {
url: url,
Expand All @@ -272,7 +256,7 @@ function Navigo(appRoute, resolveOptions) {
}

function getCurrentLocation() {
return pathToMatchObject((0, _utils.clean)((0, _utils.getCurrentEnvURL)(root)).replace(new RegExp("^" + root), ""));
return pathToMatchObject(clean(getCurrentEnvURL(root)).replace(new RegExp("^" + root), ""));
}

function directMatchWithRegisteredRoutes(path) {
Expand All @@ -282,7 +266,7 @@ function Navigo(appRoute, resolveOptions) {
navigateOptions: {},
resolveOptions: DEFAULT_RESOLVE_OPTIONS
};
(0, _matchPathToRegisteredRoutes.default)(context, function () {});
matchPathToRegisteredRoutes(context, function () {});
return context.matches ? context.matches : false;
}

Expand All @@ -291,9 +275,9 @@ function Navigo(appRoute, resolveOptions) {
instance: self,
currentLocationPath: currentLocation
};
(0, _setLocationPath.default)(context, function () {});
path = (0, _utils.clean)(path);
var match = (0, _utils.matchRoute)(context.currentLocationPath, {
setLocationPath(context, function () {});
path = clean(path);
var match = matchRoute(context.currentLocationPath, {
name: path,
path: path,
handler: function handler() {},
Expand Down Expand Up @@ -349,7 +333,7 @@ function Navigo(appRoute, resolveOptions) {
this.hooks = setGenericHooks;

this.extractGETParameters = function (url) {
return (0, _utils.extractGETParameters)(_checkForAHash(url));
return extractGETParameters(_checkForAHash(url));
};

this.lastResolved = lastResolved;
Expand All @@ -364,7 +348,7 @@ function Navigo(appRoute, resolveOptions) {
this.addLeaveHook = addHook.bind(this, "leave");
this.getRoute = getRoute;
this._pathToMatchObject = pathToMatchObject;
this._clean = _utils.clean;
this._clean = clean;
this._checkForAHash = _checkForAHash;

this._setCurrent = function (c) {
Expand All @@ -373,6 +357,4 @@ function Navigo(appRoute, resolveOptions) {

listen.call(this);
updatePageLinks.call(this);
}

module.exports = exports.default;
}
42 changes: 12 additions & 30 deletions lib/es/lifecycles.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
"use strict";

exports.__esModule = true;
exports.notFoundLifeCycle = exports.foundLifecycle = void 0;

var _Q = _interopRequireDefault(require("./Q"));

var _checkForLeaveHook = _interopRequireDefault(require("./middlewares/checkForLeaveHook"));

var _checkForBeforeHook = _interopRequireDefault(require("./middlewares/checkForBeforeHook"));

var _callHandler = _interopRequireDefault(require("./middlewares/callHandler"));

var _checkForAfterHook = _interopRequireDefault(require("./middlewares/checkForAfterHook"));

var _checkForAlreadyHook = _interopRequireDefault(require("./middlewares/checkForAlreadyHook"));

var _checkForNotFoundHandler = _interopRequireDefault(require("./middlewares/checkForNotFoundHandler"));

var _errorOut = _interopRequireDefault(require("./middlewares/errorOut"));

var _flushCurrent = _interopRequireDefault(require("./middlewares/flushCurrent"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var foundLifecycle = [_checkForAlreadyHook.default, _checkForLeaveHook.default, _checkForBeforeHook.default, _callHandler.default, _checkForAfterHook.default];
exports.foundLifecycle = foundLifecycle;
var notFoundLifeCycle = [_checkForNotFoundHandler.default, _Q.default.if(function (_ref) {
import Q from "./Q";
import checkForLeaveHook from "./middlewares/checkForLeaveHook";
import checkForBeforeHook from "./middlewares/checkForBeforeHook";
import callHandler from "./middlewares/callHandler";
import checkForAfterHook from "./middlewares/checkForAfterHook";
import checkForAlreadyHook from "./middlewares/checkForAlreadyHook";
import checkForNotFoundHandler from "./middlewares/checkForNotFoundHandler";
import errorOut from "./middlewares/errorOut";
import flushCurrent from "./middlewares/flushCurrent";
export var foundLifecycle = [checkForAlreadyHook, checkForLeaveHook, checkForBeforeHook, callHandler, checkForAfterHook];
export var notFoundLifeCycle = [checkForNotFoundHandler, Q["if"](function (_ref) {
var notFoundHandled = _ref.notFoundHandled;
return notFoundHandled;
}, foundLifecycle, [_errorOut.default, _checkForLeaveHook.default]), _flushCurrent.default];
exports.notFoundLifeCycle = notFoundLifeCycle;
}, foundLifecycle, [errorOut, checkForLeaveHook]), flushCurrent];
18 changes: 5 additions & 13 deletions lib/es/middlewares/callHandler.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
"use strict";

exports.__esModule = true;
exports.default = callHandler;

var _utils = require("../utils");

function callHandler(context, done) {
if ((0, _utils.undefinedOrTrue)(context.navigateOptions, "updateState")) {
import { undefinedOrTrue } from "../utils";
export default function callHandler(context, done) {
if (undefinedOrTrue(context.navigateOptions, "updateState")) {
context.instance._setCurrent(context.matches);
}

if ((0, _utils.undefinedOrTrue)(context.navigateOptions, "callHandler")) {
if (undefinedOrTrue(context.navigateOptions, "callHandler")) {
context.match.route.handler(context.match);
}

context.instance.updatePageLinks();
done();
}

module.exports = exports.default;
}
Loading

0 comments on commit c13a7d6

Please sign in to comment.