fix(cli): should replace css import extension

This commit is contained in:
陈嘉涵 2020-01-10 14:17:12 +08:00
parent f4d10df14d
commit 6074baa28d

View File

@ -1,11 +1,25 @@
// @ts-ignore
import { transformFileAsync } from '@babel/core';
import { removeSync, outputFileSync } from 'fs-extra';
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) => {
transformFileAsync(filePath)
let code = readFileSync(filePath, 'utf-8');
code = replaceStyleImport(code);
transformAsync(code, { filename: filePath })
.then(result => {
if (result) {
const jsFilePath = replaceExt(filePath, '.js');