@navikt/aksel
Version:
Aksel command line interface. Handles css-imports, codemods and more
34 lines (33 loc) • 1.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = transformer;
const legacy_tokens_1 = require("../config/legacy.tokens");
const token_regex_1 = require("../config/token-regex");
function transformer(file) {
let src = file.source;
for (const [name, config] of Object.entries(legacy_tokens_1.legacyTokenConfig)) {
if (!config.twOld || !config.twNew) {
continue;
}
const isBreakpoint = name.includes("breakpoint");
if (isBreakpoint) {
src = src.replace(config.regexes.tailwind, `${config.twNew}:`);
continue;
}
const beforeSplit = config.twOld.split(",");
const afterSplit = config.twNew.split(",");
const matches = src.match(config.regexes.tailwind) || [];
for (const match of matches) {
const index = beforeSplit.indexOf(match.trim().replace(":", ""));
if (index >= 0) {
const withPrefix = match.trim().startsWith(":");
const addSpace = match.startsWith(" ");
const replacementToken = afterSplit[index];
src = src.replace((0, token_regex_1.createSingleTwRegex)(match), withPrefix
? `:${replacementToken}`
: `${addSpace ? " " : ""}${replacementToken}`);
}
}
}
return src;
}