Skip to content

Commit

Permalink
chore: update vue compiler access method
Browse files Browse the repository at this point in the history
  • Loading branch information
qmhc committed Jan 11, 2022
1 parent 4684fb6 commit f59d8fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
31 changes: 23 additions & 8 deletions src/compile.ts
Expand Up @@ -4,23 +4,38 @@ const exportDefaultRE = /export\s+default/
const exportDefaultClassRE = /(?:(?:^|\n|;)\s*)export\s+default\s+class\s+([\w$]+)/

let index = 1
let compiler: typeof import('vue/compiler-sfc')
let compileRoot: string | null = null
let compiler: typeof import('vue/compiler-sfc') | null

function requireCompiler() {
if (!compiler) {
try {
// Vue 3.2.13+ ships the SFC compiler directly under the `vue` package
compiler = require('vue/compiler-sfc')
} catch (e) {
if (compileRoot) {
try {
compiler = require('@vue/compiler-sfc')
compiler = require(require.resolve('vue/compiler-sfc', { paths: [compileRoot] }))
} catch (e) {}
}

if (!compiler) {
try {
compiler = require('vue/compiler-sfc')
} catch (e) {
throw new Error('@vue/compiler-sfc is not present in the dependency tree.\n')
try {
compiler = require('@vue/compiler-sfc')
} catch (e) {
throw new Error('@vue/compiler-sfc is not present in the dependency tree.\n')
}
}
}
}

return compiler
return compiler!
}

export function setCompileRoot(root: string) {
if (root && root !== compileRoot) {
compileRoot = root
compiler = null
}
}

export function compileVueCode(code: string) {
Expand Down
3 changes: 2 additions & 1 deletion src/plugin.ts
Expand Up @@ -13,7 +13,7 @@ import {
transformAliasImport,
removePureImport
} from './transform'
import { compileVueCode } from './compile'
import { setCompileRoot, compileVueCode } from './compile'
import {
isNativeObj,
isPromise,
Expand Down Expand Up @@ -150,6 +150,7 @@ export function dtsPlugin(options: PluginOptions = {}): Plugin {
return
}

setCompileRoot(root)
compilerOptions.rootDir ||= root

project = new Project({
Expand Down

0 comments on commit f59d8fc

Please sign in to comment.