Skip to content

Commit

Permalink
Support dependencies param
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk authored and sxzz committed Jun 30, 2024
1 parent 6f06ccf commit 3cfbb7a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"@types/node": "^20.14.2",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@vitejs/plugin-vue": "^5.0.5",
"@volar/monaco": "~2.4.0-alpha.3",
"@volar/monaco": "~2.4.0-alpha.5",
"@vue/babel-plugin-jsx": "^1.2.2",
"@vue/language-service": "~2.0.24",
"assert-plus": "^1.0.0",
Expand Down
48 changes: 24 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 20 additions & 13 deletions src/monaco/vue.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as worker from 'monaco-editor-core/esm/vs/editor/editor.worker'
import type * as monaco from 'monaco-editor-core'
import {
type LanguageServiceEnvironment,
activateAutomaticTypeAcquisition,
createJsDelivrNpmFileSystem,
createTypeScriptWorkerService,
} from '@volar/monaco/worker'
import {
Expand Down Expand Up @@ -37,17 +37,31 @@ self.onmessage = async (msg: MessageEvent<WorkerMessage>) => {
worker.initialize(
(
ctx: monaco.worker.IWorkerContext<WorkerHost>,
{
tsconfig,
// TODO
dependencies,
}: CreateData,
{ tsconfig, dependencies }: CreateData,
) => {
const asFileName = (uri: URI) => uri.path
const asUri = (fileName: string): URI => URI.file(fileName)
const env: LanguageServiceEnvironment = {
workspaceFolders: [URI.file('/')],
locale,
fs: createJsDelivrNpmFileSystem(
(uri) => {
if (uri.scheme === 'file') {
if (uri.path === '/node_modules') {
return ''
} else if (uri.path.startsWith('/node_modules/')) {
return uri.path.slice('/node_modules/'.length)
}
}
},
(pkgName) => dependencies[pkgName],
(path, content) => {
ctx.host.onFetchCdnFile(
asUri('/node_modules/' + path).toString(),
content,
)
},
),
}

const { options: compilerOptions } = ts.convertCompilerOptionsFromJson(
Expand All @@ -58,13 +72,6 @@ self.onmessage = async (msg: MessageEvent<WorkerMessage>) => {
tsconfig.vueCompilerOptions || {},
)

activateAutomaticTypeAcquisition(env, { asFileName }, (path, content) => {
ctx.host.onFetchCdnFile(
asUri('/node_modules/' + path).toString(),
content,
)
})

return createTypeScriptWorkerService({
typescript: ts,
compilerOptions,
Expand Down

0 comments on commit 3cfbb7a

Please sign in to comment.