diff --git a/.babelrc b/.babelrc index 7a81beaf06..077cca7bf6 100644 --- a/.babelrc +++ b/.babelrc @@ -1,7 +1,14 @@ { - "stage": 0, - "loose": "all", - "plugins": [ - "dev-expression" - ] + "presets": ["stage-0", "react"], + "plugins": ["dev-expression"], + + "env": { + "cjs": { + "presets": ["es2015-loose"], + "plugins": ["add-module-exports"] + }, + "es": { + "presets": ["es2015-loose-native-modules"] + } + } } diff --git a/modules/PatternUtils.js b/modules/PatternUtils.js index 2d7edec196..acfc143dc6 100644 --- a/modules/PatternUtils.js +++ b/modules/PatternUtils.js @@ -94,37 +94,29 @@ export function matchPattern(pattern, pathname) { } const match = pathname.match(new RegExp(`^${regexpSource}`, 'i')) + if (match == null) { + return null + } + + const matchedPath = match[0] + let remainingPathname = pathname.substr(matchedPath.length) - let remainingPathname, paramValues - if (match != null) { - const matchedPath = match[0] - remainingPathname = pathname.substr(matchedPath.length) - - if (remainingPathname) { - // Require that the match ends at a path separator, if we didn't match - // the full path, so any remaining pathname is a new path segment. - if (matchedPath.charAt(matchedPath.length - 1) !== '/') { - return { - remainingPathname: null, - paramNames, - paramValues: null - } - } - - // If there is a remaining pathname, treat the path separator as part of - // the remaining pathname for properly continuing the match. - remainingPathname = `/${remainingPathname}` + if (remainingPathname) { + // Require that the match ends at a path separator, if we didn't match + // the full path, so any remaining pathname is a new path segment. + if (matchedPath.charAt(matchedPath.length - 1) !== '/') { + return null } - paramValues = match.slice(1).map(v => v && decodeURIComponent(v)) - } else { - remainingPathname = paramValues = null + // If there is a remaining pathname, treat the path separator as part of + // the remaining pathname for properly continuing the match. + remainingPathname = `/${remainingPathname}` } return { remainingPathname, paramNames, - paramValues + paramValues: match.slice(1).map(v => v && decodeURIComponent(v)) } } @@ -133,16 +125,19 @@ export function getParamNames(pattern) { } export function getParams(pattern, pathname) { - const { paramNames, paramValues } = matchPattern(pattern, pathname) - - if (paramValues != null) { - return paramNames.reduce(function (memo, paramName, index) { - memo[paramName] = paramValues[index] - return memo - }, {}) + const match = matchPattern(pattern, pathname) + if (!match) { + return null } - return null + const { paramNames, paramValues } = match + const params = {} + + paramNames.forEach((paramName, index) => { + params[paramName] = paramValues[index] + }) + + return params } /** diff --git a/modules/isActive.js b/modules/isActive.js index 1c40a9c01c..942fdd9f02 100644 --- a/modules/isActive.js +++ b/modules/isActive.js @@ -82,9 +82,13 @@ function routeIsActive(pathname, routes, params) { if (remainingPathname !== null && pattern) { const matched = matchPattern(pattern, remainingPathname) - remainingPathname = matched.remainingPathname - paramNames = [ ...paramNames, ...matched.paramNames ] - paramValues = [ ...paramValues, ...matched.paramValues ] + if (matched) { + remainingPathname = matched.remainingPathname + paramNames = [ ...paramNames, ...matched.paramNames ] + paramValues = [ ...paramValues, ...matched.paramValues ] + } else { + remainingPathname = null + } if (remainingPathname === '') { // We have an exact match on the route. Just check that all the params diff --git a/modules/matchRoutes.js b/modules/matchRoutes.js index 3897d65980..1f5df6648a 100644 --- a/modules/matchRoutes.js +++ b/modules/matchRoutes.js @@ -89,9 +89,13 @@ function matchRouteDeep( // we're not just searching for potential nested absolute paths. if (remainingPathname !== null && pattern) { const matched = matchPattern(pattern, remainingPathname) - remainingPathname = matched.remainingPathname - paramNames = [ ...paramNames, ...matched.paramNames ] - paramValues = [ ...paramValues, ...matched.paramValues ] + if (matched) { + remainingPathname = matched.remainingPathname + paramNames = [ ...paramNames, ...matched.paramNames ] + paramValues = [ ...paramValues, ...matched.paramValues ] + } else { + remainingPathname = null + } // By assumption, pattern is non-empty here, which is the prerequisite for // actually terminating a match. diff --git a/package.json b/package.json index 679cb139aa..43dcf1cc14 100644 --- a/package.json +++ b/package.json @@ -15,16 +15,16 @@ "homepage": "https://github.com/reactjs/react-router#readme", "bugs": "https://github.com/reactjs/react-router/issues", "scripts": { - "build": "npm run build-cjs && npm run build-es6", - "build-cjs": "rimraf lib && babel ./modules -d lib --ignore '__tests__'", - "build-es6": "rimraf es6 && babel ./modules -d es6 --blacklist=es6.modules --ignore '__tests__'", - "build-umd": "NODE_ENV=development webpack modules/index.js umd/ReactRouter.js", - "build-min": "NODE_ENV=production webpack -p modules/index.js umd/ReactRouter.min.js", + "build": "npm run build-cjs && npm run build-es", + "build-cjs": "rimraf lib && BABEL_ENV=cjs babel ./modules -d lib --ignore '__tests__'", + "build-es": "rimraf es6 && BABEL_ENV=es babel ./modules -d es6 --ignore '__tests__'", + "build-umd": "BABEL_ENV=cjs NODE_ENV=development webpack modules/index.js umd/ReactRouter.js", + "build-min": "BABEL_ENV=cjs NODE_ENV=production webpack -p modules/index.js umd/ReactRouter.min.js", "lint": "eslint modules examples", - "start": "node examples/server.js", + "start": "BABEL_ENV=cjs node examples/server.js", "test": "npm run lint && npm run test-node && npm run test-browser", - "test-browser": "karma start", - "test-node": "mocha --compilers js:babel-core/register tests.node.js" + "test-browser": "BABEL_ENV=cjs karma start", + "test-node": "BABEL_ENV=cjs mocha --compilers js:babel-register tests.node.js" }, "authors": [ "Ryan Florence", @@ -40,11 +40,18 @@ "react": "^0.14.0 || ^15.0.0" }, "devDependencies": { - "babel": "^5.8.38", - "babel-core": "^5.8.38", - "babel-eslint": "^4.1.8", - "babel-loader": "^5.4.0", - "babel-plugin-dev-expression": "^0.1.0", + "babel-cli": "^6.7.5", + "babel-core": "^6.7.6", + "babel-eslint": "^5.0.4", + "babel-loader": "^6.2.4", + "babel-plugin-add-module-exports": "^0.1.2", + "babel-plugin-dev-expression": "^0.2.1", + "babel-preset-es2015": "^6.6.0", + "babel-preset-es2015-loose": "^7.0.0", + "babel-preset-es2015-loose-native-modules": "^1.0.0", + "babel-preset-react": "^6.5.0", + "babel-preset-stage-0": "^6.5.0", + "babel-register": "^6.7.2", "bundle-loader": "^0.5.4", "codecov.io": "^0.1.6", "coveralls": "^2.11.9", @@ -52,17 +59,17 @@ "eslint": "^1.10.3", "eslint-config-rackt": "^1.1.1", "eslint-plugin-react": "^3.16.1", - "expect": "^1.16.0", + "expect": "^1.18.0", "express": "^4.13.4", "express-urlrewrite": "^1.2.0", "gzip-size": "^3.0.0", - "isparta-loader": "^1.0.0", + "isparta-loader": "^2.0.0", "karma": "^0.13.22", "karma-browserstack-launcher": "^0.1.10", "karma-chrome-launcher": "^0.2.3", "karma-coverage": "^0.5.5", "karma-mocha": "^0.2.2", - "karma-mocha-reporter": "^2.0.0", + "karma-mocha-reporter": "^2.0.1", "karma-sourcemap-loader": "^0.3.7", "karma-webpack": "^1.7.0", "mocha": "^2.4.5",