Skip to content

Commit 8822ae8

Browse files
authored
Inject hidden imports to fix .gts/.gjs extensions (#861)
* Inject hidden imports to fix `.gts`/`.gjs` extensions * re-gen snapshots * prettier * add comments * more comments * resolve some vscode packing issues * prettier
1 parent 0128be0 commit 8822ae8

File tree

13 files changed

+222
-107
lines changed

13 files changed

+222
-107
lines changed

package.json

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33
"version": "1.4.0",
44
"private": true,
55
"repository": "https://github.com/typed-ember/glint",
6-
"workspaces": {
7-
"packages": [
8-
"packages/*",
9-
"test-packages/*"
10-
],
11-
"nohoist:comment": "When running extension host in test-packages/ts-plugin-test-app, we need 1. to be able to use workspace TypeScript, and 2. to use a TS Plugin specified as an npm dependency, both which require typescript and the plugin to be present within the same folder's `node_modules` directory.",
12-
"nohoist": [
13-
"ts-plugin-test-app/typescript",
14-
"ts-plugin-test-app/@glint/tsserver-plugin"
15-
]
16-
},
176
"scripts": {
187
"build": "tsc --build",
198
"build:watch": "tsc --build --watch",

packages/core/src/transform/template/rewrite-module.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,28 @@ function calculateCorrelatedSpans(
6767
): CorrelatedSpansResult {
6868
let directives: Array<Directive> = [];
6969
let errors: Array<TransformError> = [];
70-
let partialSpans: Array<PartialCorrelatedSpan> = [];
70+
let partialSpans: Array<PartialCorrelatedSpan> = [
71+
// HACK: We prefix every transformed TS file with these non-existent imports
72+
// because it causes TypeScript to consider `.gts` and `.gjs` as possible
73+
// implied extensions when extensions are omitted from import module specifiers,
74+
// i.e. it causes `import FooComponent from './foo';` to work given a `foo.gts` file.
75+
//
76+
// Origin of this hack:
77+
// https://github.com/typed-ember/glint/issues/806#issuecomment-2758616327
78+
//
79+
// Note that these imports WILL generate diagnostic errors, but because they're
80+
// mapped to zero-length source code spans, they're essentially mapped to nothing,
81+
// and when Volar reverse-transforms the diagnostics back to the original source
82+
// code, they'll be discarded.
83+
{
84+
originalFile: script,
85+
originalStart: 0,
86+
originalLength: 0,
87+
insertionPoint: 0,
88+
transformedSource:
89+
"import __GLINT_GTS_EXTENSION_HACK__ from './__glint-non-existent.gts';\n import __GLINT_GJS_EXTENSION_HACK__ from './__glint-non-existent.gjs';\n",
90+
},
91+
];
7192

7293
let { ast, emitMetadata, error } = parseScript(ts, script, environment);
7394

packages/vscode/.vscodeignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tsconfig.json
1717
tsconfig.tsbuildinfo
1818

1919
node_modules/**/
20-
!node_modules/glint-tsserver-plugin-pack.js
21-
!node_modules/glint-core-pack.js
20+
!node_modules/glint-tsserver-plugin-pack/index.js
21+
!node_modules/glint-core-pack/index.js
2222

2323
scripts/

packages/vscode/__fixtures__/ember-app-loose-and-gts/app/components/Other.gts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Component from '@glimmer/component';
2-
import Greeting from './Greeting.gts';
2+
import Greeting from './Greeting';
3+
import Colocated from './colocated-folder';
4+
35
export interface OtherSignature {
46
Args: { target: string };
57
}
@@ -9,5 +11,6 @@ export default class Other extends Component<OtherSignature> {
911

1012
<template>
1113
<Greeting @target="World" />
14+
<Colocated @target="World" />
1215
</template>
1316
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Component from '@glimmer/component';
2+
3+
export interface ColocatedIndexSignature {
4+
Args: { target: string };
5+
}
6+
7+
export default class ColocatedIndex extends Component<ColocatedIndexSignature> {
8+
private message = 'Hello';
9+
10+
<template>
11+
{{this.message}}, {{@target}}!
12+
</template>
13+
}

packages/vscode/__fixtures__/ember-app-loose-and-gts/app/components/pod-layout/component.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/vscode/__fixtures__/ember-app-loose-and-gts/app/components/pod-layout/template.hbs

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/vscode/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "glint-vscode",
33
"displayName": "Glint",
44
"description": "Glint language server integration for VS Code",
5-
"version": "1.4.3",
5+
"version": "1.4.5",
66
"publisher": "typed-ember",
77
"private": true,
88
"author": "James C. Davis (https://github.com/jamescdavis)",
@@ -257,6 +257,7 @@
257257
"glob": "^10.2.4",
258258
"mocha": "^10.2.0",
259259
"reactive-vscode": "0.2.7-beta.1",
260+
"semver": "^7.5.2",
260261
"typescript": "^5.8.2",
261262
"vscode-ext-gen": "^0.5.0"
262263
},

packages/vscode/scripts/build.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,24 @@ const ctx = await context({
1414
bundle: true,
1515
entryPoints: {
1616
'dist/extension': require.resolve('../lib/src/extension.js'),
17-
'node_modules/glint-tsserver-plugin-pack': '../tsserver-plugin/lib/typescript-server-plugin.js',
18-
'node_modules/glint-core-pack': '../core/lib/index.js',
17+
'node_modules/glint-tsserver-plugin-pack/index':
18+
'../tsserver-plugin/lib/typescript-server-plugin.js',
19+
'node_modules/glint-core-pack/index': '../core/lib/index.js',
1920
},
2021
external: ['vscode'],
2122
logLevel: 'info',
2223
minify: !debug,
2324
outdir: fileURLToPath(new URL('../', import.meta.url)),
2425
platform: 'node',
25-
sourcemap: debug,
26+
sourcemap: false,
2627
target: 'node16',
2728

2829
// Since we're generating CJS, we need to replace any ESM import.meta.url with `__filename`
2930
// https://github.com/evanw/esbuild/issues/1492#issuecomment-893144483
3031
inject: [require.resolve('./import-meta-url.js')],
3132
define: {
3233
'import.meta.url': 'import_meta_url',
33-
GLINT_CORE_PATH: '"glint-core-pack.js"',
34+
GLINT_CORE_PATH: '"glint-core-pack/index.js"',
3435
},
3536

3637
plugins: [

packages/vscode/src/generated-meta.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Meta info
55
export const publisher = "typed-ember"
66
export const name = "glint-vscode"
7-
export const version = "1.4.3"
7+
export const version = "1.4.5"
88
export const displayName = "Glint"
99
export const description = "Glint language server integration for VS Code"
1010
export const extensionId = `${publisher}.${name}`

0 commit comments

Comments
 (0)