Replies: 1 comment 2 replies
-
Here's my current hack solution for this case: let actualPath =
resolveExports(packageJson.exports ?? null, {
types: true
}) ?? packageJson.types
if (!actualPath) {
return
}
actualPath = actualPath.replace(/^\.?\/?/, '').replace(/\.d\.ts$/, '')
const typePath = `${pathPrefix}/index.d.ts`
const typeContent = `export * from './${actualPath}'`
fileMap.set(typePath, typeContent)
const uri = monaco.Uri.file(typePath)
let model = monaco.editor.getModel(uri)
if (!model) {
model = monaco.editor.createModel(typeContent, 'typescript', uri)
} resolveExports.tstype ExportObject = {
[k: string]: PackageJsonExports
}
export type PackageJsonExports = null | string | Array<string | ExportObject> | ExportObject
const isConditionalExports = (v: ExportObject) => {
return !Object.keys(v)[0]?.startsWith('.')
}
export const resolveExports = (
exp: PackageJsonExports,
opt?: {
path?: string
order?: string[]
types?: boolean
}
): string | null => {
const path = opt?.path ?? '.'
const order = opt?.order ?? ['import', 'browser', 'default', 'require']
const types = !!opt?.types
if (exp === null || typeof exp === 'string') {
return exp
}
if (Array.isArray(exp)) {
return resolveExports(exp[0], opt)
}
if (isConditionalExports(exp)) {
// conditional exports
if (types && exp['types']) {
return resolveExports(exp['types'], opt)
}
const matched = order.find(v => !!exp[v])
return matched ? resolveExports(exp[matched], opt) : null
} else {
// subpath exports
const subpaths = Object.keys(exp)
for (const subpath of subpaths) {
if (subpath === path) {
return resolveExports(exp[subpath], opt)
}
}
return null
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
According to #2295, I can install packages that has a
index.d.ts
file in package root, but for package likesolid-js
that specified a type file path other thanindex.d.ts
, it still complainsCannot find module 'solid-js' or its corresponding type declarations.
forimport * as solid from 'solid-js'
, even I created model forfile:///node_modules/solid-js/package.json
andfile:///node_modules/solid-js/types/index.d.ts
.By the way, I'm adding all of package dependency file to monaco model and if there's more than 600 files, it complains
npm.ts:106 [b39] potential listener LEAK detected, having 600 listeners already. MOST frequent listener (364)
. Is there a better way to load these type files without resolving their dependencies manually?Beta Was this translation helpful? Give feedback.
All reactions