babel-plugin-vtex-render-route
Advanced tools
Comparing version 1.0.2 to 1.1.0
@@ -16,32 +16,57 @@ 'use strict'; | ||
ImportDeclaration: function ImportDeclaration(path) { | ||
var isRouteImport = path.node.source.value === ROUTE_IMPORT_VALUE; | ||
var isDefaultImport = t.isImportDefaultSpecifier(path.node.specifiers[0]); | ||
var isRouteImport = path.node.source.value === _utils.ROUTE_IMPORT_VALUE; | ||
var importSpecifier = path.node.specifiers[0]; | ||
var isDefaultImport = t.isImportDefaultSpecifier(importSpecifier); | ||
if (isRouteImport && isDefaultImport) { | ||
routeFnIdentifier = path.node.specifiers[0].local.name; | ||
routeFnIdentifier = importSpecifier.local.name; | ||
path.remove(); | ||
} | ||
}, | ||
CallExpression: function CallExpression(path, state) { | ||
var node = path.node; | ||
var routeBinding = path.scope.bindings[routeFnIdentifier]; | ||
var isRouteCallee = path.node.callee.name === routeFnIdentifier; | ||
var isRouteCallee = node.callee.name === routeFnIdentifier; | ||
var isModule = routeBinding ? routeBinding.kind === 'module' : null; | ||
if (isRouteCallee && isModule) { | ||
var _path$node$arguments = _slicedToArray(path.node.arguments, 2); | ||
var _node$arguments = _slicedToArray(node.arguments, 2); | ||
var nameArg = _path$node$arguments[0]; | ||
var pathArg = _path$node$arguments[1]; | ||
var nameArg = _node$arguments[0]; | ||
var pathArg = _node$arguments[1]; | ||
var isNameString = t.isStringLiteral(nameArg); | ||
var isPathString = t.isStringLiteral(pathArg); | ||
var parentArguments = path.parent.arguments; | ||
if (path.node.arguments.length > 2) { | ||
_logger2.default.warn(createFileMessage('Too many arguments on ' + routeFnIdentifier + ' function', path, state)); | ||
if (node.arguments.length < 2) { | ||
(0, _utils.logRouteFewArguments)(routeFnIdentifier, node, state); | ||
return; | ||
} else if (node.arguments.length > 2) { | ||
(0, _utils.logRouteManyArguments)(routeFnIdentifier, node, state); | ||
} else if (!isNameString || !isPathString) { | ||
(0, _utils.logRouteStringArguments)(routeFnIdentifier, node, state); | ||
return; | ||
} | ||
if (!isNameString || !isPathString) { | ||
_logger2.default.error(createFileMessage('Arguments to the ' + routeFnIdentifier + ' function must be string literals', path, state)); | ||
return; | ||
if (parentArguments) { | ||
var parentCallee = path.parent.callee; | ||
var isCalleeRoute = (0, _utils.isRouteParentCallee)(routeFnIdentifier, path); | ||
var isFewArguments = isCalleeRoute ? parentArguments.length < 1 : false; | ||
var isManyArguments = isCalleeRoute ? parentArguments.length > 1 : false; | ||
if (isFewArguments) { | ||
(0, _utils.logRouteParentFewArguments)(routeFnIdentifier, parentCallee, state); | ||
} else if (isManyArguments) { | ||
(0, _utils.logRouteParentManyArguments)(routeFnIdentifier, parentCallee, state); | ||
return; | ||
} | ||
if (isCalleeRoute) { | ||
path.parentPath.replaceWithSourceString(parentArguments[0].name); | ||
} | ||
} | ||
path.remove(); | ||
state.file.metadata.vtexRender = { | ||
@@ -59,15 +84,2 @@ route: { | ||
var _logger = require('./logger'); | ||
var _logger2 = _interopRequireDefault(_logger); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var ROUTE_IMPORT_VALUE = 'vtex.renderjs/route.js'; | ||
function createFileMessage(message, path, state) { | ||
var filename = state.file.opts.filename; | ||
var loc = path.node.loc; | ||
return message + ' on ' + filename + ' (' + loc.start.line + ':' + loc.start.column + ')'; | ||
} | ||
var _utils = require('./utils'); |
{ | ||
"name": "babel-plugin-vtex-render-route", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "A babel plugin for extracting VTEX Render route information from a component", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
# babel-plugin-vtex-render-route | ||
A babel plugin for extracting VTEX Render route information from a component | ||
## What? | ||
If you write the following: | ||
```jsx | ||
import route from 'vtex.renderjs/route.js' | ||
// Your React component code here | ||
// Let's call this component 'Home' | ||
export default route('home', '/')(Home) | ||
``` | ||
It will extract the information on the parameters provided for the `route` function and append it to the babel's file `metadata` property. | ||
With the example above it would create something like this: | ||
```javascript | ||
file { | ||
metadata { | ||
// Various babel's properties | ||
vtexRender { | ||
route { | ||
name: 'home', | ||
path: '/' | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
## How? | ||
For the extraction to be succesful it depends on some rules: | ||
- You **must** make a default `import` from `vtex.renderjs/route.js` | ||
- The arguments given for the imported function **must** be **string literals**, for now we're not accepting variables or expressions | ||
That's it! | ||
The name of the function isn't fixed, sou you're not required to use the name `route` (as written on our example). | ||
## Why? | ||
This plugin is intended to be used in conjunction with [gulp-vtex-render](http://github.com/vtex/gulp-vtex-render) on the [VTEX Toolbelt](http://github.com/vtex/toolbelt). | ||
Together they're able to extract route information on build time for the VTEX Render framework. | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9704
6
118
44