This repository was archived by the owner on Sep 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss.ts
More file actions
83 lines (78 loc) · 2.62 KB
/
css.ts
File metadata and controls
83 lines (78 loc) · 2.62 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
71
72
73
74
75
76
77
78
79
80
81
82
83
/* eslint-disable @typescript-eslint/no-shadow */
import { dirname } from 'path';
// import { promisify } from 'util';
import autoprefixer from 'autoprefixer';
import postcss from 'postcss';
import postcssSorting from 'postcss-sorting';
import postcssCombineMediaQuery from 'postcss-combine-media-query';
import sass from 'sass';
import type { Handler } from './types.js';
const escape = (str: string) => str.replace(/["%&#{}<>|]/g, (i) => ({
'"': '\'',
'%': '%25',
'&': '%26',
'#': '%23',
'{': '%7B',
'}': '%7D',
'<': '%3C',
'>': '%3E',
'|': '%7C',
}[i])!);
// const sassRender = promisify(sass.render);
const css: Handler = (repack) => async ({ source: file }) => sass.compileAsync(
file,
{
loadPaths: [dirname(file)],
style: 'compressed',
functions: {
// TODO 'asset' helper function for SASS
'asset($file, $inline: "")': async (args: sass.Value[]) => {
const file = args[0].assertString('arg1').text;
const inline = args[1].assertString('arg2').text;
if (inline !== '') {
// TODO handle base64 encoding, etc…
}
return repack(file).then((version) => new sass.SassString(`url("${version.uri}")`, { quotes: false }));
},
'inline-svg($file, $fill:"")': async (args: sass.Value[]) => {
const file = args[0].assertString('arg1').text;
const fill = args[1].assertString('arg2').text;
if (!file) {
throw new Error('No SVG file specified');
}
return repack(file).then((version) => version.data).then((svg) => {
if (!svg /* || !(svg instanceof Version) */) {
throw new Error(`${file} not found in svg object.`);
}
let data = svg.toString();
if (fill !== '') {
data = data.replace(
/(['"])#[0-9A-Fa-f]{3,6}/g,
`$1#${fill.replace(/^#/, '')}`,
);
}
return new sass.SassString(`url("data:image/svg+xml,${escape(data)}")`, { quotes: false });
}).catch((error) => {
console.error(error);
throw error;
});
},
},
},
)
// .catch(logError)
.then(({ css }) =>
// @ts-ignore
postcss([
autoprefixer(),
postcssCombineMediaQuery(),
postcssSorting({
order: ['custom-properties', 'dollar-variables', 'declarations', 'rules', 'at-rules'],
'properties-order': 'alphabetical',
'unspecified-properties-position': 'bottomAlphabetical',
}),
])
.process(css, { from: undefined }))
// remove any left over newlines
.then(({ css }) => Buffer.from(css.toString().replace(/\n/g, '')));
export default css;