This commit is contained in:
Jason 2022-02-14 00:25:17 -05:00
parent b9b6aee734
commit 82f5f76588
66 changed files with 74967 additions and 1 deletions

37
rewrite/css.js Normal file
View file

@ -0,0 +1,37 @@
import { parse, walk, generate } from "css-tree";
import EventEmitter from "./events.js";
import parsel from "./parsel.js";
class CSS extends EventEmitter {
constructor(ctx) {
super();
this.ctx = ctx;
this.meta = ctx.meta;
this.parsel = parsel;
this.parse = parse;
this.walk = walk;
this.generate = generate;
};
rewrite(str, options) {
return this.recast(str, options, 'rewrite');
};
source(str, options) {
return this.recast(str, options, 'source');
};
recast(str, options, type) {
if (!str) return str;
str = new String(str).toString();
try {
const ast = this.parse(str, { ...options, parseCustomProperty: true });
this.walk(ast, node => {
this.emit(node.type, node, options, type);
});
return this.generate(ast);
} catch(e) {
console.log(e)
return str;
};
};
};
export default CSS;