-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathbase64-encoded-import.ts
More file actions
35 lines (31 loc) · 1.09 KB
/
base64-encoded-import.ts
File metadata and controls
35 lines (31 loc) · 1.09 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
import type { Condition } from './conditions';
import { formatImportPrelude } from './format-import-prelude';
// Base64 encode an import with conditions
// The order of conditions is important and is interleaved with cascade layer declarations
// Each group of conditions and cascade layers needs to be interpreted in order
// To achieve this we create a list of base64 encoded imports, where each import contains a stylesheet with another import.
// Each import can define a single group of conditions and a single cascade layer.
export function base64EncodedConditionalImport(prelude: string, conditions: Array<Condition>): string {
conditions.reverse();
const first = conditions.pop();
if (!first) {
return prelude;
}
let params = `${prelude} ${formatImportPrelude(
first.layer,
first.media,
first.supports,
first.scope,
)}`;
for (const condition of conditions) {
params = `"data:text/css;base64,${Buffer.from(
`@import ${params}`,
).toString('base64')}" ${formatImportPrelude(
condition.layer,
condition.media,
condition.supports,
condition.scope,
)}`;
}
return params;
}