Skip to content

Commit

Permalink
fix(cli): fix gen style error
Browse files Browse the repository at this point in the history
affects: @varlet/cli
  • Loading branch information
haoziqaq committed Jul 17, 2021
1 parent 83258b5 commit 4f7dade
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
6 changes: 5 additions & 1 deletion packages/varlet-cli/src/compiler/compileModule.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)
})
)
Expand Down
14 changes: 8 additions & 6 deletions packages/varlet-cli/src/compiler/compileSFC.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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, '')
}
Expand Down
1 change: 0 additions & 1 deletion packages/varlet-cli/src/compiler/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/varlet-cli/src/shared/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 0 additions & 5 deletions packages/varlet-cli/src/shared/fsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}

0 comments on commit 4f7dade

Please sign in to comment.