1 | "use strict";
|
2 | const fs = require('fs-extra');
|
3 |
|
4 | const Path = require('path');
|
5 |
|
6 | const Utils = exports;
|
7 |
|
8 | Utils.isObjectLiteral = function(thing) {
|
9 | return !!thing && typeof thing == 'object' && (thing.constructor === Object || Object.getPrototypeOf(thing) === null);
|
10 | };
|
11 |
|
12 | Utils.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 |
|
27 | Utils.loadImports = function(code, srcDir, path, recursivelyImport) {
|
28 | let _ = path.lastIndexOf('.') + 1 || path.length;
|
29 |
|
30 | |
31 |
|
32 |
|
33 | let pathWithoutExt = path.slice(0, _);
|
34 | let extension = path.slice(_);
|
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 |
|
64 | Utils.getFolder = function(file) {
|
65 | file = Path.normalize(file);
|
66 | return file.split(Path.sep).slice(0, -1).join(Path.sep);
|
67 | };
|
68 |
|
69 | Utils.TYPE = {
|
70 | JS: 'js',
|
71 | CSS: 'css',
|
72 | HTML: 'html',
|
73 | XML: 'xml',
|
74 | JSON: 'json',
|
75 | };
|
76 |
|
77 | Utils.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 |
|
93 | Utils.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 |
|
107 | Utils.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 |