|
| 1 | +import { extractICSS, IICSSExports } from 'icss-utils'; |
| 2 | +import * as postcss from 'postcss'; |
| 3 | +import * as ts_module from 'typescript/lib/tsserverlibrary'; |
| 4 | +import * as less from 'less'; |
| 5 | +import * as sass from 'sass'; |
| 6 | +import * as reserved from 'reserved-words'; |
| 7 | +import { transformClasses } from './classTransforms'; |
| 8 | +import { Options } from '../options'; |
| 9 | +import { Logger } from './logger'; |
| 10 | + |
| 11 | +const NOT_CAMELCASE_REGEXP = /[\-_]/; |
| 12 | + |
| 13 | +const classNameToProperty = (className: string) => `'${className}': string;`; |
| 14 | +const classNameToNamedExport = (className: string) => |
| 15 | + `export const ${className}: string;`; |
| 16 | + |
| 17 | +const flattenClassNames = ( |
| 18 | + previousValue: string[] = [], |
| 19 | + currentValue: string[], |
| 20 | +) => previousValue.concat(currentValue); |
| 21 | + |
| 22 | +export const enum FileTypes { |
| 23 | + css = 'css', |
| 24 | + less = 'less', |
| 25 | + scss = 'scss', |
| 26 | +} |
| 27 | + |
| 28 | +export const getFileType = (fileName: string) => { |
| 29 | + if (fileName.endsWith('.css')) return FileTypes.css; |
| 30 | + if (fileName.endsWith('.less')) return FileTypes.less; |
| 31 | + return FileTypes.scss; |
| 32 | +}; |
| 33 | + |
| 34 | +const getFilePath = (fileName: string) => |
| 35 | + fileName.substring(0, fileName.lastIndexOf('/')); |
| 36 | + |
| 37 | +export class DtsSnapshotCreator { |
| 38 | + constructor(private readonly logger: Logger) {} |
| 39 | + |
| 40 | + getClasses(processor: postcss.Processor, css: string, fileName: string) { |
| 41 | + try { |
| 42 | + const fileType = getFileType(fileName); |
| 43 | + let transformedCss = ''; |
| 44 | + |
| 45 | + if (fileType === FileTypes.less) { |
| 46 | + less.render(css, { asyncImport: true } as any, (err, output) => { |
| 47 | + transformedCss = output.css.toString(); |
| 48 | + }); |
| 49 | + } else if (fileType === FileTypes.scss) { |
| 50 | + const filePath = getFilePath(fileName); |
| 51 | + transformedCss = sass |
| 52 | + .renderSync({ |
| 53 | + data: css, |
| 54 | + includePaths: [filePath], |
| 55 | + }) |
| 56 | + .css.toString(); |
| 57 | + } else { |
| 58 | + transformedCss = css; |
| 59 | + } |
| 60 | + |
| 61 | + const processedCss = processor.process(transformedCss); |
| 62 | + |
| 63 | + return processedCss.root |
| 64 | + ? extractICSS(processedCss.root).icssExports |
| 65 | + : {}; |
| 66 | + } catch (e) { |
| 67 | + this.logger.error(e); |
| 68 | + return {}; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + createExports(classes: IICSSExports, options: Options) { |
| 73 | + const isCamelCase = (className: string) => |
| 74 | + !NOT_CAMELCASE_REGEXP.test(className); |
| 75 | + const isReservedWord = (className: string) => !reserved.check(className); |
| 76 | + |
| 77 | + const processedClasses = Object.keys(classes) |
| 78 | + .map(transformClasses(options.camelCase)) |
| 79 | + .reduce(flattenClassNames, []); |
| 80 | + const camelCasedKeys = processedClasses |
| 81 | + .filter(isCamelCase) |
| 82 | + .filter(isReservedWord) |
| 83 | + .map(classNameToNamedExport); |
| 84 | + |
| 85 | + const defaultExport = `\ |
| 86 | +declare const classes: { |
| 87 | + ${processedClasses.map(classNameToProperty).join('\n ')} |
| 88 | +}; |
| 89 | +export default classes; |
| 90 | +`; |
| 91 | + |
| 92 | + if (camelCasedKeys.length) { |
| 93 | + return defaultExport + camelCasedKeys.join('\n') + '\n'; |
| 94 | + } |
| 95 | + return defaultExport; |
| 96 | + } |
| 97 | + |
| 98 | + getDtsSnapshot( |
| 99 | + ts: typeof ts_module, |
| 100 | + processor: postcss.Processor, |
| 101 | + fileName: string, |
| 102 | + scriptSnapshot: ts.IScriptSnapshot, |
| 103 | + options: Options, |
| 104 | + ) { |
| 105 | + const css = scriptSnapshot.getText(0, scriptSnapshot.getLength()); |
| 106 | + |
| 107 | + // FIXME: Temporary workaround for https://github.com/mrmckeb/typescript-plugin-css-modules/issues/41 |
| 108 | + // Needs investigation for a more elegant solution. |
| 109 | + if (/export default classes/.test(css)) { |
| 110 | + return scriptSnapshot; |
| 111 | + } |
| 112 | + |
| 113 | + const classes = this.getClasses(processor, css, fileName); |
| 114 | + const dts = this.createExports(classes, options); |
| 115 | + return ts.ScriptSnapshot.fromString(dts); |
| 116 | + } |
| 117 | +} |
0 commit comments