UNPKG

@twstyled/babel-preset

Version:

Babel plugin for twstyled -- the full-featured Tailwind CSS + CSS in JS Compiler

95 lines (94 loc) 4.38 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const class_utilities_1 = __importDefault(require("@xwind/class-utilities")); const core_1 = __importStar(require("@xwind/core")); const util_1 = require("@twstyled/util"); let $transformer; function getArgs(referencePath) { if (!referencePath.isIdentifier()) { throw new Error('Reference path is not identifier'); } const twClasses = []; const path = referencePath.parentPath; if (path.isCallExpression()) { const argumentPaths = path.get('arguments'); if (Array.isArray(argumentPaths)) { for (const argumentPath of argumentPaths) { const { confident, value } = argumentPath.evaluate(); if (!confident) throw new Error(`Value of "${argumentPath.getSource()}" could not be statically evaluated.`); twClasses.push(value); } } } if (path.isTaggedTemplateExpression()) { const quasiPath = path.get('quasi'); if (!Array.isArray(quasiPath)) { const { confident, value } = quasiPath.evaluate(); if (!confident) throw new Error(`Value of "${quasiPath.getSource()}" could not be statically evaluated.`); twClasses.push(value); } } return twClasses; } function getTransformerTWX(configPath) { const twConfigPath = util_1.getTwConfigPath(configPath); const { twConfig, isNewTwConfig } = util_1.getTwConfigCache(twConfigPath); if (!$transformer || isNewTwConfig) { const { screens, variants, baseRoot, componentsRoot, utilitiesRoot, generateTwClassSubstituteRoot } = core_1.default(twConfig); const twClassesUtils = class_utilities_1.default(twConfig.separator, [ ...screens, ...variants ]); const twClassDictionary = Object.assign({ XWIND_BASE: core_1.createTwClassDictionary(baseRoot).XWIND_GLOBAL }, core_1.createTwClassDictionary(componentsRoot, utilitiesRoot)); const tailwindObjectstyles = (twClasses) => { var _a, _b; const parsedTwClasses = twClassesUtils.parser(twClasses); const composedTwClasses = twClassesUtils.composer(twClasses); const objectstyles = []; for (const parsedTwClass of parsedTwClasses) { const twRoot = generateTwClassSubstituteRoot(twClassDictionary, parsedTwClass); objectstyles.push(core_1.transformTwRootToObjectstyle(parsedTwClass.twClass, twRoot)); } let objectstyle = core_1.mergeObjectstyles(objectstyles); if ((_b = (_a = twConfig.xwind) === null || _a === void 0 ? void 0 : _a.objectstyles) === null || _b === void 0 ? void 0 : _b.plugins) { for (const plugin of twConfig.xwind.objectstyles.plugins) { objectstyle = plugin(objectstyle, composedTwClasses, twConfig); } } return objectstyle; }; $transformer = (paths, state, t) => { for (const path of paths) { const args = getArgs(path); const objectstyles = tailwindObjectstyles(args); path.parentPath.replaceWith(t.valueToNode(objectstyles)); } }; } return $transformer; } exports.default = getTransformerTWX;