Skip to content

Commit

Permalink
refactor: move types for webpack 4
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Aug 25, 2022
1 parent 0cf54de commit 67571bb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 70 deletions.
4 changes: 2 additions & 2 deletions build/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ module.exports = [
},
{
input: resolve('src/composables/index.js'),
file: resolve('./dist/composables.mjs'),
file: resolve('./composables.mjs'),
format: 'es'
},
{
input: resolve('src/composables/index.js'),
file: resolve('./dist/composables.js'),
file: resolve('./composables.js'),
format: 'cjs'
}
].map(genConfig)
Expand Down
134 changes: 69 additions & 65 deletions dist/composables.js → composables.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,108 +3,108 @@
* (c) 2022 Evan You
* @license MIT
*/
'use strict';
'use strict'

Object.defineProperty(exports, '__esModule', { value: true });
Object.defineProperty(exports, '__esModule', { value: true })

var vue = require('vue');
var vue = require('vue')

// dev only warn if no current instance

function throwNoCurrentInstance (method) {
if (!vue.getCurrentInstance()) {
throw new Error(
("[vue-router]: Missing current instance. " + method + "() must be called inside <script setup> or setup().")
('[vue-router]: Missing current instance. ' + method + '() must be called inside <script setup> or setup().')
)
}
}

function useRouter () {
if (process.env.NODE_ENV !== 'production') {
throwNoCurrentInstance('useRouter');
throwNoCurrentInstance('useRouter')
}

return vue.getCurrentInstance().proxy.$root.$router
}

function useRoute () {
if (process.env.NODE_ENV !== 'production') {
throwNoCurrentInstance('useRoute');
throwNoCurrentInstance('useRoute')
}

var root = vue.getCurrentInstance().proxy.$root;
var root = vue.getCurrentInstance().proxy.$root
if (!root._$route) {
var route = vue.effectScope(true).run(function () { return vue.shallowReactive(Object.assign({}, root.$router.currentRoute)); }
);
root._$route = route;
var route = vue.effectScope(true).run(function () { return vue.shallowReactive(Object.assign({}, root.$router.currentRoute)) }
)
root._$route = route

root.$router.afterEach(function (to) {
Object.assign(route, to);
});
Object.assign(route, to)
})
}

return root._$route
}

function onBeforeRouteUpdate (guard) {
if (process.env.NODE_ENV !== 'production') {
throwNoCurrentInstance('onBeforeRouteUpdate');
throwNoCurrentInstance('onBeforeRouteUpdate')
}

return useFilteredGuard(guard, isUpdateNavigation)
}
function isUpdateNavigation (to, from, depth) {
var toMatched = to.matched;
var fromMatched = from.matched;
var toMatched = to.matched
var fromMatched = from.matched
return (
toMatched.length >= depth &&
toMatched
.slice(0, depth + 1)
.every(function (record, i) { return record === fromMatched[i]; })
.every(function (record, i) { return record === fromMatched[i] })
)
}

function isLeaveNavigation (to, from, depth) {
var toMatched = to.matched;
var fromMatched = from.matched;
var toMatched = to.matched
var fromMatched = from.matched
return toMatched.length < depth || toMatched[depth] !== fromMatched[depth]
}

function onBeforeRouteLeave (guard) {
if (process.env.NODE_ENV !== 'production') {
throwNoCurrentInstance('onBeforeRouteLeave');
throwNoCurrentInstance('onBeforeRouteLeave')
}

return useFilteredGuard(guard, isLeaveNavigation)
}

var noop = function () {};
var noop = function () {}
function useFilteredGuard (guard, fn) {
var instance = vue.getCurrentInstance();
var router = useRouter();
var instance = vue.getCurrentInstance()
var router = useRouter()

var target = instance.proxy;
var target = instance.proxy
// find the nearest RouterView to know the depth
while (
target &&
target.$vnode &&
target.$vnode.data &&
target.$vnode.data.routerViewDepth == null
) {
target = target.$parent;
target = target.$parent
}

var depth =
target && target.$vnode && target.$vnode.data
? target.$vnode.data.routerViewDepth
: null;
: null

if (depth != null) {
var removeGuard = router.beforeEach(function (to, from, next) {
return fn(to, from, depth) ? guard(to, from, next) : next()
});
})

vue.onUnmounted(removeGuard);
vue.onUnmounted(removeGuard)
return removeGuard
}

Expand All @@ -122,37 +122,37 @@ function guardEvent (e) {
if (e.button !== undefined && e.button !== 0) { return }
// don't redirect if `target="_blank"`
if (e.currentTarget && e.currentTarget.getAttribute) {
var target = e.currentTarget.getAttribute('target');
var target = e.currentTarget.getAttribute('target')
if (/\b_blank\b/i.test(target)) { return }
}
// this may be a Weex event which doesn't have this method
if (e.preventDefault) {
e.preventDefault();
e.preventDefault()
}
return true
}

