-
-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathpost-process.ts
More file actions
105 lines (86 loc) · 2.62 KB
/
post-process.ts
File metadata and controls
105 lines (86 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import type { AtRule, AtRuleProps, Root, RootProps } from 'postcss';
import type { ImportStatement, NodesStatement, Stylesheet} from './statement';
import { isImportStatement, isNodesStatement, isPreImportStatement, isWarning } from './statement';
export function postProcess(stylesheet: Stylesheet, atRule: (defaults?: AtRuleProps) => AtRule, root: (defaults?: RootProps) => Root): void {
let indexOfFirstImport = -1;
let indexOfLastImport = -1;
for (let i = 0; i < stylesheet.statements.length; i++) {
const stmt = stylesheet.statements[i];
if (isImportStatement(stmt)) {
if (indexOfFirstImport === -1) {
indexOfFirstImport = i;
}
indexOfLastImport = i;
continue;
}
}
for (let i = 0; i < stylesheet.statements.length; i++) {
const stmt = stylesheet.statements[i];
if (isImportStatement(stmt)) {
continue;
}
if (isWarning(stmt)) {
continue;
}
if (isNodesStatement(stmt) && !stmt.importingNode) {
continue;
}
if (isPreImportStatement(stmt)) {
if (i < indexOfLastImport) {
const params = 'data:text/css;base64,' + Buffer.from(stmt.node.toString()).toString('base64');
const importStmt: ImportStatement = {
type: 'import',
uri: params,
fullUri: '"' + params + '"',
node: atRule({
name: 'import',
params: '"' + params + '"',
source: stmt.node.source,
}),
conditions: stmt.conditions,
from: stmt.from,
importingNode: stmt.importingNode,
};
stylesheet.statements.splice(i, 1, importStmt);
} else {
const nodesStmt: NodesStatement = {
type: 'nodes',
nodes: [stmt.node],
conditions: stmt.conditions,
from: stmt.from,
importingNode: stmt.importingNode,
};
stylesheet.statements.splice(i, 1, nodesStmt);
}
continue;
}
if (i < indexOfFirstImport && stmt.nodes.every((x) => x.type === 'atrule' && !x.nodes)) {
continue;
}
if (i < indexOfLastImport && (stmt.nodes.every((x) => x.type === 'comment'))) {
continue;
}
if (i < indexOfLastImport) {
const dummyRoot = root();
stmt.nodes.forEach((node) => {
node.parent = undefined;
dummyRoot.append(node);
});
const params = 'data:text/css;base64,' + Buffer.from(dummyRoot.toString()).toString('base64');
const importStmt: ImportStatement = {
type: 'import',
uri: params,
fullUri: '"' + params + '"',
node: atRule({
name: 'import',
params: '"' + params + '"',
source: stmt.importingNode?.source ?? stmt.nodes[0]?.source,
}),
conditions: stmt.conditions,
from: stmt.from,
importingNode: stmt.importingNode,
};
stylesheet.statements.splice(i, 1, importStmt);
}
}
}