diff --git a/packages/varlet-cli/src/compiler/compileModule.ts b/packages/varlet-cli/src/compiler/compileModule.ts index 1c7afb08b64..6e4e8cc64b4 100644 --- a/packages/varlet-cli/src/compiler/compileModule.ts +++ b/packages/varlet-cli/src/compiler/compileModule.ts @@ -1,6 +1,6 @@ import webpack from 'webpack' import logger from '../shared/logger' -import { EXAMPLE_DIR_NAME, TESTS_DIR_NAME, DOCS_DIR_NAME, SRC_DIR, ES_DIR } from '../shared/constant' +import { EXAMPLE_DIR_NAME, TESTS_DIR_NAME, DOCS_DIR_NAME, SRC_DIR, ES_DIR, STYLE_DIR_NAME } from '../shared/constant' import { copy, ensureFileSync, readdir, removeSync } from 'fs-extra' import { getComponentNames, getExportDirNames, isDir, isLess, isScript, isSFC } from '../shared/fsUtils' import { compileSFC } from './compileSFC' @@ -38,6 +38,10 @@ export async function compileDir(dir: string) { ;[TESTS_DIR_NAME, EXAMPLE_DIR_NAME, DOCS_DIR_NAME].includes(filename) && removeSync(file) + if (filename === STYLE_DIR_NAME) { + return Promise.resolve() + } + return compileFile(file) }) ) diff --git a/packages/varlet-cli/src/compiler/compileSFC.ts b/packages/varlet-cli/src/compiler/compileSFC.ts index e92a025da64..7dfb9dd733a 100644 --- a/packages/varlet-cli/src/compiler/compileSFC.ts +++ b/packages/varlet-cli/src/compiler/compileSFC.ts @@ -1,7 +1,7 @@ import hash from 'hash-sum' -import { readFile, remove, writeFileSync } from 'fs-extra' +import { readFile, remove, writeFileSync, appendFileSync } from 'fs-extra' import { parse, compileTemplate, compileStyle } from '@vue/compiler-sfc' -import { appendContent, replaceExt } from '../shared/fsUtils' +import { replaceExt } from '../shared/fsUtils' import { compileScript } from './compileScript' import { clearEmptyLine, compileLess } from './compileStyle' import { resolve, parse as parsePath } from 'path' @@ -50,12 +50,14 @@ export function extractStyleDependencies(file: string, code: string) { styleImports.forEach((styleImport: string) => { const normalizedPath = normalizeStyleDependency(styleImport) - appendContent(cssFile, `import '${normalizedPath}.css'\n`) - ext === '.less' && appendContent(lessFile, `import '${normalizedPath}.less'\n`) + appendFileSync(cssFile, `import '${normalizedPath}.css'\n`) + if (ext === '.less') { + appendFileSync(lessFile, `import '${normalizedPath}.less'\n`) + } }) - appendContent(cssFile, `import '${normalizeStyleDependency(base)}.css'\n`) - ext === '.less' && appendContent(lessFile, `import '${normalizeStyleDependency(base)}.less'\n`) + appendFileSync(cssFile, `import '${normalizeStyleDependency(base)}.css'\n`) + ext === '.less' && appendFileSync(lessFile, `import '${normalizeStyleDependency(base)}.less'\n`) return code.replace(LESS_IMPORT_RE, '') } diff --git a/packages/varlet-cli/src/compiler/compileScript.ts b/packages/varlet-cli/src/compiler/compileScript.ts index e2fab802e59..41a0294d03f 100644 --- a/packages/varlet-cli/src/compiler/compileScript.ts +++ b/packages/varlet-cli/src/compiler/compileScript.ts @@ -37,7 +37,6 @@ export async function compileScript(script: string, path: string) { ], plugins: ['@babel/plugin-transform-runtime'], })) as BabelFileResult - code = replaceStyleExt(code as string) code = replaceVueExt(code as string) code = replaceTSExt(code as string) diff --git a/packages/varlet-cli/src/shared/constant.ts b/packages/varlet-cli/src/shared/constant.ts index 46d39fe66e6..192a7fc4d49 100644 --- a/packages/varlet-cli/src/shared/constant.ts +++ b/packages/varlet-cli/src/shared/constant.ts @@ -11,6 +11,7 @@ export const TYPES_DIR = resolve(CWD, 'types') export const ROOT_DOCS_DIR = resolve(CWD, 'docs') export const ESLINT_EXTENSIONS = ['.vue', '.ts', '.js'] export const EXTENSIONS = ['.vue', '.ts', '.js', '.less', '.css'] +export const STYLE_DIR_NAME = 'style' export const EXAMPLE_DIR_NAME = 'example' export const DOCS_DIR_NAME = 'docs' export const EXAMPLE_DIR_INDEX = 'index.vue' diff --git a/packages/varlet-cli/src/shared/fsUtils.ts b/packages/varlet-cli/src/shared/fsUtils.ts index ac198698b15..8c62ad2415c 100644 --- a/packages/varlet-cli/src/shared/fsUtils.ts +++ b/packages/varlet-cli/src/shared/fsUtils.ts @@ -68,8 +68,3 @@ export function bigCamelize(str: string): string { export function camelize(str: string): string { return str.replace(/-(\w)/g, (_: any, p: string) => p.toUpperCase()) } - -export function appendContent(file: string, content: string) { - const code = readFileSync(file) - writeFileSync(file, `${code}${content}`) -}