chore(cli): split compilers

This commit is contained in:
陈嘉涵
2019-11-29 13:20:43 +08:00
parent e2c9628316
commit 51d2512311
9 changed files with 90 additions and 74 deletions
+19 -36
View File
@@ -1,46 +1,29 @@
import postcss from 'postcss';
import postcssrc from 'postcss-load-config';
import { parse } from 'path';
import { render as renderLess } from 'less';
import { renderSync as renderSass } from 'sass';
import { readFileSync, writeFileSync } from 'fs';
import { replaceExt } from '../common';
import { POSTCSS_CONFIG_FILE } from '../common/constant';
import { compileCss } from './compile-css';
import { compileLess } from './compile-less';
import { compileSass } from './compile-sass';
async function compilePostcss(filePath: string, source: string | Buffer) {
const config = await postcssrc({}, POSTCSS_CONFIG_FILE);
const output = await postcss(config.plugins as any).process(source, {
from: undefined
});
writeFileSync(filePath, output);
}
async function compileLess(filePath: string) {
const source = readFileSync(filePath, 'utf-8');
const { css } = await renderLess(source, {
filename: filePath
});
return css;
}
async function compileSass(filePath: string) {
const { css } = renderSass({ file: filePath });
return css;
}
export async function compileStyle(filePath: string) {
async function compileFile(filePath: string) {
const parsedPath = parse(filePath);
if (parsedPath.ext === '.less') {
const source = await compileLess(filePath);
await compilePostcss(replaceExt(filePath, '.css'), source);
} else if (parsedPath.ext === '.scss') {
const source = await compileSass(filePath);
await compilePostcss(replaceExt(filePath, '.css'), source);
} else {
const source = readFileSync(filePath, 'utf-8');
await compilePostcss(filePath, source);
return compileCss(source);
}
if (parsedPath.ext === '.scss') {
const source = await compileSass(filePath);
return compileCss(source);
}
const source = readFileSync(filePath, 'utf-8');
return compileCss(source);
}
export async function compileStyle(filePath: string) {
const content = await compileFile(filePath);
writeFileSync(replaceExt(filePath, '.css'), content);
}