function includesParams (outer, inner) {
var loop = function ( key ) {
var innerValue = inner[key];
var outerValue = outer[key];
var loop = function (key) {
var innerValue = inner[key]
var outerValue = outer[key]
if (typeof innerValue === 'string') {
if (innerValue !== outerValue) { return { v: false } }
} else {
if (
!Array.isArray(outerValue) ||
outerValue.length !== innerValue.length ||
innerValue.some(function (value, i) { return value !== outerValue[i]; })
innerValue.some(function (value, i) { return value !== outerValue[i] })
) {
return { v: false }
}
}
};
}

for (var key in inner) {
var returned = loop( key );
var returned = loop(key)

if ( returned ) return returned.v;
if (returned) return returned.v
}

return true
Expand All @@ -170,7 +170,7 @@ function isSameRouteLocationParamsValue (a, b) {

function isEquivalentArray (a, b) {
return Array.isArray(b)
? a.length === b.length && a.every(function (value, i) { return value === b[i]; })
? a.length === b.length && a.every(function (value, i) { return value === b[i] })
: a.length === 1 && a[0] === b
}

Expand All @@ -186,25 +186,25 @@ function isSameRouteLocationParams (a, b) {

function useLink (props) {
if (process.env.NODE_ENV !== 'production') {
throwNoCurrentInstance('useLink');
throwNoCurrentInstance('useLink')
}

var router = useRouter();
var currentRoute = useRoute();
var router = useRouter()
var currentRoute = useRoute()

var resolvedRoute = vue.computed(function () { return router.resolve(vue.unref(props.to), currentRoute); });
var resolvedRoute = vue.computed(function () { return router.resolve(vue.unref(props.to), currentRoute) })

var activeRecordIndex = vue.computed(function () {
var route = resolvedRoute.value.route;
var matched = route.matched;
var length = matched.length;
var routeMatched = matched[length - 1];
var currentMatched = currentRoute.matched;
var route = resolvedRoute.value.route
var matched = route.matched
var length = matched.length
var routeMatched = matched[length - 1]
var currentMatched = currentRoute.matched
if (!routeMatched || !currentMatched.length) { return -1 }
var index = currentMatched.indexOf(routeMatched);
var index = currentMatched.indexOf(routeMatched)
if (index > -1) { return index }
// possible parent record
var parentRecord = currentMatched[currentMatched.length - 2];
var parentRecord = currentMatched[currentMatched.length - 2]

return (
// we are dealing with nested routes
Expand All @@ -214,40 +214,44 @@ function useLink (props) {
// child of the same parent
parentRecord && parentRecord === routeMatched.parent
)
});
})

var isActive = vue.computed(
function () { return activeRecordIndex.value > -1 &&
includesParams(currentRoute.params, resolvedRoute.value.route.params); }
);
function () {
return activeRecordIndex.value > -1 &&
includesParams(currentRoute.params, resolvedRoute.value.route.params)
}
)
var isExactActive = vue.computed(
function () { return activeRecordIndex.value > -1 &&
function () {
return activeRecordIndex.value > -1 &&
activeRecordIndex.value === currentRoute.matched.length - 1 &&
isSameRouteLocationParams(currentRoute.params, resolvedRoute.value.route.params); }
);
isSameRouteLocationParams(currentRoute.params, resolvedRoute.value.route.params)
}
)

var navigate = function (e) {
var href = resolvedRoute.value.route;
var href = resolvedRoute.value.route
if (guardEvent(e)) {
return props.replace
? router.replace(href)
: router.push(href)
}
return Promise.resolve()
};
}

return {
href: vue.computed(function () { return resolvedRoute.value.href; }),
route: vue.computed(function () { return resolvedRoute.value.route; }),
href: vue.computed(function () { return resolvedRoute.value.href }),
route: vue.computed(function () { return resolvedRoute.value.route }),
isExactActive: isExactActive,
isActive: isActive,
navigate: navigate
}
}

exports.isSameRouteLocationParams = isSameRouteLocationParams;
exports.onBeforeRouteLeave = onBeforeRouteLeave;
exports.onBeforeRouteUpdate = onBeforeRouteUpdate;
exports.useLink = useLink;
exports.useRoute = useRoute;
exports.useRouter = useRouter;
exports.isSameRouteLocationParams = isSameRouteLocationParams
exports.onBeforeRouteLeave = onBeforeRouteLeave
exports.onBeforeRouteUpdate = onBeforeRouteUpdate
exports.useLink = useLink
exports.useRoute = useRoute
exports.useRouter = useRouter
File renamed without changes.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"dist/*.js",
"dist/*.mjs",
"types/*.d.ts",
"composables.mjs",
"composables.js",
"composables.d.ts",
"vetur/tags.json",
"vetur/attributes.json"
Expand All @@ -33,9 +35,9 @@
"types": "./types/index.d.ts"
},
"./composables": {
"import": "./dist/composables.mjs",
"require": "./dist/composables.js",
"types": "./types/composables.d.ts"
"import": "./composables.mjs",
"require": "./composables.js",
"types": "./composables.d.ts"
},
"./dist/*": "./dist/*",
"./types/*": "./types/*",
Expand Down

0 comments on commit 67571bb

Please sign in to comment.