mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
fix(cli): failed to import sfc after build
This commit is contained in:
parent
764e9e2d69
commit
ac856c2673
@ -38,7 +38,7 @@ export function getCssBaseFile() {
|
|||||||
const IMPORT_STYLE_RE = /import\s+?(?:(?:".*?")|(?:'.*?'))[\s]*?(?:;|$|)/g;
|
const IMPORT_STYLE_RE = /import\s+?(?:(?:".*?")|(?:'.*?'))[\s]*?(?:;|$|)/g;
|
||||||
|
|
||||||
// "import 'a.less';" => "import 'a.css';"
|
// "import 'a.less';" => "import 'a.css';"
|
||||||
export function replaceCssImport(code: string) {
|
export function replaceCssImportExt(code: string) {
|
||||||
return code.replace(IMPORT_STYLE_RE, str =>
|
return code.replace(IMPORT_STYLE_RE, str =>
|
||||||
str.replace(`.${CSS_LANG}`, '.css')
|
str.replace(`.${CSS_LANG}`, '.css')
|
||||||
);
|
);
|
||||||
|
@ -1,16 +1,18 @@
|
|||||||
import { transformAsync } from '@babel/core';
|
import { transformAsync } from '@babel/core';
|
||||||
import { readFileSync, removeSync, outputFileSync } from 'fs-extra';
|
import { readFileSync, removeSync, outputFileSync } from 'fs-extra';
|
||||||
import { replaceExt } from '../common';
|
import { replaceExt } from '../common';
|
||||||
import { replaceCssImport } from '../common/css';
|
import { replaceCssImportExt } from '../common/css';
|
||||||
|
import { replaceScriptImportExt } from './get-deps';
|
||||||
|
|
||||||
export function compileJs(filePath: string): Promise<undefined> {
|
export function compileJs(filePath: string): Promise<undefined> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let code = readFileSync(filePath, 'utf-8');
|
let code = readFileSync(filePath, 'utf-8');
|
||||||
|
|
||||||
code = replaceCssImport(code);
|
code = replaceCssImportExt(code);
|
||||||
|
code = replaceScriptImportExt(code, '.vue', '');
|
||||||
|
|
||||||
transformAsync(code, { filename: filePath })
|
transformAsync(code, { filename: filePath })
|
||||||
.then(result => {
|
.then((result) => {
|
||||||
if (result) {
|
if (result) {
|
||||||
const jsFilePath = replaceExt(filePath, '.js');
|
const jsFilePath = replaceExt(filePath, '.js');
|
||||||
|
|
||||||
|
@ -87,7 +87,7 @@ export async function compileSfc(filePath: string): Promise<any> {
|
|||||||
const descriptor = parseSfc(filePath);
|
const descriptor = parseSfc(filePath);
|
||||||
const { template, styles } = descriptor;
|
const { template, styles } = descriptor;
|
||||||
|
|
||||||
const hasScoped = styles.some(s => s.scoped);
|
const hasScoped = styles.some((s) => s.scoped);
|
||||||
const scopeId = hasScoped ? `data-v-${hash(source)}` : '';
|
const scopeId = hasScoped ? `data-v-${hash(source)}` : '';
|
||||||
|
|
||||||
// compile js part
|
// compile js part
|
||||||
@ -107,9 +107,7 @@ export async function compileSfc(filePath: string): Promise<any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
writeFileSync(jsFilePath, script);
|
writeFileSync(jsFilePath, script);
|
||||||
compileJs(jsFilePath)
|
compileJs(jsFilePath).then(resolve).catch(reject);
|
||||||
.then(resolve)
|
|
||||||
.catch(reject);
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -71,3 +71,15 @@ export function getDeps(filePath: string) {
|
|||||||
|
|
||||||
return paths;
|
return paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// "import App from 'App.vue';" => "import App from 'App.xxx';"
|
||||||
|
export function replaceScriptImportExt(code: string, from: string, to: string) {
|
||||||
|
const importLines = matchImports(code);
|
||||||
|
|
||||||
|
importLines.forEach(importLine => {
|
||||||
|
const result = importLine.replace(from, to);
|
||||||
|
code = code.replace(importLine, result);
|
||||||
|
});
|
||||||
|
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user