UNPKG

3.84 kBJavaScriptView Raw
1"use strict";
2const fs = require('fs-extra');
3
4const Path = require('path');
5
6const Utils = exports;
7
8Utils.isObjectLiteral = function(thing) {
9 return !!thing && typeof thing == 'object' && (thing.constructor === Object || Object.getPrototypeOf(thing) === null);
10};
11
12Utils.reportSettings = function(settings) {
13 let logMessage = ['Minifying:'];
14
15 if (settings.minifySelectors) logMessage.push('Selectors');
16 if (settings.minifyCSS) logMessage.push('CSS files');
17 if (settings.minifyJS) logMessage.push('JavaScript files' + (settings.minifyJS.harmony ? ' (harmony)' : ''));
18 if (settings.minifyHTML) logMessage.push('HTML files' + (settings.minifyHTML.minifyInlineJS ? ' + inline JavaScript' : '') + (settings.minifyHTML.minifyInlineCSS ? ' + inline CSS' : ''));
19 if (settings.minifyXML) logMessage.push('XML files');
20
21 if (logMessage.length == 1) logMessage = ['Not minifying anything'];
22
23 logMessage = logMessage.join('\n - ');
24 console.log(logMessage);
25};
26
27Utils.loadImports = function(code, srcDir, path, recursivelyImport) {
28 let _ = path.lastIndexOf('.') + 1 || path.length;
29
30 /*
31 WARNING: File paths are relative to source and destination dirs
32 */
33 let pathWithoutExt = path.slice(0, _); // "html/app." (last dot included) instead of "html/app.js"
34 let extension = path.slice(_); // js, html, css ...
35
36 code = code.replace(/<ZC-IMPORT\[(.+?)\]>/g, (_, importFile) => {
37 let importFilePath, importFileAbsPath;
38
39 if (importFile.indexOf('/') === 0) {
40 importFileAbsPath = importFile;
41 } else if (importFile.indexOf('/') > 0) {
42 importFilePath = Utils.getFolder(pathWithoutExt) + '/' + importFile;
43 } else {
44 if (extension) {
45 importFilePath = pathWithoutExt + importFile + '.' + extension;
46 } else {
47 importFilePath = pathWithoutExt + '.' + importFile;
48 }
49 }
50
51 if (!importFileAbsPath) {
52 importFileAbsPath = srcDir + importFilePath;
53 }
54
55 let ret = fs.readFileSync(importFileAbsPath, 'utf8');
56 if (recursivelyImport) {
57 ret = Utils.loadImports(ret, srcDir, importFilePath, true);
58 }
59 return ret;
60 });
61 return code;
62};
63
64Utils.getFolder = function(file) {
65 file = Path.normalize(file);
66 return file.split(Path.sep).slice(0, -1).join(Path.sep);
67};
68
69Utils.TYPE = {
70 JS: 'js',
71 CSS: 'css',
72 HTML: 'html',
73 XML: 'xml',
74 JSON: 'json',
75};
76
77Utils.getType = function(extension, extensions) {
78 if (extensions.js.includes(extension)) {
79 return Utils.TYPE.JS;
80 } else if (extensions.css.includes(extension)) {
81 return Utils.TYPE.CSS;
82 } else if (extensions.html.includes(extension)) {
83 return Utils.TYPE.HTML;
84 } else if (extensions.xml.includes(extension)) {
85 return Utils.TYPE.XML;
86 } else if (extensions.json.includes(extension)) {
87 return Utils.TYPE.JSON;
88 } else {
89 throw new TypeError(`Unknown type for extension ${extension}`);
90 }
91};
92
93Utils.clone = function(thing) {
94 if (Array.isArray(thing)) {
95 return thing.map(Utils.clone);
96 } else if (Utils.isObjectLiteral(thing)) {
97 let ret = Object.create(null);
98 Object.keys(thing).forEach(propName => {
99 ret[propName] = Utils.clone(thing[propName]);
100 });
101 return ret;
102 } else {
103 return thing;
104 }
105};
106
107Utils.makeSettings = function(defaults, provided) {
108 let result = Object.create(null);
109
110 Object.keys(defaults).forEach(propName => {
111 let defaultValue = defaults[propName];
112 let providedValue = provided[propName];
113
114 if (providedValue === undefined) {
115 result[propName] = defaultValue;
116 } else if (Utils.isObjectLiteral(defaultValue)) {
117 if (providedValue === true) {
118 result[propName] = defaultValue;
119 } else if (!Utils.isObjectLiteral(providedValue)) {
120 result[propName] = Utils.clone(providedValue);
121 } else {
122 result[propName] = Utils.makeSettings(defaultValue, providedValue);
123 }
124 } else {
125 result[propName] = Utils.clone(providedValue);
126 }
127 });
128
129 return result;
130};
\No newline at end of file