aliascss 1.4.3 → 1.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { compiler as statementMaker } from './returnStatement.js';
2
2
  import config from './config.js';
3
+ import { splitByCommaOutsideParens } from './utils/helper.js';
3
4
  // import mapRawAttributeNames from './utils/browser-helper.js';
4
5
  export default statementMaker;
5
6
  export let classPrinter = {
@@ -93,7 +94,8 @@ export let classPrinter = {
93
94
  split.forEach((each) => {
94
95
  try {
95
96
  const [at, pNv] = each.replace(/\]\[/, ']-[').replace("-", "=").split("=");
96
- const result = returnPnV(pNv.replace(/^\[|\]$/g, '').split(','));
97
+ // const result=returnPnV(pNv.replace(/^\[|\]$/g,'').split(','));
98
+ const result = returnPnV(splitByCommaOutsideParens(pNv.replace(/^\[|\]$/g, '')));
97
99
  kfStatement += ` ${at.replace('@', '').replace(/:/g, ',').replace(/,/g, "%,").replace(/[\[|\]]/g, '')}% {${result ? result : ''}}`;
98
100
  }
99
101
  catch (error) {
@@ -0,0 +1,27 @@
1
+ export default function main(config: {
2
+ ['input']: string | string[];
3
+ [key: string]: any;
4
+ }): void;
5
+ export declare function globalCSSCompiler(): void;
6
+ export declare function compileFile(file: string, config: any, statementMaker: {
7
+ ['make']: (a: string) => string;
8
+ group: (a: string, b: string) => string;
9
+ ['rawCSS']?: string;
10
+ ['customGroupStatement']?: string;
11
+ }, globalClassList: string[]): string[];
12
+ export declare function groupStatement(name: string, classNames: string, statementMaker: {
13
+ ['group']: (a: string, b: string) => string;
14
+ }): string;
15
+ export declare function keyframesStatement(name: string, timeLine: string, statementMaker: {
16
+ ['make']: (a: string, b?: string | null, c?: boolean) => string;
17
+ }): string;
18
+ export declare function createCSSFileByFile(file: string, bool?: boolean): void;
19
+ export declare function postify(file: string, plugin: any[], minify?: boolean, sort?: "mobile-first" | "desktop-first"): void;
20
+ export declare function writeStatementToFile(file: string, content: string, from?: string): void;
21
+ export declare function initialize(configFile: {
22
+ [key: string]: any;
23
+ }): void;
24
+ export declare function watch(config: {
25
+ ['input']: string | string[];
26
+ [key: string]: any;
27
+ }): void;
@@ -0,0 +1,379 @@
1
+ import extractClassNamesFromFile from "./extractClassNamesFromFile.js";
2
+ import { compiler as statementMaker } from "../returnStatement.js";
3
+ import config from '../config.js';
4
+ import fs from "fs";
5
+ import fg from "fast-glob";
6
+ import path from 'path';
7
+ import postcss from 'postcss';
8
+ import postcssDiscardDuplicates from 'postcss-discard-duplicates';
9
+ import cssnano from 'cssnano';
10
+ import chokidar from 'chokidar';
11
+ import sortMediaQueries from 'postcss-sort-media-queries';
12
+ import { splitByCommaOutsideParens } from '../utils/helper.js';
13
+ const init = {
14
+ classList: [], // hold className for MasterCSS to avoid repeating
15
+ groups: [],
16
+ input: '', // input glob pattern
17
+ output: '', // location off output
18
+ '--file': false, // if set true compileFileByFile to [filename].css
19
+ statementMaker,
20
+ config,
21
+ '--module': false,
22
+ rawCSS: '',
23
+ layers: [],
24
+ initializedLayer: false,
25
+ customGroupStatement: '',
26
+ minify: false,
27
+ globPattern: '', // Use this to check valid new created file to add to watch during watch.on
28
+ };
29
+ export default function main(config) {
30
+ // initialize
31
+ initialize(config);
32
+ if (init.config.prefix)
33
+ init.statementMaker.prefix = init.config.prefix;
34
+ if (init.input && (init.output || init['--file'])) {
35
+ const input = fg.sync(init.input, { dot: true });
36
+ // console.log(input);
37
+ input.forEach((file) => {
38
+ createCSSFileByFile(file);
39
+ });
40
+ postify(init.output, [postcssDiscardDuplicates], init.minify);
41
+ }
42
+ else {
43
+ console.error("Please Provide input, output or set '--file' to true");
44
+ }
45
+ }
46
+ export function globalCSSCompiler() {
47
+ const classList = [];
48
+ if (init.globPattern && init.output) {
49
+ fs.truncateSync(init.output);
50
+ // 1. Write @layer once at the top of the freshly truncated file
51
+ if (Array.isArray(init.layers) && init.layers.length > 0) {
52
+ fs.writeFileSync(init.output, `@layer ${init.layers.join(', ')};\n\n`, { flag: 'a+' });
53
+ init.initializedLayer = true;
54
+ }
55
+ const input = fg.sync(init.globPattern, { dot: true });
56
+ // console.log(input);
57
+ input.forEach((file) => {
58
+ const [gbContent,] = compileFile(file, config, statementMaker, classList);
59
+ try {
60
+ fs.writeFileSync(init.output, `/* Start :-----------ACSS ${path.relative('.', file)}------------------------*/\n${gbContent}
61
+ \n/* End :-----------ACSS ${path.relative('.', file)}----------------------------*/\n
62
+ `, { flag: 'a+' });
63
+ console.log('AliasCSS compiled', path.relative('.', file), 'to main css file :' + init.output);
64
+ }
65
+ catch (error) {
66
+ console.log('\x1b[31m', "Couldn't able to append/create to main css file :" + init.output);
67
+ console.log(error);
68
+ }
69
+ });
70
+ postify(init.output, [postcssDiscardDuplicates], init.minify);
71
+ }
72
+ else {
73
+ console.error("Please Provide input, output or set '--file' to true");
74
+ }
75
+ }
76
+ export function compileFile(file, config, statementMaker, globalClassList) {
77
+ let [globalStatement, compiledStatement, moduleStatement] = ['', '', ''];
78
+ // inject config.group and config.statement
79
+ if (statementMaker.rawCSS) {
80
+ compiledStatement += `/* From:aliacss.config.statement */\n${statementMaker.rawCSS}\n/* -----End:aliacss.config.statement----- */\n\n`;
81
+ moduleStatement += `/* From:aliacss.config.statement */\n${statementMaker.rawCSS}\n/* ------End:aliacss.config.statement----- */\n\n`;
82
+ }
83
+ if (statementMaker.customGroupStatement) {
84
+ compiledStatement += `/* From:aliacss.config.group */\n${statementMaker.customGroupStatement}\n/* -----End:aliacss.config.group----- */\n\n`;
85
+ moduleStatement += `/* From:aliacss.config.group */\n${statementMaker.customGroupStatement}\n/* -----End:aliacss.config.group----- */\n\n`;
86
+ }
87
+ const [classList, groups, keyframe, rawCSSStatement] = extractClassNamesFromFile(file, config);
88
+ // 1. raw-css
89
+ if (rawCSSStatement) {
90
+ globalStatement += rawCSSStatement + '\n';
91
+ compiledStatement += rawCSSStatement + '\n';
92
+ }
93
+ // 2. group
94
+ for (const key in groups) {
95
+ if (groups.hasOwnProperty(key)) {
96
+ // --------------ModuleStatement
97
+ if (/^module=/.test(key)) {
98
+ const mdStatement = statementMaker.group(groups[key], key.replace(/module=/, ''));
99
+ moduleStatement += mdStatement + '\n';
100
+ }
101
+ // -------------EndOfModule
102
+ else {
103
+ const gpStatement = statementMaker.group(groups[key], key);
104
+ globalStatement += gpStatement + '\n';
105
+ compiledStatement += gpStatement + '\n';
106
+ }
107
+ }
108
+ }
109
+ // 2. ClassList
110
+ classList.forEach((e) => {
111
+ const statement = statementMaker.make(e);
112
+ if (statement) {
113
+ console.log('\x1b[32m', statement);
114
+ if (globalClassList.indexOf(e) === -1) {
115
+ globalStatement += statement + '\n';
116
+ globalClassList.push(e);
117
+ // console.log(e);
118
+ }
119
+ compiledStatement += statement + '\n';
120
+ }
121
+ });
122
+ // 3. Keyframe
123
+ for (const key in keyframe) {
124
+ if (keyframe.hasOwnProperty(key)) {
125
+ const kfStatement = keyframesStatement(key, keyframe[key], statementMaker);
126
+ globalStatement += kfStatement + '\n';
127
+ compiledStatement += kfStatement + '\n';
128
+ }
129
+ }
130
+ return [globalStatement, compiledStatement, moduleStatement];
131
+ }
132
+ export function groupStatement(name, classNames, statementMaker) {
133
+ return statementMaker.group(classNames, name) + '\n';
134
+ }
135
+ export function keyframesStatement(name, timeLine, statementMaker) {
136
+ let kfStatement = '@keyframes ' + name + "{\n";
137
+ const returnPnV = (e) => e.map((i) => statementMaker.make(i, null, true)).join(";");
138
+ const split = timeLine.split(/\s+/);
139
+ split.forEach((each) => {
140
+ try {
141
+ const [at, pNv] = each.replace(/\]\[/, ']-[').replace("-", "=").split("=");
142
+ // kfStatement+=` ${at.replace('@','').replace(/:/g,',').replace(/,/g,"%,").replace(/[\[|\]]/g,'')}% {${returnPnV(pNv.replace(/^\[|\]$/g,'').split(','))}}`
143
+ kfStatement += ` ${at.replace('@', '').replace(/:/g, ',').replace(/,/g, "%,").replace(/[\[|\]]/g, '')}% {${returnPnV(splitByCommaOutsideParens(pNv.replace(/^\[|\]$/g, '')))}}`;
144
+ }
145
+ catch (error) {
146
+ console.error('Not a valid entry for AliasCSS keyframes processor : ' + each, error);
147
+ }
148
+ });
149
+ return kfStatement + "\n}\n";
150
+ }
151
+ export function createCSSFileByFile(file, bool = false) {
152
+ const [gbContent, content, module] = compileFile(file, config, statementMaker, init.classList);
153
+ // console.log(init.classList);
154
+ // If its fileByFile
155
+ if (init["--module"] && module.trim()) {
156
+ try {
157
+ fs.writeFileSync(file + '.module.css', module);
158
+ // console.log(content);
159
+ console.log('AliasCSS compiled', path.relative('.', file), 'module Locally ');
160
+ }
161
+ catch (error) {
162
+ console.log('\x1b[31m', "Couldn't able to append/create locally from " + file);
163
+ console.log(error);
164
+ }
165
+ }
166
+ if (init["--file"]) {
167
+ try {
168
+ fs.writeFileSync(file + '.css', content);
169
+ postify(file + '.css', [postcssDiscardDuplicates], init.minify);
170
+ // console.log(content);
171
+ console.log('AliasCSS compiled', path.relative('.', file), 'Locally');
172
+ }
173
+ catch (error) {
174
+ console.log('\x1b[31m', "Couldn't able to append/create locally from " + file);
175
+ console.log(error);
176
+ }
177
+ }
178
+ // Check for Global
179
+ if (!bool && init.output) {
180
+ try {
181
+ fs.writeFileSync(init.output, `\n/* Start :-----------ACSS ${path.relative('.', file)}------------------------*/\n${postcss([postcssDiscardDuplicates]).process(gbContent)}
182
+ \n/* End :-----------ACSS ${path.relative('.', file)}----------------------------*/\n
183
+ `, { flag: 'a+' });
184
+ console.log('AliasCSS compiled', path.relative('.', file), 'to main css file :' + init.output);
185
+ }
186
+ catch (error) {
187
+ console.log('\x1b[31m', "Couldn't able to append/create to main css file :" + init.output);
188
+ console.log(error);
189
+ }
190
+ }
191
+ }
192
+ export function postify(file, plugin, minify = false, sort = "mobile-first") {
193
+ const data = fs.readFileSync(file, 'utf-8');
194
+ plugin.push(sortMediaQueries({
195
+ sort
196
+ }));
197
+ if (fs.existsSync(file)) {
198
+ if (minify === true)
199
+ plugin.push(cssnano);
200
+ postcss(plugin).process(data, { from: undefined }).then(result => {
201
+ fs.truncateSync(file);
202
+ fs.writeFileSync(file, result.css);
203
+ });
204
+ }
205
+ // fs.readFile(file, (err, css) => {
206
+ // postcss(plugin)
207
+ // .process(css, { from: file, to: file+"post.css" })
208
+ // .then(result => {
209
+ // fs.writeFile(file+"post.css", result.css, () => true)
210
+ // // if ( result.map ) {
211
+ // // fs.writeFile(file+'.map', result.map.toString(), () => true)
212
+ // // }
213
+ // })
214
+ // })
215
+ }
216
+ export function writeStatementToFile(file, content, from) {
217
+ // Check for Global
218
+ if (init.output) {
219
+ try {
220
+ fs.writeFileSync(init.output, `\n/* Start :-----------ACSS:From aliasCSSConfig.${from}------------------------*/\n${content}
221
+ \n/* End :-----------ACSS aliasCSSConfig.${from}----------------------------*/\n
222
+ `, { flag: 'a+' });
223
+ console.log('AliasCSS compiled', '[aliasCSSConfig.statement]', 'aliascss.config.js');
224
+ }
225
+ catch (error) {
226
+ console.log('\x1b[31m', "Couldn't able to append/create [key:statement] from alias.config.js ");
227
+ console.log(error);
228
+ }
229
+ }
230
+ }
231
+ // Initialize
232
+ export function initialize(configFile) {
233
+ // ----------------input-------------------
234
+ if (configFile.hasOwnProperty('input')) {
235
+ init.input = configFile.input;
236
+ init.globPattern = configFile.input;
237
+ }
238
+ // ----------------input-------------------
239
+ if (configFile.hasOwnProperty('layers') && Array.isArray(configFile.layers)) {
240
+ init.layers = configFile.layers;
241
+ }
242
+ else {
243
+ init.layers = config.layers;
244
+ }
245
+ // ----------------Prefix-------------------
246
+ if (configFile.hasOwnProperty('prefix')) {
247
+ init.config.prefix = configFile.prefix;
248
+ }
249
+ // ----------------output-------------------
250
+ if (configFile.hasOwnProperty('output')) {
251
+ const op = configFile.output;
252
+ if (op.hasOwnProperty('location')) {
253
+ init.output = op.location;
254
+ }
255
+ if (op.hasOwnProperty('--file') && op['--file'] === true) {
256
+ init["--file"] = true;
257
+ }
258
+ }
259
+ // Adding Custom color, length etc
260
+ if (configFile.hasOwnProperty('custom') && typeof configFile.custom === 'object') {
261
+ Object.keys(configFile.custom).forEach((key) => {
262
+ init.statementMaker.addCustom(key, configFile.custom[key]);
263
+ });
264
+ }
265
+ // Extend custom class name define {'border-color-native":'border-color: skyBlue',}
266
+ if (configFile.hasOwnProperty('prebuild')) {
267
+ init.statementMaker.prebuild(configFile.prebuild);
268
+ }
269
+ // Ignore these class name
270
+ if (configFile.hasOwnProperty('ignore')) {
271
+ if (Array.isArray(configFile.ignore)) {
272
+ configFile.ignore.forEach((each) => init.config.ignore.push(each.trim()));
273
+ }
274
+ }
275
+ // --module
276
+ if (configFile.hasOwnProperty('--module') && configFile.hasOwnProperty('importModuleAs')) {
277
+ init["--module"] = true;
278
+ init.config.useCSSModule = init["--module"] ? true : false;
279
+ if (init.config.useCSSModule)
280
+ init.config.matchCSSModuleFunction = init.config.createCSSModuleRegex(configFile.importModuleAs);
281
+ }
282
+ // ---------output Minify
283
+ if (configFile.hasOwnProperty('minify')) {
284
+ init.minify = configFile.minify;
285
+ }
286
+ // ------------Extractor function-------
287
+ if (configFile.hasOwnProperty('extractorFunction')) {
288
+ init.config.useExtractorFunction = configFile.extractorFunction.trim() ? true : false;
289
+ if (init.config.useExtractorFunction)
290
+ init.config.matchExtractorFunction = init.config.createExtractorRegex(configFile.extractorFunction);
291
+ }
292
+ else {
293
+ init.config.useExtractorFunction = true;
294
+ init.config.matchExtractorFunction = init.config.createExtractorRegex('String');
295
+ }
296
+ // ---------Extend----------------
297
+ if (configFile.hasOwnProperty('extend') && typeof configFile.extend === 'object') {
298
+ const filteredObj = {};
299
+ Object.keys(configFile.extend).map((key) => {
300
+ // if (configFile.extend[key].hasOwnProperty('compiler') && (typeof configFile.extend[key].compiler === 'function')){
301
+ filteredObj[key] = configFile.extend[key];
302
+ // }
303
+ });
304
+ init.statementMaker.extend(filteredObj);
305
+ }
306
+ // -----------------useColon----------------------------
307
+ if (configFile.hasOwnProperty('useColon')) {
308
+ init.config.useColon = configFile.useColon;
309
+ }
310
+ // ---------------addMedia----------------
311
+ if (configFile.hasOwnProperty('media') && typeof configFile.media === 'object') {
312
+ init.statementMaker.addMedia(configFile.media);
313
+ }
314
+ // /Truncate File every time we bundle
315
+ if (configFile.hasOwnProperty('truncate') && configFile.truncate === false) {
316
+ // do nothing
317
+ }
318
+ else {
319
+ if (fs.existsSync(init.output))
320
+ fs.truncateSync(path.resolve(init.output));
321
+ writeStatementToFile(init.output, `@layer ${init.layers.join(', ')};`, 'Initializating @layer,');
322
+ }
323
+ // Custom CSS statement
324
+ if (configFile.hasOwnProperty('statement')) {
325
+ init.rawCSS = configFile.statement;
326
+ init.statementMaker.rawCSS = configFile.statement;
327
+ writeStatementToFile(init.output, configFile.statement, 'config.statement');
328
+ }
329
+ // Custom Groups bundling acss classnames in single class name
330
+ if (configFile.hasOwnProperty('group')) {
331
+ init.customGroupStatement = init.statementMaker.groupObj(configFile.group);
332
+ init.statementMaker.customGroupStatement = init.statementMaker.groupObj(configFile.group);
333
+ if (init.customGroupStatement)
334
+ writeStatementToFile(init.output, init.customGroupStatement, 'config.group');
335
+ }
336
+ }
337
+ // ----------------WATCH-------------------------------
338
+ export function watch(config) {
339
+ main(config);
340
+ const watcher = chokidar.watch(init.input, {
341
+ ignored: '*.css', // ignore dot-files
342
+ persistent: true
343
+ });
344
+ const compileFile = (file) => {
345
+ console.log(`--processing file : ${file}`);
346
+ createCSSFileByFile(file, true);
347
+ globalCSSCompiler();
348
+ postify(init.output, [postcssDiscardDuplicates], init.minify);
349
+ console.log('Done! ');
350
+ };
351
+ // Something to use when events are received.
352
+ const log = console.log.bind(console);
353
+ // Add event listeners.
354
+ watcher
355
+ .on('add', path => log(`File ${path} has been added to watch`))
356
+ .on('change', path => compileFile(path));
357
+ // .on('unlink', path => log(`File ${path} has been removed`));
358
+ // More possible events.
359
+ watcher
360
+ .on('addDir', path => log(`Directory ${path} has been added`))
361
+ .on('unlinkDir', path => log(`Directory ${path} has been removed`))
362
+ // .on('error', error => log(`Watcher error: ${error}`))
363
+ // .on('ready', () => log('Initial scan complete. Ready for changes'))
364
+ .on('raw', (event, path, details) => {
365
+ // log('Raw event info:', event, path, details);
366
+ if (event === 'created' && details.type === 'file') {
367
+ if (init.globPattern) {
368
+ const files = fg.sync(init.globPattern, { absolute: true });
369
+ if (files.indexOf(path) !== -1) {
370
+ watcher.add(path);
371
+ // console.log(path,123123);
372
+ }
373
+ }
374
+ else {
375
+ watcher.add(path);
376
+ }
377
+ }
378
+ });
379
+ }
@@ -1,27 +1,27 @@
1
1
  export default function main(config: {
2
- ['input']: string | string[];
3
2
  [key: string]: any;
4
- }): void;
5
- export declare function globalCSSCompiler(): void;
3
+ input: string | string[];
4
+ }): Promise<void>;
5
+ export declare function globalCSSCompiler(): Promise<void>;
6
6
  export declare function compileFile(file: string, config: any, statementMaker: {
7
- ['make']: (a: string) => string;
7
+ make: (a: string, b?: any, c?: any) => string;
8
8
  group: (a: string, b: string) => string;
9
- ['rawCSS']?: string;
10
- ['customGroupStatement']?: string;
9
+ rawCSS?: string;
10
+ customGroupStatement?: string;
11
11
  }, globalClassList: string[]): string[];
12
12
  export declare function groupStatement(name: string, classNames: string, statementMaker: {
13
- ['group']: (a: string, b: string) => string;
13
+ group: (a: string, b: string) => string;
14
14
  }): string;
15
15
  export declare function keyframesStatement(name: string, timeLine: string, statementMaker: {
16
- ['make']: (a: string, b?: string | null, c?: boolean) => string;
16
+ make: (a: string, b?: string | null, c?: boolean) => string;
17
17
  }): string;
18
- export declare function createCSSFileByFile(file: string, bool?: boolean): void;
19
- export declare function postify(file: string, plugin: any[], minify?: boolean, sort?: "mobile-first" | "desktop-first"): void;
18
+ export declare function createCSSFileByFile(file: string, bool?: boolean): Promise<void>;
19
+ export declare function postify(file: string, plugin: any[], minify?: boolean, sort?: "mobile-first" | "desktop-first"): Promise<void>;
20
20
  export declare function writeStatementToFile(file: string, content: string, from?: string): void;
21
21
  export declare function initialize(configFile: {
22
22
  [key: string]: any;
23
23
  }): void;
24
24
  export declare function watch(config: {
25
- ['input']: string | string[];
26
25
  [key: string]: any;
26
+ input: string | string[];
27
27
  }): void;