@@ -153,8 +153,21 @@ const supportedExtensions = [
153
153
] ;
154
154
155
155
// tslint:disable-next-line: no-implicit-dependencies
156
- function parseWithVue ( vueTemplateCompiler : typeof import ( '@vue/compiler-sfc' ) , fileData : string ) {
157
- const { descriptor } = vueTemplateCompiler . parse ( fileData ) ;
156
+ function parseWithVue (
157
+ vueTemplateCompiler : typeof import ( '@vue/compiler-sfc' ) ,
158
+ typescriptPackage : typeof import ( 'typescript' ) ,
159
+ fileData : string ,
160
+ filePath : string ,
161
+ ) {
162
+ // Calls to registerTS are idempotent, so it's safe to call it repeatedly like
163
+ // we are here.
164
+ //
165
+ // See https://github.com/ardatan/graphql-tools/pull/7271 for more details.
166
+ //
167
+
168
+ vueTemplateCompiler . registerTS ( ( ) => typescriptPackage ) ;
169
+
170
+ const { descriptor } = vueTemplateCompiler . parse ( fileData , { filename : filePath } ) ;
158
171
159
172
return descriptor . script || descriptor . scriptSetup
160
173
? vueTemplateCompiler . compileScript ( descriptor , { id : Date . now ( ) . toString ( ) } ) . content
@@ -168,7 +181,7 @@ function customBlockFromVue(
168
181
filePath : string ,
169
182
blockType : string ,
170
183
) : Source | undefined {
171
- const { descriptor } = vueTemplateCompiler . parse ( fileData ) ;
184
+ const { descriptor } = vueTemplateCompiler . parse ( fileData , { filename : filePath } ) ;
172
185
173
186
const block = descriptor . customBlocks . find ( b => b . type === blockType ) ;
174
187
if ( block === undefined ) {
@@ -232,7 +245,7 @@ export const gqlPluckFromCodeString = async (
232
245
if ( options . gqlVueBlock ) {
233
246
blockSource = await pluckVueFileCustomBlock ( code , filePath , options . gqlVueBlock ) ;
234
247
}
235
- code = await pluckVueFileScript ( code ) ;
248
+ code = await pluckVueFileScript ( code , filePath ) ;
236
249
} else if ( fileExt === '.svelte' ) {
237
250
code = await pluckSvelteFileScript ( code ) ;
238
251
} else if ( fileExt === '.astro' ) {
@@ -273,7 +286,7 @@ export const gqlPluckFromCodeStringSync = (
273
286
if ( options . gqlVueBlock ) {
274
287
blockSource = pluckVueFileCustomBlockSync ( code , filePath , options . gqlVueBlock ) ;
275
288
}
276
- code = pluckVueFileScriptSync ( code ) ;
289
+ code = pluckVueFileScriptSync ( code , filePath ) ;
277
290
} else if ( fileExt === '.svelte' ) {
278
291
code = pluckSvelteFileScriptSync ( code ) ;
279
292
} else if ( fileExt === '.astro' ) {
@@ -391,6 +404,21 @@ const MissingGlimmerCompilerError = new Error(
391
404
` ) ,
392
405
) ;
393
406
407
+ const MissingTypeScriptPackageError = new Error (
408
+ freeText ( `
409
+ GraphQL template literals cannot be plucked from a Vue template code without having the "typescript" package installed.
410
+ Please install it and try again.
411
+
412
+ Via NPM:
413
+
414
+ $ npm install typescript
415
+
416
+ Via Yarn:
417
+
418
+ $ yarn add typescript
419
+ ` ) ,
420
+ ) ;
421
+
394
422
async function loadVueCompilerAsync ( ) {
395
423
try {
396
424
// eslint-disable-next-line import/no-extraneous-dependencies
@@ -400,6 +428,15 @@ async function loadVueCompilerAsync() {
400
428
}
401
429
}
402
430
431
+ async function loadTypeScriptPackageAsync ( ) {
432
+ try {
433
+ // eslint-disable-next-line import/no-extraneous-dependencies
434
+ return await import ( 'typescript' ) ;
435
+ } catch {
436
+ throw MissingTypeScriptPackageError ;
437
+ }
438
+ }
439
+
403
440
function loadVueCompilerSync ( ) {
404
441
try {
405
442
// eslint-disable-next-line import/no-extraneous-dependencies
@@ -409,18 +446,31 @@ function loadVueCompilerSync() {
409
446
}
410
447
}
411
448
412
- async function pluckVueFileScript ( fileData : string ) {
413
- const vueTemplateCompiler = await loadVueCompilerAsync ( ) ;
414
- return parseWithVue ( vueTemplateCompiler , fileData ) ;
449
+ function loadTypeScriptPackageSync ( ) {
450
+ try {
451
+ // eslint-disable-next-line import/no-extraneous-dependencies
452
+ return require ( 'typescript' ) ;
453
+ } catch {
454
+ throw MissingTypeScriptPackageError ;
455
+ }
415
456
}
416
457
417
- function pluckVueFileScriptSync ( fileData : string ) {
458
+ async function pluckVueFileScript ( fileData : string , filePath : string ) {
459
+ const [ typescriptPackage , vueTemplateCompiler ] = await Promise . all ( [
460
+ loadTypeScriptPackageAsync ( ) ,
461
+ loadVueCompilerAsync ( ) ,
462
+ ] ) ;
463
+ return parseWithVue ( vueTemplateCompiler , typescriptPackage , fileData , filePath ) ;
464
+ }
465
+
466
+ function pluckVueFileScriptSync ( fileData : string , filePath : string ) {
418
467
const vueTemplateCompiler = loadVueCompilerSync ( ) ;
419
- return parseWithVue ( vueTemplateCompiler , fileData ) ;
468
+ const typescriptPackage = loadTypeScriptPackageSync ( ) ;
469
+ return parseWithVue ( vueTemplateCompiler , typescriptPackage , fileData , filePath ) ;
420
470
}
421
471
422
472
async function pluckVueFileCustomBlock ( fileData : string , filePath : string , blockType : string ) {
423
- const vueTemplateCompiler = await loadVueCompilerSync ( ) ;
473
+ const vueTemplateCompiler = await loadVueCompilerAsync ( ) ;
424
474
return customBlockFromVue ( vueTemplateCompiler , fileData , filePath , blockType ) ;
425
475
}
426
476
0 commit comments