35 lines
974 B
TypeScript
35 lines
974 B
TypeScript
// @ts-ignore
|
|
import { transformAsync } from '@babel/core';
|
|
import { readFileSync, removeSync, outputFileSync } from 'fs-extra';
|
|
import { replaceExt } from '../common';
|
|
import { CSS_LANG } from '../common/css';
|
|
|
|
const IMPORT_STYLE_RE = /import\s+?(?:(?:".*?")|(?:'.*?'))[\s]*?(?:;|$|)/g;
|
|
|
|
// "import 'a.less';" => "import 'a.css';"
|
|
function replaceStyleImport(code: string) {
|
|
return code.replace(IMPORT_STYLE_RE, str =>
|
|
str.replace(`.${CSS_LANG}`, '.css')
|
|
);
|
|
}
|
|
|
|
export function compileJs(filePath: string): Promise<undefined> {
|
|
return new Promise((resolve, reject) => {
|
|
let code = readFileSync(filePath, 'utf-8');
|
|
|
|
code = replaceStyleImport(code);
|
|
|
|
transformAsync(code, { filename: filePath })
|
|
.then(result => {
|
|
if (result) {
|
|
const jsFilePath = replaceExt(filePath, '.js');
|
|
|
|
removeSync(filePath);
|
|
outputFileSync(jsFilePath, result.code);
|
|
resolve();
|
|
}
|
|
})
|
|
.catch(reject);
|
|
});
|
|
}
|