babel-plugin-react-pug-classnames
Advanced tools
Comparing version 0.2.1 to 0.3.0
76
index.js
@@ -1,3 +0,13 @@ | ||
var DEFAULT_CLASSNAMES_FUNCTION = 'babel-plugin-react-pug-classnames/classnames' | ||
var DEFAULT_CLASSNAMES_FUNCTION = 'babel-plugin-react-pug-classnames/classcat' | ||
var LEGACY_CLASSNAMES_FUNCTION = 'babel-plugin-react-pug-classnames/prefixedClassnames' | ||
// DEPRECATED. | ||
// Legacy classnames prefixing is enabled by default for now. | ||
// In later versions it will be turned off by default and then removed completely. | ||
// If you need to provide some kind of BEM-like prefixing or another type | ||
// of classnames functionality -- specify the classnamesFunction in options | ||
// which must be a module name with the following code: | ||
// exports.c = function (name, modifiers) { ... } | ||
var DEFAULT_SUPPORT_LEGACY = true | ||
function isTargetAttr (attribute, classAttribute) { | ||
@@ -13,10 +23,20 @@ if (!classAttribute) classAttribute = 'className' | ||
module.exports = (babel) => { | ||
var reqName | ||
var hasTransformedClassName | ||
var t = babel.types | ||
function generateRequireExpression (elementName, expression, classnamesFunction) { | ||
var require = t.callExpression(t.identifier('require'), [ | ||
t.stringLiteral(classnamesFunction || DEFAULT_CLASSNAMES_FUNCTION) | ||
]) | ||
function isRequire(node) { | ||
return ( | ||
node && | ||
node.declarations && | ||
node.declarations[0] && | ||
node.declarations[0].init && | ||
node.declarations[0].init.callee && | ||
node.declarations[0].init.callee.name === "require" | ||
); | ||
} | ||
function generateRequireExpression (elementName, expression, opts) { | ||
var callExpression = t.callExpression( | ||
require, | ||
reqName, | ||
[t.stringLiteral(elementName), expression] | ||
@@ -27,2 +47,15 @@ ) | ||
function generateRequire(name, opts) { | ||
var legacy = opts.legacy == null ? DEFAULT_SUPPORT_LEGACY : opts.legacy | ||
var classnamesFn = opts.classnamesFunction || ( | ||
legacy ? LEGACY_CLASSNAMES_FUNCTION : DEFAULT_CLASSNAMES_FUNCTION | ||
) | ||
var require = t.callExpression(t.identifier('require'), [ | ||
t.stringLiteral(classnamesFn) | ||
]) | ||
var cFn = t.memberExpression(require, t.identifier('c')); | ||
var d = t.variableDeclarator(name, cFn); | ||
return t.variableDeclaration("var", [d]); | ||
} | ||
function processClass (JSXOpeningElement, opts) { | ||
@@ -74,3 +107,4 @@ var name = JSXOpeningElement.node.name.name | ||
if (t.isObjectExpression(expr.right) || t.isArrayExpression(expr.right)) { | ||
expr.right = generateRequireExpression(elementName, expr.right, opts.classnamesFunction) | ||
hasTransformedClassName = true | ||
expr.right = generateRequireExpression(elementName, expr.right, opts) | ||
} | ||
@@ -96,3 +130,4 @@ } | ||
// Process only if the styleName value is an object or array | ||
expr = generateRequireExpression(elementName, expr, opts.requireFunction) | ||
hasTransformedClassName = true | ||
expr = generateRequireExpression(elementName, expr, opts) | ||
} | ||
@@ -119,3 +154,28 @@ | ||
return { | ||
post() { | ||
reqName = null; | ||
hasTransformedClassName = null; | ||
}, | ||
visitor: { | ||
Program: { | ||
enter(path, state) { | ||
reqName = path.scope.generateUidIdentifier( | ||
"classnames" | ||
); | ||
}, | ||
exit(path, state) { | ||
if (!hasTransformedClassName) { | ||
return; | ||
} | ||
const lastImportOrRequire = path | ||
.get("body") | ||
.filter(p => p.isImportDeclaration() || isRequire(p.node)) | ||
.pop(); | ||
if (lastImportOrRequire) { | ||
lastImportOrRequire.insertAfter(generateRequire(reqName, state.opts)); | ||
} | ||
} | ||
}, | ||
JSXElement (JSXElement, params) { | ||
@@ -122,0 +182,0 @@ JSXElement.traverse({ |
{ | ||
"name": "babel-plugin-react-pug-classnames", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "[react-pug] Transform className property using BEM classnames-like function", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
12242
4
160