import { EXT_NAME, GLOB_REG, COLORS, UNIT, IMPORTANT, OVERRIDE_RULES, MEDIA_QUERIES, BEFORE_CONTENTS, CONFIG_FILE_NAME } from '../constant'; const fs = require('fs'); const path = require('path'); let programConfig = { [OVERRIDE_RULES]: {} } as any; const runType = { uniapp: { [EXT_NAME]: ['vue'], reg: /(?:(?<=class=(["']))[\s\S]*?(?=\1))|((?<=class={)[\s\S]*?(?=}))/gi } }; /** * 获取配置信息 * @param str * @returns */ export const getConfig = (str: string | number): any => { switch (str) { // 配置默认值 case EXT_NAME: return programConfig[EXT_NAME] || programConfig[programConfig.type] || runType[programConfig.type][EXT_NAME]; case GLOB_REG: return runType[programConfig.type].reg; case COLORS: return programConfig[COLORS] || {}; case UNIT: return programConfig[UNIT] || 'px'; case MEDIA_QUERIES: return programConfig[MEDIA_QUERIES] || {} case IMPORTANT: return programConfig[IMPORTANT] === undefined ? true : programConfig[IMPORTANT] case BEFORE_CONTENTS: return programConfig[BEFORE_CONTENTS] || '/* stylelint-disable */' default: return programConfig[str]; } } /** * 设置配置信息 * @param config */ export const setConfig = (config: any): any => { programConfig = config; } /** * 获取设置的单位 * @param number * @param str * @returns */ export const getUnit = (number: string | number, str: string) => { if(+number === 0) { return ''; } if (str === 'p') { return '%'; } if (!str) { return programConfig[UNIT] || 'px'; } return str; } const getFilePath = (str: any) => { return path.resolve(process.cwd(), str); } export const readConfigFile = () => { let options = null; if (fs.existsSync(getFilePath(CONFIG_FILE_NAME))) { options = require(getFilePath(CONFIG_FILE_NAME)); // options = getFilePath(CONFIG_FILE_NAME); } else { throw new Error(`请先创建autocss配置文件:${CONFIG_FILE_NAME}。`); } return options; }