Skip to content

Commit

Permalink
feat: 支持生成.vue文件的类型
Browse files Browse the repository at this point in the history
  • Loading branch information
MAXLZ1 committed Dec 29, 2022
1 parent 93e68e9 commit e1f4ae9
Show file tree
Hide file tree
Showing 13 changed files with 5,855 additions and 3,152 deletions.
2 changes: 2 additions & 0 deletions packages/emp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@svgr/webpack": "^6.3.0",
"@types/express": "^4.17.13",
"@types/node": "^16.11.9",
"@vue/compiler-sfc": "^3.2.45",
"address": "^1.1.2",
"axios": "^0.27.2",
"babel-loader": "^8.2.5",
Expand Down Expand Up @@ -107,6 +108,7 @@
"typescript": "^4.7.3",
"typescript-plugin-css-modules": "^3.4.0",
"url-loader": "^4.1.1",
"vue-tsc": "^1.0.18",
"webpack": "^5.75.0",
"webpack-bundle-analyzer": "^4.5.0",
"webpack-chain": "^6.5.1",
Expand Down
28 changes: 25 additions & 3 deletions packages/emp/src/dts/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import store from 'src/helper/store'
import fs from 'fs-extra'
import {transformExposesPath, transformImportExposesPath, transformLibName, transformPathImport} from './transform'
import {ConfigResolveAliasType} from 'src/types'
import {getVueTsService} from './getVueTsService'
import {parse} from '@vue/compiler-sfc'

//
export type DTSTLoadertype = {
build: RquireBuildOptions
Expand All @@ -33,6 +36,7 @@ type CodeObjType = {
class DTSEmitFile {
outDir: string
languageService: ts.LanguageService
vueLanguageService: ts.LanguageService
lib: CodeObjType = {code: '', key: []}
tsconfig: ts.CompilerOptions
empFilename = ''
Expand All @@ -51,6 +55,7 @@ class DTSEmitFile {
// baseUrl: store.config.appSrc,
}
this.languageService = getTSService(this.tsconfig, store.root)
this.vueLanguageService = getVueTsService(this.tsconfig, store.root)
}
setup(op: DTSOptionsType) {
this.op = op
Expand All @@ -62,8 +67,25 @@ class DTSEmitFile {
}
this.op.needClear && fs.removeSync(this.outDir)
}
emit(filename: string) {
const output = this.languageService.getEmitOutput(filename)
async emit(filename: string) {
let output: ts.EmitOutput
if (filename.endsWith('.vue')) {
const content = await fs.promises.readFile(filename, 'utf-8')
const sfc = parse(content)
const { script, scriptSetup } = sfc.descriptor
if (script || scriptSetup) {
let lang = scriptSetup?.lang || script?.lang || 'js'
if (/jsx?/.test(lang)) {
return
}
output = this.vueLanguageService.getEmitOutput(`${filename}.${lang}`)
} else {
return
}
} else {
output = this.languageService.getEmitOutput(filename)
}

try {
if (!output.emitSkipped) {
output.outputFiles.forEach(o => {
Expand Down Expand Up @@ -103,7 +125,7 @@ class DTSEmitFile {
genCode(o: ts.OutputFile) {
if (!this.op.build) return
if (!this.lib.key.includes(o.name)) {
let mod = o.name.split(`/${this.op.build.typesOutDir}/`)[1].replace('.d.ts', '')
let mod = o.name.split(`/${this.op.build.typesOutDir}/`)[1].replace(/(\.vue)?\.d\.ts/, '')
if (mod.endsWith('/index')) {
mod = mod.replace('/index', '')
}
Expand Down
8 changes: 4 additions & 4 deletions packages/emp/src/dts/dtsThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ parentPort.on('message', async (payload: any) => {
if (options) {
const dts = new DTSEmitFile()
dts.setup(options)
const pathStr = path.join(appAbsSrc, '**/*.(ts|tsx)').replace(/\\/g, '/')
const pathStr = path.join(appAbsSrc, '**/*.(ts|tsx|vue)').replace(/\\/g, '/')
const dtslist = await glob([pathStr])
// console.log('dtslist', dtslist, appSrc, `${appSrc}/**/*.(ts|tsx)`)
dtslist.map(d => {
dts.emit(d)
})
await Promise.all(dtslist.map(async d => {
await dts.emit(d)
}))
dts.createFile()
parentPort.postMessage('finish')
}
Expand Down
8 changes: 6 additions & 2 deletions packages/emp/src/dts/getTSConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ const parseConfigHost = {
function getFileNames(cwd: string) {
const tsconfigPath = getTSConfigPath(cwd)
const tsconfig = getTSConfig(cwd)

if (tsconfigPath) {
const parsed = ts.parseJsonConfigFileContent(tsconfig, parseConfigHost, path.dirname(tsconfigPath))
const parsed = ts.parseJsonConfigFileContent(tsconfig, parseConfigHost, path.dirname(tsconfigPath), undefined, undefined, undefined, [{
extension: '.vue',
isMixedContent: true,
scriptKind: ts.ScriptKind.Deferred
}])
return parsed.fileNames
}
return []
Expand Down
2 changes: 1 addition & 1 deletion packages/emp/src/dts/getTSService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function getTSService(options: ts.CompilerOptions, cwd: string) {
return cache.languageService
}
cache.cwd = cwd
const rootFileNames = getFileNames(cwd)
const rootFileNames = getFileNames(cwd).filter(fileName => !fileName.endsWith('.vue'))

const files: ts.MapLike<{version: number}> = {}

Expand Down
32 changes: 32 additions & 0 deletions packages/emp/src/dts/getVueTsService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {createProgram} from 'vue-tsc'
import ts from 'typescript'
import { getFileNames } from './getTSConfig'

const cache: {
languageService?: ts.LanguageService
cwd: string
} = {cwd: ''}

function getVueTsService(options: ts.CompilerOptions, cwd: string) {
if (cache.languageService && cache.cwd === cwd) {
return cache.languageService
}
cache.cwd = cwd

const rootFileNames = getFileNames(cwd).filter(fileName => fileName.endsWith('.vue'))

const host = ts.createCompilerHost(options, undefined)

const program = createProgram({
rootNames: rootFileNames,
options,
host
})

const service = program.__vue.languageService.__internal__.languageService
cache.languageService = service

return service
}

export {getVueTsService}
Loading

0 comments on commit e1f4ae9

Please sign in to comment.