-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathgenerate-bundle.js
executable file
·288 lines (264 loc) · 7.96 KB
/
generate-bundle.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env node
/* eslint-disable no-console */
const glob = require("glob");
const path = require("path");
const sass = require("sass");
const fs = require("fs");
const pkg = require("../package.json");
const rimraf = require("rimraf");
const CleanCSS = require("clean-css");
const cleanCSSInstance = new CleanCSS({
advanced: true,
promise: true,
});
const currentDir = path.dirname(__dirname);
const { exec } = require("child_process");
const bodyMatch = new RegExp("body ?({(?:.|\\s|\\S)*?})", "m");
const rootMatch = new RegExp(":root ?({(?:.|\\s|\\S)*?})", "m");
// The list of directories in the dist to pull
const tokensList = [["evo-core", "evo-light"]];
const tokensDirList = ["evo"];
/**
* Main Processing class. Holds info about args passed and ds version
*/
class CssProcesser {
constructor(tokensFile, tokensDir, args) {
this.tokensFile = tokensFile;
this.tokensDir = tokensDir;
this.args = args;
this.processed = [];
this.skipped = [];
let classDef = "";
const scopeClass = args.scopeClass;
if (scopeClass) {
for (let i = 0; i < args.scopeSpecificity; i++) {
classDef += `.${scopeClass}`;
}
}
this.classDef = classDef;
}
get minify() {
return this.args.minify !== false;
}
run() {
return this.generateSASS()
.then((raw) => this.compileSASS(raw))
.then((raw) =>
this.minify ? cleanCSSInstance.minify(raw) : { styles: raw },
)
.then((raw) => this.writeAllFiles(raw.styles))
.catch((e) => console.error(e));
}
getDistCss() {
return new Promise((resolve, reject) => {
glob(`${currentDir}/dist/**/*.css`, (err, files) => {
if (err) {
return reject(err);
}
console.log(files);
resolve(files);
});
});
}
wrap(cssContents) {
return `${this.classDef} {
${cssContents}
}`;
}
rewrapBody(cssContents) {
// First get match
const body = bodyMatch.exec(cssContents);
if (body && body[1]) {
// Add new body
const newContents = cssContents.replace(bodyMatch, "");
return `body ${this.classDef} ${body[1]}
${this.wrap(newContents)}
`;
}
return this.wrap(cssContents);
}
rewrapRoot(cssContents) {
const root = rootMatch.exec(cssContents);
if (root && root[1]) {
// Add newroot
const newContents = cssContents.replace(rootMatch, "");
return `${this.classDef} ${root[1]}
${this.wrap(newContents)}
`;
}
return this.wrap(cssContents);
}
/**
* Generates a raw sass file for given css file
* @param {*} file
*/
generateRawSASS(file) {
const fileName = path.basename(file, ".css");
if (
this.args.modules.length > 0 &&
this.args.modules.indexOf(fileName) === -1
) {
if (this.tokensFile.indexOf(fileName) === -1) {
this.skipped.push(fileName);
return "";
}
}
this.processed.push(fileName);
if (!!this.classDef) {
// Need to read file and output
const cssContents = fs.readFileSync(file).toString();
// Check if it's global module and change body
if (fileName === "global") {
return this.rewrapBody(cssContents);
}
if (this.tokensFile.indexOf(fileName) > -1) {
return this.rewrapRoot(cssContents);
}
return this.wrap(cssContents);
}
return `@import "${file}";`;
}
processFiles(files) {
return new Promise((resolve) => {
const compiled = files
.map((file) => this.generateRawSASS(file))
.join("\n");
const processed = this.processed;
const skipped = this.skipped;
const tokensFile = this.tokensFile;
if (this.args.modules.length > 0 || this.args.verbose) {
console.log(
`Processed ${processed.length} modules for ${tokensFile}`,
);
}
if (this.args.verbose) {
console.log(
`Modules processed: ${processed.join(
",",
)} for ${tokensFile}`,
);
console.log(
`Skipped ${skipped.length} modules for ${tokensFile}`,
);
console.log(
`Modules skipped: ${skipped.join(",")} for ${tokensFile}`,
);
}
resolve(compiled);
});
}
generateSASS() {
return this.getDistCss(this.tokensFile).then((files) =>
this.processFiles(files),
);
}
compileSASS(raw) {
const compilation = sass.compileString(raw);
return compilation.css;
}
writeAllFiles(raw) {
const cdnPath = getCDNPath(this.args.name);
rimraf.sync(cdnPath);
return makeDir(cdnPath).then(() =>
writeFile(
`${cdnPath}/skin-${this.tokensDir}.${
this.minify ? "min." : ""
}css`,
`/* autoprefixer: off */\n${raw}\n/* autoprefixer: on */`,
),
);
}
}
function getCDNPath(bundle) {
return `${currentDir}/_cdn/${bundle}/v${pkg.version}`;
}
/**
* Runs the compilation ofsass
* @param {*} res The response with tokensFile and raw csssass/
* @param {*} plugin The given plugin to run the render with
*/
/**
* Runs npm build to get dist output
*/
function prebuild() {
return new Promise((resolve, reject) => {
console.log("Running build...");
exec("npm run build:css", (err) => {
if (err) {
return reject(err);
}
console.log("npm build successful");
return resolve();
});
});
}
function makeDir(dirPath) {
return new Promise((resolve, reject) => {
fs.mkdir(dirPath, { recursive: true }, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
}
function writeFile(file, data) {
return new Promise((resolve, reject) => {
fs.writeFile(file, data, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
}
/**
* Main function to start, generates sass files and compiles and writes them
* @param {*} name
* @param {*} args
*/
function runCSSBuild(name, args) {
prebuild()
.then(() =>
Promise.all(
tokensList.map((token, index) => {
const cssProcesser = new CssProcesser(
token,
tokensDirList[index],
args,
);
return cssProcesser.run();
}),
),
)
.then(() => {
console.log(`Bundles created successfully!
Please upload the ./_cdn/${args.name}/v${pkg.version} directory to CDN
`);
})
.catch((e) => {
console.error(e);
});
}
async function listBundles(argv) {
await prebuild();
tokensList.forEach((tokensFile, index) => {
const cssProcesser = new CssProcesser(
tokensFile,
tokensDirList[index],
argv,
);
cssProcesser.getDistCss().then((files) => {
console.log(`======================`);
console.log(`${tokensFile} - modules avaiable`);
console.log(`======================`);
files.forEach((file) => {
console.log(path.basename(file, ".css"));
});
});
});
}
module.exports = {
runCSSBuild,
listBundles,
};