Skip to content

Commit

Permalink
fix(cli): 修改.vue文件编译规则
Browse files Browse the repository at this point in the history
affects: @varlet/cli
  • Loading branch information
haoziqaq committed Nov 9, 2020
1 parent fe23083 commit 71ab372
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/varlet-cli/src/commands/compile.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion packages/varlet-cli/src/compiler/compileSFC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down
25 changes: 24 additions & 1 deletion packages/varlet-cli/src/compiler/compileScript.ts
Original file line number Diff line number Diff line change
@@ -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 = /((?<!['"`])import\s+.+from\s+['"]\s*\.{1,2}\/.+)\.vue(\s*['"`])(?!\s*['"`])/g
export const REQUIRE_VUE_PATH_RE = /(?<!['"`]\s*)(require\s*\(\s*['"]\.{1,2}\/.+)\.vue(\s*['"`]\))(?!\s*['"`])/g
export const IMPORT_TS_PATH_RE = /((?<!['"`])import\s+.+from\s+['"]\s*\.{1,2}\/.+\.)ts(\s*['"`])(?!\s*['"`])/g
export const REQUIRE_TS_PATH_RE = /(?<!['"`]\s*)(require\s*\(\s*['"]\.{1,2}\/.+\.)ts(\s*['"`]\))(?!\s*['"`])/g

export function replaceVueExt(script: string) {
const replacer = (_: any, p1: string, p2: string): string => {
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, {
Expand Down
22 changes: 0 additions & 22 deletions packages/varlet-cli/src/compiler/compileStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { replaceExt } from '../shared/fsUtils'

export const IMPORT_LESS_PATH_RE = /((?<!['"`])import\s+['"]\s*\.{1,2}\/.+\.)less(\s*['"`])(?!\s*['"`])/g
export const REQUIRE_LESS_PATH_RE = /(?<!['"`]\s*)(require\s*\(\s*['"]\.{1,2}\/.+\.)less(\s*['"`]\))(?!\s*['"`])/g
export const IMPORT_VUE_PATH_RE = /((?<!['"`])import\s+.+from\s+['"]\s*\.{1,2}\/.+\.)vue(\s*['"`])(?!\s*['"`])/g
export const REQUIRE_VUE_PATH_RE = /(?<!['"`]\s*)(require\s*\(\s*['"]\.{1,2}\/.+\.)vue(\s*['"`]\))(?!\s*['"`])/g
export const IMPORT_TS_PATH_RE = /((?<!['"`])import\s+.+from\s+['"]\s*\.{1,2}\/.+\.)ts(\s*['"`])(?!\s*['"`])/g
export const REQUIRE_TS_PATH_RE = /(?<!['"`]\s*)(require\s*\(\s*['"]\.{1,2}\/.+\.)ts(\s*['"`]\))(?!\s*['"`])/g
export const EMPTY_SPACE_RE = /[\s]+/g
export const EMPTY_LINE_RE = /[\n\r]*/g

Expand All @@ -22,24 +18,6 @@ export function replaceStyleExt(script: string) {
.replace(REQUIRE_LESS_PATH_RE, replacer)
}

export function replaceVueExt(script: string) {
const replacer = (_: any, p1: string, p2: string): string => {
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, '')
Expand Down

0 comments on commit 71ab372

Please sign in to comment.