rantail
Version:
The ultimate companion for Tailwind CSS. Write clean and secure code effortlessly. Protect your work from theft with Rantail. Encode your Tailwind classes with unique and customizable cuid's. Enjoy smooth, encoded, and theft-proof coding.
221 lines (213 loc) • 6.56 kB
JavaScript
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined")
return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/logger.ts
var Logger = class _Logger {
/**
* Generic error logger
* @param text
* @returns
*/
static error(...text) {
return console.error(`\u274C`, `[rantail]`, ...text);
}
/**
* Generic log
* @param emoji
* @param text
*/
static log(emoji, ...text) {
return console.log(emoji, `[rantail]:`, ...text);
}
/**
* Generic success logger
* @param
* @returns
*/
static generationCompleted() {
_Logger.log(`\u2705`, "Generation completed");
}
/**
* Generic file process logger
* @param
* @returns
*/
static logFile(fileName) {
_Logger.log(`\u2757`, `Processing File`, fileName);
}
/**
* Generic config loader logger
* @param
* @returns
*/
static loadingConfig(path2) {
_Logger.log(`\u2728`, `Loading Rantail config from `, path2);
}
static loadingCSS(path2) {
_Logger.log(`\u2728`, `Loading main CSS file from `, path2);
}
};
// src/utils/merge.ts
import _ from "lodash";
var overwriteMerge = (...configs) => {
return _.merge({}, ...configs);
};
// src/utils/defaults.ts
var defaultConfig = {
content: [
"./pages/**/*.{js,jsx,ts,tsx}",
"./components/**/*.{js,jsx,ts,tsx}",
"./app/**/*.{js,jsx,ts,tsx}",
"./src/**/*.{js,jsx,ts,tsx}"
],
cuidLength: 12
};
var withDefaultConfig = (config) => {
return overwriteMerge(defaultConfig, config);
};
// src/utils/path.ts
import path from "path";
import fs from "fs/promises";
import { pathToFileURL } from "url";
var minimist = __require("minimist");
var getPath = (...pathSegment) => {
return path.resolve(process.cwd(), ...pathSegment);
};
var getConfigFilePath = () => __async(void 0, null, function* () {
const args = minimist(process.argv.slice(2));
const configPath = getPath(args.config || "rantail.config.js");
return fs.stat(configPath).then(() => pathToFileURL(configPath).toString());
});
var getCSSFilePath = (cssFilePath) => __async(void 0, null, function* () {
if (!cssFilePath) {
throw new Error("CSS File Path is not declared in the config File, continuing with unencoded code build.");
}
const fullCssFilePath = getPath(cssFilePath);
Logger.loadingCSS(fullCssFilePath);
return fullCssFilePath;
});
// src/parsers/config-parser.ts
var ConfigParser = class {
/**
* Load rantail.config.js as module
* @returns
*/
loadBaseConfig() {
return __async(this, null, function* () {
const path2 = yield getConfigFilePath();
Logger.loadingConfig(path2);
const baseConfig = yield import(path2);
if (!(baseConfig == null ? void 0 : baseConfig.default)) {
throw new Error("Unable to import rantail config file");
}
return withDefaultConfig(baseConfig.default);
});
}
loadConfig() {
return __async(this, null, function* () {
const config = yield this.loadBaseConfig();
return {
config
};
});
}
};
// src/cli.ts
import fs2 from "fs";
import * as fg from "fast-glob";
// src/utils/cuid.ts
import { init } from "@paralleldrive/cuid2";
var generateCUID = (cuidLength, prefix, suffix) => {
const cuid = init({ length: cuidLength });
let generatedCuid = cuid();
if (prefix) {
generatedCuid = prefix + generatedCuid;
}
if (suffix) {
generatedCuid = generatedCuid + suffix;
}
return generatedCuid;
};
// src/cli.ts
var CLI = class {
main() {
return __async(this, null, function* () {
var _a;
const configParser = new ConfigParser();
const { config } = yield configParser.loadConfig();
let cssFilePath = yield getCSSFilePath(config.cssFilePath);
fs2.appendFileSync(cssFilePath, "\n");
const classNameRegex = /className=(['"])(.*?)\1|className={`([^`]*?)`}/g;
let match;
let replacements = {};
for (const pattern of config.content) {
const files = fg.globSync(pattern);
for (const file of files) {
Logger.logFile(file);
let jsxFileContent = fs2.readFileSync(file, "utf8");
while ((match = classNameRegex.exec(jsxFileContent)) !== null) {
const originalClassNames = (_a = match[2] || match[3]) == null ? void 0 : _a.replace(/`|'|"|{|}/g, "").split(" ");
if (Array.isArray(originalClassNames)) {
for (const originalClassName of originalClassNames) {
if (!/^[a-z0-9-:\\[\]\\]+$/.test(originalClassName) || originalClassName.length < 3 || config.ignore && originalClassName.startsWith(config.ignore)) {
continue;
}
if (!replacements[originalClassName]) {
const randomClassName = generateCUID(config.cuidLength, config.prefix, config.suffix);
replacements[originalClassName] = randomClassName;
let cssContent = `.${randomClassName} { @apply ${originalClassName}; }
`;
fs2.appendFileSync(cssFilePath, cssContent);
}
}
}
}
for (let key in replacements) {
let value = replacements[key];
let escapedKey = key.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
let regex = new RegExp(`(?<![a-z0-9-_])${escapedKey}(?![a-z0-9-_])`, "g");
jsxFileContent = jsxFileContent.replace(regex, value);
}
fs2.writeFileSync(file, jsxFileContent);
}
}
});
}
/**
* Execute and log result
* @returns
*/
execute() {
return __async(this, null, function* () {
return this.main().then(Logger.generationCompleted).catch(Logger.error);
});
}
};
export {
CLI
};
//# sourceMappingURL=cli.mjs.map