gatsby-transformer-javascript-frontmatter
Advanced tools
Comparing version 1.0.6-0 to 1.0.6
@@ -1,129 +0,165 @@ | ||
var _asyncToGenerator = require("@babel/runtime/helpers/asyncToGenerator"); | ||
"use strict"; | ||
const _ = require(`lodash`); | ||
var _regenerator = require("babel-runtime/regenerator"); | ||
const crypto = require(`crypto`); | ||
var _regenerator2 = _interopRequireDefault(_regenerator); | ||
const babylon = require(`babylon`); | ||
var _extends2 = require("babel-runtime/helpers/extends"); | ||
const traverse = require(`babel-traverse`).default; | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
function onCreateNode(_x) { | ||
return _onCreateNode.apply(this, arguments); | ||
} | ||
var _asyncToGenerator2 = require("babel-runtime/helpers/asyncToGenerator"); | ||
function _onCreateNode() { | ||
_onCreateNode = _asyncToGenerator(function* ({ | ||
node, | ||
getNode, | ||
actions, | ||
loadNodeContent | ||
}) { | ||
const createNode = actions.createNode, | ||
createParentChildLink = actions.createParentChildLink; // This only processes javascript & jsx files. | ||
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2); | ||
if (node.internal.mediaType !== `application/javascript` && node.internal.mediaType !== `text/jsx`) { | ||
return; | ||
} | ||
var onCreateNode = function () { | ||
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(_ref) { | ||
var node = _ref.node, | ||
getNode = _ref.getNode, | ||
boundActionCreators = _ref.boundActionCreators, | ||
loadNodeContent = _ref.loadNodeContent; | ||
var createNode, createParentChildLink, code, options, exportsData, frontmatter, error, ast, parseData, objStr, contentDigest, nodeData; | ||
return _regenerator2.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
createNode = boundActionCreators.createNode, createParentChildLink = boundActionCreators.createParentChildLink; | ||
const code = yield loadNodeContent(node); | ||
const options = { | ||
sourceType: `module`, | ||
allowImportExportEverywhere: true, | ||
plugins: [`jsx`, `doExpressions`, `objectRestSpread`, `decorators`, `classProperties`, `exportExtensions`, `asyncGenerators`, `functionBind`, `functionSent`, `dynamicImport`, `flow`] | ||
}; | ||
let exportsData, frontmatter, error; | ||
// This only processes javascript & jsx files. | ||
try { | ||
const ast = babylon.parse(code, options); | ||
if (!(node.internal.mediaType !== `application/javascript` && node.internal.mediaType !== `text/jsx`)) { | ||
_context.next = 3; | ||
break; | ||
} | ||
const parseData = function parseData(node) { | ||
let value; | ||
return _context.abrupt("return"); | ||
if (node.type === `TemplateLiteral`) { | ||
// Experimental basic support for template literals: | ||
// Extract and join any text content; ignore interpolations | ||
value = node.quasis.map(quasi => quasi.value.cooked).join(``); | ||
} else if (node.type === `ObjectExpression`) { | ||
value = {}; | ||
node.properties.forEach(elem => { | ||
value[elem.key.name] = parseData(elem.value); | ||
}); | ||
} else if (node.type === `ArrayExpression`) { | ||
value = node.elements.map(elem => parseData(elem)); | ||
} else { | ||
value = node.value; | ||
} | ||
case 3: | ||
_context.next = 5; | ||
return loadNodeContent(node); | ||
return value; | ||
}; | ||
case 5: | ||
code = _context.sent; | ||
options = { | ||
sourceType: `module`, | ||
allowImportExportEverywhere: true, | ||
plugins: [`jsx`, `doExpressions`, `objectRestSpread`, `decorators`, `classProperties`, `exportExtensions`, `asyncGenerators`, `functionBind`, `functionSent`, `dynamicImport`, `flow`] | ||
}; | ||
exportsData = void 0, frontmatter = void 0, error = void 0; | ||
frontmatter = {}; | ||
error = false; | ||
traverse(ast, { | ||
AssignmentExpression: function AssignmentExpression(astPath) { | ||
if (astPath.node.left.type === `MemberExpression` && astPath.node.left.property.name === `frontmatter`) { | ||
astPath.node.right.properties.forEach(node => { | ||
frontmatter[node.key.name] = parseData(node.value); | ||
}); | ||
} | ||
}, | ||
ExportNamedDeclaration: function ExportNamedDeclaration(astPath) { | ||
const declaration = astPath.node.declaration; | ||
try { | ||
ast = babylon.parse(code, options); | ||
if (declaration && declaration.type === `VariableDeclaration`) { | ||
const dataVariableDeclarator = _.find(declaration.declarations, d => d.id.name === `frontmatter`); | ||
parseData = function parseData(node) { | ||
var value = void 0; | ||
if (dataVariableDeclarator && dataVariableDeclarator.init) { | ||
dataVariableDeclarator.init.properties.forEach(node => { | ||
frontmatter[node.key.name] = parseData(node.value); | ||
if (node.type === `TemplateLiteral`) { | ||
// Experimental basic support for template literals: | ||
// Extract and join any text content; ignore interpolations | ||
value = node.quasis.map(function (quasi) { | ||
return quasi.value.cooked; | ||
}).join(``); | ||
} else if (node.type === `ObjectExpression`) { | ||
value = {}; | ||
node.properties.forEach(function (elem) { | ||
value[elem.key.name] = parseData(elem.value); | ||
}); | ||
} else if (node.type === `ArrayExpression`) { | ||
value = node.elements.map(function (elem) { | ||
return parseData(elem); | ||
}); | ||
} else { | ||
value = node.value; | ||
} | ||
return value; | ||
}; | ||
frontmatter = {}; | ||
error = false; | ||
traverse(ast, { | ||
AssignmentExpression: function AssignmentExpression(astPath) { | ||
if (astPath.node.left.type === `MemberExpression` && astPath.node.left.property.name === `frontmatter`) { | ||
astPath.node.right.properties.forEach(function (node) { | ||
frontmatter[node.key.name] = parseData(node.value); | ||
}); | ||
} | ||
}, | ||
ExportNamedDeclaration: function ExportNamedDeclaration(astPath) { | ||
var declaration = astPath.node.declaration; | ||
if (declaration && declaration.type === `VariableDeclaration`) { | ||
var dataVariableDeclarator = _.find(declaration.declarations, function (d) { | ||
return d.id.name === `frontmatter`; | ||
}); | ||
if (dataVariableDeclarator && dataVariableDeclarator.init) { | ||
dataVariableDeclarator.init.properties.forEach(function (node) { | ||
frontmatter[node.key.name] = parseData(node.value); | ||
}); | ||
} | ||
} | ||
} | ||
}); | ||
} catch (e) { | ||
// stick the error on the query so the user can | ||
// react to an error as they see fit | ||
error = { | ||
err: true, | ||
code: e.code, | ||
message: e.message, | ||
stack: e.stack | ||
}; | ||
} finally { | ||
// only create node if frontmatter is not empty | ||
if (!_.isEmpty(frontmatter)) { | ||
exportsData = (0, _extends3.default)({}, frontmatter, { | ||
error: error | ||
}); | ||
objStr = JSON.stringify(node); | ||
contentDigest = crypto.createHash(`md5`).update(objStr).digest(`hex`); | ||
nodeData = { | ||
id: `${node.id} >>> JavascriptFrontmatter`, | ||
children: [], | ||
parent: node.id, | ||
node: (0, _extends3.default)({}, node), | ||
internal: { | ||
contentDigest, | ||
type: `JavascriptFrontmatter` | ||
} | ||
}; | ||
nodeData.frontmatter = (0, _extends3.default)({}, exportsData); | ||
if (node.internal.type === `File`) { | ||
nodeData.fileAbsolutePath = node.absolutePath; | ||
} | ||
createNode(nodeData); | ||
createParentChildLink({ parent: node, child: nodeData }); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} catch (e) { | ||
// stick the error on the query so the user can | ||
// react to an error as they see fit | ||
error = { | ||
err: true, | ||
code: e.code, | ||
message: e.message, | ||
stack: e.stack | ||
}; | ||
} finally { | ||
// only create node if frontmatter is not empty | ||
if (!_.isEmpty(frontmatter)) { | ||
exportsData = Object.assign({}, frontmatter, { | ||
error: error | ||
}); | ||
const objStr = JSON.stringify(node); | ||
const contentDigest = crypto.createHash(`md5`).update(objStr).digest(`hex`); | ||
const nodeData = { | ||
id: `${node.id} >>> JavascriptFrontmatter`, | ||
children: [], | ||
parent: node.id, | ||
node: Object.assign({}, node), | ||
internal: { | ||
contentDigest, | ||
type: `JavascriptFrontmatter` | ||
} | ||
}; | ||
nodeData.frontmatter = Object.assign({}, exportsData); | ||
if (node.internal.type === `File`) { | ||
nodeData.fileAbsolutePath = node.absolutePath; | ||
case 9: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
createNode(nodeData); | ||
createParentChildLink({ | ||
parent: node, | ||
child: nodeData | ||
}); | ||
} | ||
} | ||
}); | ||
return _onCreateNode.apply(this, arguments); | ||
} | ||
}, _callee, this); | ||
})); | ||
return function onCreateNode(_x) { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _ = require(`lodash`); | ||
var crypto = require(`crypto`); | ||
var babylon = require(`babylon`); | ||
var traverse = require(`babel-traverse`).default; | ||
exports.onCreateNode = onCreateNode; |
{ | ||
"name": "gatsby-transformer-javascript-frontmatter", | ||
"description": "Gatsby transformer plugin for JavaScript to extract exports.frontmatter statically.", | ||
"version": "1.0.6-0", | ||
"version": "1.0.6", | ||
"author": "Jacob Bolda <me@jacobbolda.com>", | ||
@@ -13,4 +13,2 @@ "dependencies": { | ||
"devDependencies": { | ||
"@babel/cli": "^7.0.0-beta.38", | ||
"@babel/core": "^7.0.0-beta.38", | ||
"cross-env": "^5.0.5" | ||
@@ -17,0 +15,0 @@ }, |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
9361
1
136
1