fix(@vant/cli): #8890 sfc compile error (#8914)

This commit is contained in:
zoy-l 2021-06-23 16:38:14 +08:00 committed by GitHub
parent 6af1d38a3b
commit ec0f16b2aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,8 @@ import { compileJs } from './compile-js';
import { compileStyle } from './compile-style'; import { compileStyle } from './compile-style';
const RENDER_FN = '__vue_render__'; const RENDER_FN = '__vue_render__';
const EXPORT = 'export default {'; const VUEIDS = '__vue_sfc__';
const EXPORT = 'export default';
// trim some unused code // trim some unused code
function trim(code: string) { function trim(code: string) {
@ -25,14 +26,14 @@ function injectRender(script: string, render: string) {
render = render.replace('export function render', `function ${RENDER_FN}`); render = render.replace('export function render', `function ${RENDER_FN}`);
return script.replace( script += `\n${render}\n${VUEIDS}.render = ${RENDER_FN} \n`;
EXPORT,
`${render}\n${EXPORT}\n render: ${RENDER_FN},\n` return script;
);
} }
function injectScopeId(script: string, scopeId: string) { function injectScopeId(script: string, scopeId: string) {
return script.replace(EXPORT, `${EXPORT}\n _scopeId: '${scopeId}',\n\n`); script += `\n${VUEIDS}._scopeId = '${scopeId}'`;
return script;
} }
function injectStyle(script: string, styles: SFCBlock[], filePath: string) { function injectStyle(script: string, styles: SFCBlock[], filePath: string) {
@ -44,7 +45,9 @@ function injectStyle(script: string, styles: SFCBlock[], filePath: string) {
}) })
.join('\n'); .join('\n');
return script.replace(EXPORT, `${imports}\n\n${EXPORT}`); script = `${imports}\n${script}`;
return script;
} }
return script; return script;
@ -63,6 +66,7 @@ export async function compileSfc(filePath: string): Promise<any> {
const tasks = [remove(filePath)]; const tasks = [remove(filePath)];
const source = readFileSync(filePath, 'utf-8'); const source = readFileSync(filePath, 'utf-8');
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);
@ -78,12 +82,15 @@ export async function compileSfc(filePath: string): Promise<any> {
let script = descriptor.script!.content; let script = descriptor.script!.content;
script = injectStyle(script, styles, filePath); script = injectStyle(script, styles, filePath);
script = script.replace(EXPORT, `const ${VUEIDS} =`);
if (template) { if (template) {
const render = compileTemplate({ const render = compileTemplate({
id: scopeId, id: scopeId,
source: template.content, source: template.content,
filename: filePath, filename: filePath,
}).code; }).code;
script = injectRender(script, render); script = injectRender(script, render);
} }
@ -91,6 +98,8 @@ export async function compileSfc(filePath: string): Promise<any> {
script = injectScopeId(script, scopeId); script = injectScopeId(script, scopeId);
} }
script += `\n${EXPORT} ${VUEIDS}`;
writeFileSync(scriptFilePath, script); writeFileSync(scriptFilePath, script);
compileJs(scriptFilePath).then(resolve).catch(reject); compileJs(scriptFilePath).then(resolve).catch(reject);
}) })