const css = require('./');
const fs = require('fs');
if (process.argv[process.argv.length - 1] !== __filename) {
let opts = {
filename: process.argv[process.argv.length - 1],
code: fs.readFileSync(process.argv[process.argv.length - 1]),
minify: true,
sourceMap: true,
targets: {
chrome: 95 << 16
}
};
console.time('optimize');
let r = css.transform(opts);
console.timeEnd('optimize')
console.log(r);
let code = r.code;
if (r.map) {
code = code.toString() + `\n/*# sourceMappingURL=out.css.map */\n`;
}
fs.writeFileSync('out.css', code);
if (r.map) {
fs.writeFileSync('out.css.map', r.map);
}
return;
}
let res = css.transform({
filename: __filename,
code: Buffer.from(`
@namespace "http://foo.com";
@namespace svg "http://bar.com";
.selector > .nested[data-foo=bar]:not(.foo):hover::part(tab active) {
width: 32px;
--foo: var(--bar, 30px);
background: linear-gradient(red, green);
}
svg|foo {
test: foo;
}
@media (hover) and (width > 50px) {
.foo {
color: red;
background: inline('.gitignore');
}
}
`),
visitor: {
visitLength(length) {
if (length.unit === 'px') {
return {
unit: 'rem',
value: length.value / 16
}
}
return length;
},
visitColor(color) {
console.log(color);
return color;
},
visitImage(image) {
image.value.value[1].push('moz');
return image;
},
visitProperty(property) {
if (property.property === 'background') {
property.value[0].repeat.x = 'no-repeat';
property.value[0].repeat.y = 'no-repeat';
}
return property;
},
visitMediaQuery(query) {
query.media_type = 'print';
return query;
},
visitFunction(fn) {
if (fn.name === 'inline') {
return {
name: 'url',
arguments: [{
type: 'token',
value: {
type: 'string',
value: fs.readFileSync(fn.arguments[0].value.value).toString('base64'),
}
}]
}
}
return fn;
},
visitSelector(selector) {
console.log(require('util').inspect(selector, { depth: 10 }));
for (let component of selector) {
if (component.type === 'class') {
component.name = 'tw-' + component.name;
} else if (component.type === 'attribute') {
component.name = 'tw-' + component.name;
component.operation.operator = 'includes';
}
}
return selector;
}
},
});
console.log(res.code.toString());