diff --git a/packages/varlet-cli/src/commands/compile.ts b/packages/varlet-cli/src/commands/compile.ts index 4fde51a7987..712e717eec4 100644 --- a/packages/varlet-cli/src/commands/compile.ts +++ b/packages/varlet-cli/src/commands/compile.ts @@ -1,7 +1,7 @@ import logger from '../shared/logger' import ora from 'ora' import { compileES } from '../compiler/compileES' -import { copy, readdir, remove } from 'fs-extra' +import { copy, remove } from 'fs-extra' import { CJS_DIR, ES_DIR, SRC_DIR } from '../shared/constant' import { compileCJS } from '../compiler/compileCJS' import { watch } from 'chokidar' diff --git a/packages/varlet-cli/src/compiler/compileSFC.ts b/packages/varlet-cli/src/compiler/compileSFC.ts index d1fe5109203..e527bb6d17a 100644 --- a/packages/varlet-cli/src/compiler/compileSFC.ts +++ b/packages/varlet-cli/src/compiler/compileSFC.ts @@ -43,7 +43,7 @@ export async function compileSFC(path: string, modules: string | boolean = false const { code } = render content = injectRender(content, code) // script - await compileScript(content, path, modules) + await compileScript(content, replaceExt(path, 'Sfc.ts'), modules) // style styles.forEach((style: SFCStyleBlock, index: number) => { const stylePath = replaceExt(path, `Sfc${index === 0 ? '' : index}.${style.lang}`) diff --git a/packages/varlet-cli/src/compiler/compileScript.ts b/packages/varlet-cli/src/compiler/compileScript.ts index 040595a45ff..ac2747fea56 100644 --- a/packages/varlet-cli/src/compiler/compileScript.ts +++ b/packages/varlet-cli/src/compiler/compileScript.ts @@ -1,9 +1,32 @@ import { BabelFileResult, transformAsync } from '@babel/core' import { replaceExt } from '../shared/fsUtils' -import { replaceStyleExt, replaceTSExt, replaceVueExt } from './compileStyle' +import { replaceStyleExt } from './compileStyle' import { writeFileSync, readFileSync, removeSync } from 'fs-extra' import logger from '../shared/logger' +export const IMPORT_VUE_PATH_RE = /((? { + return `${p1}Sfc.js${p2}` + } + return script + .replace(IMPORT_VUE_PATH_RE, replacer) + .replace(REQUIRE_VUE_PATH_RE, replacer) +} + +export function replaceTSExt(script: string) { + const replacer = (_: any, p1: string, p2: string): string => { + return `${p1}js${p2}` + } + return script + .replace(IMPORT_TS_PATH_RE, replacer) + .replace(REQUIRE_TS_PATH_RE, replacer) +} + export async function compileScript(script: string, path: string, modules: string | boolean = false) { try { let { code } = await transformAsync(script, { diff --git a/packages/varlet-cli/src/compiler/compileStyle.ts b/packages/varlet-cli/src/compiler/compileStyle.ts index 426038abe1c..cb341b885fa 100644 --- a/packages/varlet-cli/src/compiler/compileStyle.ts +++ b/packages/varlet-cli/src/compiler/compileStyle.ts @@ -6,10 +6,6 @@ import { replaceExt } from '../shared/fsUtils' export const IMPORT_LESS_PATH_RE = /((? { - return `${p1}js${p2}` - } - return script - .replace(IMPORT_VUE_PATH_RE, replacer) - .replace(REQUIRE_VUE_PATH_RE, replacer) -} - -export function replaceTSExt(script: string) { - const replacer = (_: any, p1: string, p2: string): string => { - return `${p1}js${p2}` - } - return script - .replace(IMPORT_TS_PATH_RE, replacer) - .replace(REQUIRE_TS_PATH_RE, replacer) -} - export function clearEmptyLine(style: string) { return style .replace(EMPTY_LINE_RE, '')