index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import path from 'path';
  2. import fs from 'fs-extra';
  3. import os from 'os';
  4. import postcss from "postcss";
  5. import postcssScss from 'postcss-scss';
  6. import { getScssVariableNotUsedInSelectorSetPlugin,transVarPlugin } from "./transVarPlugin";
  7. export interface Options {
  8. sourcePath: string,
  9. resultPath: string
  10. }
  11. const getAllScssFilesInPath = (filePath: string) => {
  12. if (!path.isAbsolute(filePath)) {
  13. filePath = path.resolve(filePath);
  14. }
  15. const isScss = (filePath: string) => path.extname(filePath) === '.scss';
  16. const stat = fs.lstatSync(filePath);
  17. if (stat.isSymbolicLink()) {
  18. return [];
  19. }
  20. if (stat.isFile()) {
  21. if (isScss(filePath)) {
  22. return [filePath];
  23. } else {
  24. return [];
  25. }
  26. }
  27. const scssFilePaths: string[] = [];
  28. const fileList = fs.readdirSync(filePath);
  29. fileList.forEach(filename => {
  30. const pathOfCurrentFile = path.join(filePath, filename);
  31. const stat = fs.lstatSync(pathOfCurrentFile);
  32. if (stat.isSymbolicLink()) {
  33. return;
  34. } else if (stat.isFile()) {
  35. if (isScss(pathOfCurrentFile)) {
  36. scssFilePaths.push(pathOfCurrentFile);
  37. }
  38. } else {
  39. scssFilePaths.push(...getAllScssFilesInPath(pathOfCurrentFile));
  40. }
  41. });
  42. return scssFilePaths;
  43. };
  44. // const prepareTransDir = (sourcePath: string) => {
  45. // const tempDirPath = path.join(os.tmpdir(), `semi_scss_to_css_vars_${Math.random().toString(36).slice(-6)}`);
  46. // fs.copySync(sourcePath, tempDirPath);
  47. // return tempDirPath;
  48. // }
  49. const transScssToCSSVar=(scssFilePathList:string[])=>{
  50. //scssFilePathList=['./test/test.scss'];
  51. let allCssDefine:{key:string,value:string}[] =[];
  52. for (const scssFilePath of scssFilePathList){
  53. try {
  54. const raw=fs.readFileSync(scssFilePath,{ encoding:'utf-8' });
  55. const scssVariableInSelectorSet = new Set<string>();
  56. postcss([getScssVariableNotUsedInSelectorSetPlugin(scssVariableInSelectorSet)]).process(raw, { syntax: postcssScss }).css;
  57. const cssDefine :{key:string,value:string}[]=[];
  58. const result=postcss([transVarPlugin(scssVariableInSelectorSet,cssDefine)]).process(raw, { syntax: postcssScss });
  59. fs.writeFileSync(scssFilePath,result.css,"utf8");
  60. allCssDefine=[...allCssDefine,...cssDefine];
  61. } catch (e){
  62. console.error(e);
  63. console.error(`Error While processing ${scssFilePath}`);
  64. }
  65. }
  66. fs.writeFileSync('allCSSVar.scss',(()=>{
  67. return `.allCSSVar{\n${allCssDefine.map(({ key,value })=>{return `${key}:${value};`;}).join('\n')}\n}`;
  68. })(),'utf-8');
  69. };
  70. const transScssVariables2CssVariables = ({ sourcePath, resultPath }: Options) => {
  71. const transDir = sourcePath;
  72. const scssFileList = getAllScssFilesInPath(transDir);
  73. transScssToCSSVar(scssFileList);
  74. return scssFileList;
  75. };
  76. export {
  77. transScssVariables2CssVariables
  78. };