-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathcss.ts
More file actions
70 lines (63 loc) · 1.83 KB
/
Copy pathcss.ts
File metadata and controls
70 lines (63 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { FormatDefinition, type FileData, type FileFormat, type FormatHandler } from "../FormatHandler.ts";
import CommonFormats, { Category } from "src/CommonFormats.ts";
const LESS_FORMAT = new FormatDefinition(
"LESS Stylesheet",
"less",
"less",
"text/less",
Category.CODE
);
const SCSS_FORMAT = new FormatDefinition(
"SCSS Stylesheet",
"scss",
"scss",
"text/x-scss",
Category.CODE
);
class cssHandler implements FormatHandler {
public name: string = "CSS";
public supportedFormats?: FileFormat[];
public ready: boolean = false;
async init() {
this.supportedFormats = [
CommonFormats.CSS.builder("css")
.allowFrom(true)
.allowTo(true)
.markLossless(),
LESS_FORMAT.builder("less")
.allowFrom(true),
SCSS_FORMAT.builder("scss")
.allowFrom(true)
];
this.ready = true;
}
async doConvert(
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat,
): Promise<FileData[]> {
const outputFiles: FileData[] = [];
for (const file of inputFiles) {
const source = new TextDecoder().decode(file.bytes);
const basename = file.name.split(".").slice(0, -1).join(".");
let css: string;
if (inputFormat.internal === "less") {
const less = await import("less");
const { css: compiled } = await less.default.render(source);
css = compiled;
} else if (inputFormat.internal === "scss") {
const sass = await import("sass");
const result = sass.compileString(source, {url: new URL(`file://${file.name}`)})
css = result.css;
} else {
css = source;
}
outputFiles.push({
name: `${basename}.${outputFormat.internal}`,
bytes: new TextEncoder().encode(css),
})
}
return outputFiles;
}
}
export default cssHandler;