This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
generated from productdevbookcom/vue-ts-bundle-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// @credit: @antfu https://github.com/antfu/nuxt-content-twoslash | ||
|
||
import { existsSync } from 'node:fs' | ||
import fs from 'node:fs/promises' | ||
import fg from 'fast-glob' | ||
import ts from 'typescript' | ||
import { dirname, join, relative, resolve } from 'pathe' | ||
|
||
export const prepend = [ | ||
'/// <reference types="./.nuxt/nuxt.d.ts" />', | ||
'', | ||
].join('\n') | ||
|
||
async function getTypeDecorations(dir: string, filesMap: Record<string, string> = {}) { | ||
const files = await fg('**/*.d.ts', { | ||
cwd: dir, | ||
onlyFiles: true, | ||
}) | ||
await Promise.all( | ||
files.map(async (file) => { | ||
filesMap[`.nuxt/${file}`] = await fs.readFile(join(dir, file), 'utf-8') | ||
}), | ||
) | ||
return filesMap | ||
} | ||
|
||
function removeJSONComments(content: string) { | ||
return content.replace(/\/\/.*/g, '') | ||
} | ||
|
||
async function getNuxtCompilerOptions(dir: string) { | ||
const path = join(dir, 'tsconfig.json') | ||
if (existsSync(path)) { | ||
try { | ||
const tsconfig = await fs.readFile(path, 'utf-8') | ||
const config = JSON.parse(removeJSONComments(tsconfig)) || {} | ||
const json = ts.convertCompilerOptionsFromJson(config.compilerOptions, dir, '').options | ||
Object.entries(json.paths || {}).forEach(([key, value]) => { | ||
json.paths![key] = value.map((v: string) => `./${relative(dirname(dir), resolve(dir, v))}`) | ||
if (key === '#imports') | ||
json.paths![key] = ['./.nuxt/imports.d.ts'] | ||
}) | ||
return json | ||
} | ||
catch (e) { | ||
console.error('[nuxt-content-twoslash] Failed to parse .nuxt/tsconfig.json', e) | ||
return {} | ||
} | ||
} | ||
return {} | ||
} | ||
|
||
export const typeDecorations = await getTypeDecorations(join(__dirname, '..', '.nuxt')) | ||
|
||
export const nuxtCompilerOptions = await getNuxtCompilerOptions(join(__dirname, '..', '.nuxt')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters