This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·146 lines (114 loc) · 3.47 KB
/
index.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Sakugawa
* https://github.com/paazmaya/sakugawa
*
* Copyright (c) Juga Paazmaya <paazmaya@yahoo.com> (https://paazmaya.fi)
* Licensed under the MIT license.
*/
const css = require('css');
// const postcss = require('postcss');
class Sakugawa {
constructor (styles, options) {
if (typeof styles !== 'string') {
throw new Error('styles must be a string');
}
if (styles.length < 1) {
throw new Error('styles must not be empty');
}
options = options || {};
this.maxSelectors = options.maxSelectors || 4090;
this.maxImports = options.maxImports || 4;
this.mediaQueries = options.mediaQueries || 'normal';
this.filename = options.filename || 'input.css';
this.minSheets = options.minSheets || 1;
this.ast = css.parse(styles);
}
_clone(ast) {
// Store temporarily elsewhere to speed up cloning
const origRules = ast.stylesheet.rules;
ast.stylesheet.rules = [];
const clone = JSON.parse(JSON.stringify(ast));
ast.stylesheet.rules = origRules;
clone.stylesheet.rules = [];
// Handle charset copying, if such exists
const charsetRules = origRules.filter((rule) => {
return rule.type === 'charset';
});
if (charsetRules.length > 0) {
clone.stylesheet.rules = charsetRules;
}
return clone;
}
_countSelectors(rule, includeMedia) {
let total = 0;
if (rule.type === 'rule' && typeof rule.selectors === 'object') {
total += rule.selectors.length;
}
else if (includeMedia && rule.type === 'media' && rule.rules) {
rule.rules.forEach((media) => {
total += this._countSelectors(media, true); // even while media should not contain media...
});
}
return total;
}
_generatePages(rules, ast) {
const pages = [];
let clone,
selectorsForThisPage = 0;
// Remove charset rules since they are added when clone is created
rules = rules.filter(function filterNonCharsetRules(rule) {
return rule.type !== 'charset';
});
rules.forEach((rule) => {
const selectorCount = this._countSelectors(rule, true);
if (clone && selectorsForThisPage + selectorCount <= this.maxSelectors) {
selectorsForThisPage += selectorCount;
clone.stylesheet.rules.push(rule);
}
else {
// Restart
selectorsForThisPage = selectorCount;
clone = this._clone(ast);
clone.stylesheet.rules.push(rule);
pages.push(clone);
}
});
return pages;
}
createPages() {
let pages = [],
{
rules
} = this.ast.stylesheet;
// Remove media queries from the first iteration when not needed there
if (this.mediaQueries !== 'normal') {
rules = rules.filter((rule) => {
return rule.type !== 'media';
});
}
pages = this._generatePages(rules, this.ast);
// Separate media queries
if (this.mediaQueries === 'separate') {
const mediaRules = this.ast.stylesheet.rules.filter((rule) => {
return rule.type === 'media';
});
pages = pages.concat(this._generatePages(mediaRules, this.ast));
}
return pages;
}
getPages() {
const pages = this.createPages();
const sheets = pages.map((page) => {
const str = css.stringify(page);
return str;
});
while (sheets.length < this.minSheets) {
sheets.push('');
}
return sheets;
}
}
module.exports = function exports(styles, options) {
const s = new Sakugawa(styles, options);
return s.getPages();
};