@vue-macros/api
Advanced tools
Comparing version 0.12.0 to 0.13.0
@@ -5,3 +5,2 @@ import { MagicStringAST, SFC } from '@vue-macros/common'; | ||
import { TypeScript, Declaration, Statement, TSModuleBlock, TSType, TSParenthesizedType, TSTypeAliasDeclaration, Node, Identifier, TSCallSignatureDeclaration, TSFunctionType, TSConstructSignatureDeclaration, TSMethodSignature, TSPropertySignature, TSMappedType, TSInterfaceDeclaration, TSInterfaceBody, TSTypeLiteral, TSIntersectionType, TemplateLiteral, StringLiteral, TSLiteralType, NumericLiteral, BooleanLiteral, BigIntLiteral, TSTypeElement, TSIndexedAccessType, TSUnionType, TSTypeOperator, CallExpression, LVal, VariableDeclaration, ExpressionStatement, TSTypeReference, ObjectMethod, ObjectProperty, Expression } from '@babel/types'; | ||
import { PluginContext } from 'rollup'; | ||
import { Plugin } from 'vite'; | ||
@@ -85,9 +84,4 @@ | ||
type ResolveTSFileIdImpl = (id: string, importer: string) => Promise<string | undefined> | string | undefined; | ||
declare const resolveTSFileId: ResolveTSFileIdImpl; | ||
/** | ||
* @limitation don't node_modules and JavaScript file | ||
*/ | ||
declare const resolveTSFileIdNode: ResolveTSFileIdImpl; | ||
declare function setResolveTSFileIdImpl(impl: ResolveTSFileIdImpl): void; | ||
declare function resolveDts(id: string, importer: string): Promise<string | undefined>; | ||
declare const resolveDtsHMR: NonNullable<Plugin["handleHotUpdate"]>; | ||
@@ -281,7 +275,2 @@ declare function resolveTSTemplateLiteral({ type, scope }: TSResolvedType<TemplateLiteral>): Promise<StringLiteral[]>; | ||
declare const OxcResolve: () => { | ||
resolve: (ctx: PluginContext) => ResolveTSFileIdImpl; | ||
handleHotUpdate: NonNullable<Plugin["handleHotUpdate"]>; | ||
}; | ||
export { type ASTDefinition, type AnalyzeResult, type DefaultsASTRaw, type DefineEmitsStatement, type DefinePropsStatement, DefinitionKind, type Emits, type EmitsBase, OxcResolve, type Props, type PropsBase, type ResolveTSFileIdImpl, type RuntimePropDefinition, type TSDeclaration, type TSEmits, type TSFile, type TSModule, type TSNamespace, type TSProperties, type TSProps, type TSPropsMethod, type TSPropsProperty, type TSResolvedType, type TSScope, type TSScopeBase, UNKNOWN_TYPE, analyzeSFC, attachNodeLoc, checkForTSProperties, genRuntimePropDefinition, getTSFile, getTSPropertiesKeys, handleTSEmitsDefinition, handleTSPropsDefinition, inferRuntimeType, isSupportedForTSReferencedType, isTSDeclaration, isTSNamespace, mergeTSProperties, namespaceSymbol, resolveMaybeTSUnion, resolveTSFileId, resolveTSFileIdNode, resolveTSIndexedAccessType, resolveTSLiteralType, resolveTSNamespace, resolveTSProperties, resolveTSReferencedType, resolveTSScope, resolveTSTemplateLiteral, resolveTSTypeOperator, resolveTypeElements, setResolveTSFileIdImpl, tsFileCache }; | ||
export { type ASTDefinition, type AnalyzeResult, type DefaultsASTRaw, type DefineEmitsStatement, type DefinePropsStatement, DefinitionKind, type Emits, type EmitsBase, type Props, type PropsBase, type RuntimePropDefinition, type TSDeclaration, type TSEmits, type TSFile, type TSModule, type TSNamespace, type TSProperties, type TSProps, type TSPropsMethod, type TSPropsProperty, type TSResolvedType, type TSScope, type TSScopeBase, UNKNOWN_TYPE, analyzeSFC, attachNodeLoc, checkForTSProperties, genRuntimePropDefinition, getTSFile, getTSPropertiesKeys, handleTSEmitsDefinition, handleTSPropsDefinition, inferRuntimeType, isSupportedForTSReferencedType, isTSDeclaration, isTSNamespace, mergeTSProperties, namespaceSymbol, resolveDts, resolveDtsHMR, resolveMaybeTSUnion, resolveTSIndexedAccessType, resolveTSLiteralType, resolveTSNamespace, resolveTSProperties, resolveTSReferencedType, resolveTSScope, resolveTSTemplateLiteral, resolveTSTypeOperator, resolveTypeElements, tsFileCache }; |
@@ -28,23 +28,98 @@ // src/index.ts | ||
// src/ts/resolve-file.ts | ||
import { lstatSync } from "node:fs"; | ||
import path from "node:path"; | ||
var resolveTSFileId = (id, importer) => { | ||
return resolveTSFileIdImpl(id, importer); | ||
}; | ||
var resolveTSFileIdNode = (id, importer) => { | ||
return tryResolve(id, importer) || tryResolve(`${id}.ts`, importer) || tryResolve(`${id}.d.ts`, importer) || tryResolve(`${id}/index`, importer) || tryResolve(`${id}/index.ts`, importer) || tryResolve(`${id}/index.d.ts`, importer); | ||
}; | ||
function tryResolve(id, importer) { | ||
const filePath = path.resolve(importer, "..", id); | ||
try { | ||
const stat = lstatSync(filePath); | ||
if (stat.isFile()) return filePath; | ||
} catch { | ||
return; | ||
// src/ts/scope.ts | ||
import { readFile } from "node:fs/promises"; | ||
import { | ||
babelParse, | ||
getFileCodeAndLang, | ||
REGEX_SUPPORTED_EXT | ||
} from "@vue-macros/common"; | ||
var tsFileCache = /* @__PURE__ */ Object.create(null); | ||
async function getTSFile(filePath) { | ||
if (tsFileCache[filePath]) return tsFileCache[filePath]; | ||
const content = await readFile(filePath, "utf-8"); | ||
const { code, lang } = getFileCodeAndLang(content, filePath); | ||
return tsFileCache[filePath] = { | ||
kind: "file", | ||
filePath, | ||
content, | ||
ast: REGEX_SUPPORTED_EXT.test(filePath) ? babelParse(code, lang, { cache: true }).body : void 0 | ||
}; | ||
} | ||
function resolveTSScope(scope) { | ||
const isFile = scope.kind === "file"; | ||
let parentScope; | ||
if (!isFile) parentScope = resolveTSScope(scope.scope); | ||
const file = isFile ? scope : parentScope.file; | ||
const body = isFile ? scope.ast : scope.ast.body; | ||
const exports = scope.exports; | ||
const declarations = isFile ? scope.declarations : { ...resolveTSScope(scope.scope).declarations, ...scope.declarations }; | ||
return { | ||
isFile, | ||
file, | ||
body, | ||
declarations, | ||
exports | ||
}; | ||
} | ||
// src/ts/resolve-file.ts | ||
var typesResolver; | ||
var referencedFiles = /* @__PURE__ */ new Map(); | ||
function collectReferencedFile(importer, file) { | ||
if (!importer) return; | ||
if (!referencedFiles.has(file)) { | ||
referencedFiles.set(file, /* @__PURE__ */ new Set([importer])); | ||
} else { | ||
referencedFiles.get(file).add(importer); | ||
} | ||
} | ||
var resolveTSFileIdImpl = resolveTSFileIdNode; | ||
function setResolveTSFileIdImpl(impl) { | ||
resolveTSFileIdImpl = impl; | ||
var resolveCache = /* @__PURE__ */ new Map(); | ||
async function resolveDts(id, importer) { | ||
const cached = resolveCache.get(importer)?.get(id); | ||
if (cached) return cached; | ||
if (!typesResolver) { | ||
const { ResolverFactory } = await import("oxc-resolver"); | ||
typesResolver = new ResolverFactory({ | ||
mainFields: ["types"], | ||
conditionNames: ["types", "import"], | ||
extensions: [".d.ts", ".ts"] | ||
}); | ||
} | ||
const { error, path: resolved } = await typesResolver.async( | ||
path.dirname(importer), | ||
id | ||
); | ||
if (error || !resolved) return; | ||
collectReferencedFile(importer, resolved); | ||
if (resolveCache.has(importer)) { | ||
resolveCache.get(importer).set(id, resolved); | ||
} else { | ||
resolveCache.set(importer, /* @__PURE__ */ new Map([[id, resolved]])); | ||
} | ||
return resolved; | ||
} | ||
var resolveDtsHMR = ({ | ||
file, | ||
server, | ||
modules | ||
}) => { | ||
const cache = /* @__PURE__ */ new Map(); | ||
if (tsFileCache[file]) delete tsFileCache[file]; | ||
const affected = getAffectedModules(file); | ||
return [...modules, ...affected]; | ||
function getAffectedModules(file2) { | ||
if (cache.has(file2)) return cache.get(file2); | ||
if (!referencedFiles.has(file2)) return /* @__PURE__ */ new Set([]); | ||
const modules2 = /* @__PURE__ */ new Set([]); | ||
cache.set(file2, modules2); | ||
for (const importer of referencedFiles.get(file2)) { | ||
const mods = server.moduleGraph.getModulesByFile(importer); | ||
if (mods) mods.forEach((m) => modules2.add(m)); | ||
getAffectedModules(importer).forEach((m) => modules2.add(m)); | ||
} | ||
return modules2; | ||
} | ||
}; | ||
@@ -396,38 +471,2 @@ // src/ts/resolve-reference.ts | ||
// src/ts/scope.ts | ||
import { readFile } from "node:fs/promises"; | ||
import { | ||
babelParse, | ||
getFileCodeAndLang, | ||
REGEX_SUPPORTED_EXT | ||
} from "@vue-macros/common"; | ||
var tsFileCache = /* @__PURE__ */ Object.create(null); | ||
async function getTSFile(filePath) { | ||
if (tsFileCache[filePath]) return tsFileCache[filePath]; | ||
const content = await readFile(filePath, "utf-8"); | ||
const { code, lang } = getFileCodeAndLang(content, filePath); | ||
return tsFileCache[filePath] = { | ||
kind: "file", | ||
filePath, | ||
content, | ||
ast: REGEX_SUPPORTED_EXT.test(filePath) ? babelParse(code, lang, { cache: true }).body : void 0 | ||
}; | ||
} | ||
function resolveTSScope(scope) { | ||
const isFile = scope.kind === "file"; | ||
let parentScope; | ||
if (!isFile) parentScope = resolveTSScope(scope.scope); | ||
const file = isFile ? scope : parentScope.file; | ||
const body = isFile ? scope.ast : scope.ast.body; | ||
const exports = scope.exports; | ||
const declarations = isFile ? scope.declarations : { ...resolveTSScope(scope.scope).declarations, ...scope.declarations }; | ||
return { | ||
isFile, | ||
file, | ||
body, | ||
declarations, | ||
exports | ||
}; | ||
} | ||
// src/ts/resolve-reference.ts | ||
@@ -506,3 +545,3 @@ function isSupportedForTSReferencedType(node) { | ||
} else if (stmt.type === "ExportAllDeclaration") { | ||
const resolved = await resolveTSFileId(stmt.source.value, file.filePath); | ||
const resolved = await resolveDts(stmt.source.value, file.filePath); | ||
if (!resolved) continue; | ||
@@ -515,3 +554,3 @@ const sourceScope = await getTSFile(resolved); | ||
if (stmt.source) { | ||
const resolved = await resolveTSFileId(stmt.source.value, file.filePath); | ||
const resolved = await resolveDts(stmt.source.value, file.filePath); | ||
if (!resolved) continue; | ||
@@ -555,3 +594,3 @@ const scope2 = await getTSFile(resolved); | ||
} else if (stmt.type === "ImportDeclaration") { | ||
const resolved = await resolveTSFileId(stmt.source.value, file.filePath); | ||
const resolved = await resolveDts(stmt.source.value, file.filePath); | ||
if (!resolved) continue; | ||
@@ -1374,70 +1413,4 @@ const importScope = await getTSFile(resolved); | ||
} | ||
// src/resolve.ts | ||
import path2 from "node:path"; | ||
import { ResolverFactory } from "oxc-resolver"; | ||
var typesResolver = new ResolverFactory({ | ||
mainFields: ["types"], | ||
conditionNames: ["types", "import"], | ||
extensions: [".d.ts", ".ts"] | ||
}); | ||
var OxcResolve = () => { | ||
const referencedFiles = /* @__PURE__ */ new Map(); | ||
function collectReferencedFile(importer, file) { | ||
if (!importer) return; | ||
if (!referencedFiles.has(file)) { | ||
referencedFiles.set(file, /* @__PURE__ */ new Set([importer])); | ||
} else { | ||
referencedFiles.get(file).add(importer); | ||
} | ||
} | ||
const resolveCache = /* @__PURE__ */ new Map(); | ||
function withResolveCache(id, importer, result) { | ||
if (!resolveCache.has(importer)) { | ||
resolveCache.set(importer, /* @__PURE__ */ new Map([[id, result]])); | ||
return result; | ||
} | ||
resolveCache.get(importer).set(id, result); | ||
return result; | ||
} | ||
const resolve = () => { | ||
return async (id, importer) => { | ||
const cached = resolveCache.get(importer)?.get(id); | ||
if (cached) return cached; | ||
const resolved = await typesResolver.async(path2.dirname(importer), id); | ||
if (resolved.error || !resolved.path) return; | ||
collectReferencedFile(importer, resolved.path); | ||
return withResolveCache(id, importer, resolved.path); | ||
}; | ||
}; | ||
const handleHotUpdate = ({ | ||
file, | ||
server, | ||
modules | ||
}) => { | ||
const cache = /* @__PURE__ */ new Map(); | ||
function getAffectedModules(file2) { | ||
if (cache.has(file2)) return cache.get(file2); | ||
if (!referencedFiles.has(file2)) return /* @__PURE__ */ new Set([]); | ||
const modules2 = /* @__PURE__ */ new Set([]); | ||
cache.set(file2, modules2); | ||
for (const importer of referencedFiles.get(file2)) { | ||
const mods = server.moduleGraph.getModulesByFile(importer); | ||
if (mods) mods.forEach((m) => modules2.add(m)); | ||
getAffectedModules(importer).forEach((m) => modules2.add(m)); | ||
} | ||
return modules2; | ||
} | ||
if (tsFileCache[file]) delete tsFileCache[file]; | ||
const affected = getAffectedModules(file); | ||
return [...modules, ...affected]; | ||
}; | ||
return { | ||
resolve, | ||
handleHotUpdate | ||
}; | ||
}; | ||
export { | ||
DefinitionKind, | ||
OxcResolve, | ||
UNKNOWN_TYPE, | ||
@@ -1459,5 +1432,5 @@ analyzeSFC, | ||
parseSFC, | ||
resolveDts, | ||
resolveDtsHMR, | ||
resolveMaybeTSUnion, | ||
resolveTSFileId, | ||
resolveTSFileIdNode, | ||
resolveTSIndexedAccessType, | ||
@@ -1472,4 +1445,3 @@ resolveTSLiteralType, | ||
resolveTypeElements, | ||
setResolveTSFileIdImpl, | ||
tsFileCache | ||
}; |
{ | ||
"name": "@vue-macros/api", | ||
"version": "0.12.0", | ||
"version": "0.13.0", | ||
"description": "General API for Vue Macros.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
118279
3148