diff --git a/.vscode/launch.json b/.vscode/launch.json index 5a42d86e8..83186a621 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -55,6 +55,28 @@ "lifeart.vscode-glimmer-syntax", "${workspaceFolder}/test-packages" ] + }, + { + "name": "Debug Extension (TS Plugin Only, no Glint)", + "type": "extensionHost", + "request": "launch", + "preLaunchTask": "npm: build", + "autoAttachChildProcesses": true, + "runtimeExecutable": "${execPath}", + "outFiles": [ + "${workspaceFolder}/**/*.js", + "!**/node_modules/**" + ], + "args": [ + "--extensionDevelopmentPath=${workspaceFolder}/packages/vscode", + // "--disable-extension", + // "vscode.typescript-language-features", + "--disable-extension", + "lifeart.vscode-glimmer-syntax", + "--disable-extension", + "typed-ember.glint-vscode", + "${workspaceFolder}/test-packages" + ] } ] } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 3f339a653..1707e3bf1 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,9 +1,10 @@ -import { GlintConfig, loadConfig } from './config/index.js'; +import { GlintConfig, loadConfig, findConfig } from './config/index.js'; import * as utils from './language-server/util/index.js'; +import { createEmberLanguagePlugin } from './volar/ember-language-plugin.js'; /** @internal */ export const pathUtils = utils; -export { loadConfig }; +export { loadConfig, findConfig, createEmberLanguagePlugin }; export type { GlintConfig }; diff --git a/packages/typescript-plugin/package.json b/packages/typescript-plugin/package.json index 1eb976115..5753e290e 100644 --- a/packages/typescript-plugin/package.json +++ b/packages/typescript-plugin/package.json @@ -1,10 +1,11 @@ { "name": "@glint/typescript-plugin", "version": "1.4.0", - "type": "module", + "type": "commonjs", "repository": "typed-ember/glint", "description": "TypeScript Server Plugin for Glint", "license": "MIT", + "main": "lib/typescript-server-plugin.js", "authors": [ "Alex Matchneer (https://github.com/machty)" ], diff --git a/packages/typescript-plugin/src/typescript-server-plugin.ts b/packages/typescript-plugin/src/typescript-server-plugin.ts index 9e6ac8476..84cd9c772 100644 --- a/packages/typescript-plugin/src/typescript-server-plugin.ts +++ b/packages/typescript-plugin/src/typescript-server-plugin.ts @@ -1,26 +1,34 @@ -import { createLanguageServicePlugin } from '@volar/typescript/lib/quickstart/createLanguageServicePlugin.js'; -import { findConfig } from '../../core/src/config/index.js'; -import { createEmberLanguagePlugin } from '../../core/src/volar/ember-language-plugin.js'; +import type ts from 'typescript'; -const plugin = createLanguageServicePlugin((ts, info) => { - const cwd = info.languageServiceHost.getCurrentDirectory(); - const glintConfig = findConfig(cwd); +// Top level "imports" need to be CJS requires because TS Plugins must be CJS; +// we dynamically import() the ESM modules we need below within the async fn. +const { + createAsyncLanguageServicePlugin, +} = require('@volar/typescript/lib/quickstart/createAsyncLanguageServicePlugin.js'); - // NOTE: this code used to assert in the failure of finding Glint config; I'm - // not sure whether it's better to be lenient, but we were getting test failures - // on environment-ember-loose's `yarn run test`. - if (glintConfig) { - const gtsLanguagePlugin = createEmberLanguagePlugin(glintConfig); - return { - languagePlugins: [gtsLanguagePlugin], - }; - } else { - return { - languagePlugins: [], - }; - } -}); +const plugin = createAsyncLanguageServicePlugin( + ['.ts', '.js', '.gts', '.gjs', '.hbs'], + 7 satisfies ts.ScriptKind.Deferred, + async (_ts: any, info: any) => { + const { findConfig, createEmberLanguagePlugin } = await import('@glint/core'); + + const cwd = info.languageServiceHost.getCurrentDirectory(); + const glintConfig = findConfig(cwd); + + // NOTE: this code used to assert in the failure of finding Glint config; I'm + // not sure whether it's better to be lenient, but we were getting test failures + // on environment-ember-loose's `yarn run test`. + if (glintConfig) { + const gtsLanguagePlugin = createEmberLanguagePlugin(glintConfig); + return { + languagePlugins: [gtsLanguagePlugin], + }; + } else { + return { + languagePlugins: [], + }; + } + }, +); -// @ts-expect-error TypeScript Plugin needs to be exported with `export =` -// eslint-disable-next-line no-restricted-syntax export = plugin; diff --git a/packages/typescript-plugin/tsconfig.json b/packages/typescript-plugin/tsconfig.json index 329193ea7..639801484 100644 --- a/packages/typescript-plugin/tsconfig.json +++ b/packages/typescript-plugin/tsconfig.json @@ -2,7 +2,11 @@ "extends": "../../tsconfig.compileroptions.json", "compilerOptions": { "rootDir": "src", - "outDir": "lib" + "outDir": "lib", + + // ts server plugins need to be cjs + "module": "CommonJS", + "moduleResolution": "node" }, "include": ["src"], "references": [{ "path": "../core" }] diff --git a/packages/vscode/package.json b/packages/vscode/package.json index b71827e61..72ef010bd 100644 --- a/packages/vscode/package.json +++ b/packages/vscode/package.json @@ -162,10 +162,6 @@ { "name": "typescript-hbs-plugin", "enableForWorkspaceTypeScriptVersions": true - }, - { - "name": "@glint/typescript-plugin", - "enableForWorkspaceTypeScriptVersions": true } ], "jsonValidation": [ diff --git a/test-packages/ts-template-imports-app/package.json b/test-packages/ts-template-imports-app/package.json index 27ef44865..a64aac7f7 100644 --- a/test-packages/ts-template-imports-app/package.json +++ b/test-packages/ts-template-imports-app/package.json @@ -8,6 +8,9 @@ "test:typecheck": "glint", "test:tsc": "echo 'no standalone tsc within this project'" }, + "devDependencies": { + "@glint/typescript-plugin": "*" + }, "volta": { "extends": "../../package.json" } diff --git a/test-packages/ts-template-imports-app/tsconfig.json b/test-packages/ts-template-imports-app/tsconfig.json index cbbfc8311..9c4caf689 100644 --- a/test-packages/ts-template-imports-app/tsconfig.json +++ b/test-packages/ts-template-imports-app/tsconfig.json @@ -1,7 +1,8 @@ { "extends": "../../tsconfig.compileroptions.json", "compilerOptions": { - "baseUrl": "." + "baseUrl": ".", + "plugins": [{ "name": "@glint/typescript-plugin" }] }, "include": ["src", "types"], "glint": {