Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持生成.vue文件的类型 #334

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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