Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Commit

Permalink
fix: use locally installed @vue/compiler-sfc
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Mar 18, 2021
1 parent 90191bb commit 7dcf115
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
25 changes: 22 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from 'path'
import fs from 'fs'
import { parse, compileScript } from '@vue/compiler-sfc'
import { Project, SourceFile } from 'ts-morph'
import glob from 'fast-glob'

Expand All @@ -9,7 +8,25 @@ export type Options = {
outDir?: string
}

let vueCompiler: typeof import('@vue/compiler-sfc')

const getVueCompiler = () => {
if (!vueCompiler) {
try {
vueCompiler = require(path.resolve('node_modules/@vue/compiler-sfc'))
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
throw new Error(`@vue/compiler-sfc is not founded in ./node_modules`)
}
throw error
}
}

return vueCompiler
}

export async function build({ input, outDir }: Options) {
const vueCompiler = getVueCompiler()
const tsConfigFilePath = fs.existsSync('tsconfig.json')
? 'tsconfig.json'
: undefined
Expand All @@ -30,7 +47,7 @@ export async function build({ input, outDir }: Options) {
await Promise.all(
files.map(async (file) => {
const content = await fs.promises.readFile(file, 'utf8')
const sfc = parse(content)
const sfc = vueCompiler.parse(content)
const { script, scriptSetup } = sfc.descriptor
if (script || scriptSetup) {
let content = ''
Expand All @@ -40,7 +57,9 @@ export async function build({ input, outDir }: Options) {
if (script.lang === 'ts') isTS = true
}
if (scriptSetup) {
const compiled = compileScript(sfc.descriptor, { id: 'xxx' })
const compiled = vueCompiler.compileScript(sfc.descriptor, {
id: 'xxx',
})
content += compiled.content
if (scriptSetup.lang === 'ts') isTS = true
}
Expand Down
20 changes: 0 additions & 20 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,6 @@ const options: Options = {
})
},
},
{
// Some modules like `pug` are required in consolidate but should not be bundled
name: 'ignore-require-in-consolidate',
setup(build) {
build.onResolve({ filter: /^[a-z]/ }, (args) => {
if (builtinModules.includes(args.path))
return {
path: args.path,
external: true,
}
if (args.importer.includes('consolidate')) {
const id = resolve.silent(args.resolveDir, args.path)
if (id) {
return { path: id }
}
return { path: args.path, external: true }
}
})
},
},
],
}

Expand Down

0 comments on commit 7dcf115

Please sign in to comment.