11import type { FragmentRegistry } from './registry'
22
3+ import MagicString from 'magic-string'
4+
35import { collectSpreads , scanDefinitions } from '../../runtime/utils/graphql/scanner'
46import { findGraphqlLiterals } from './literals'
57import { resolveFragments } from './registry'
@@ -16,23 +18,13 @@ export interface GraphqlTransformOptions {
1618 resolveRegistry : ( file : string ) => FragmentRegistry | undefined
1719}
1820
19- function applyInsertions ( source : string , insertions : Insertion [ ] ) {
20- let result = source
21-
22- for ( const insertion of [ ...insertions ] . sort ( ( left , right ) => right . at - left . at ) ) {
23- result = result . slice ( 0 , insertion . at ) + insertion . text + result . slice ( insertion . at )
24- }
25-
26- return result
27- }
28-
29- export function transformGraphqlLiterals ( code : string , file : string , options : GraphqlTransformOptions ) {
21+ function collectFragmentInsertions ( code : string , file : string , options : GraphqlTransformOptions ) {
3022 const registry = options . resolveRegistry ( file )
3123
32- if ( ! registry ) return
33-
3424 const insertions : Insertion [ ] = [ ]
3525
26+ if ( ! registry ) return insertions
27+
3628 for ( const literal of findGraphqlLiterals ( code ) ) {
3729 const definitions = scanDefinitions ( literal . content )
3830 const definesOperation = definitions . some ( definition => definition . kind === 'operation' )
@@ -62,25 +54,42 @@ export function transformGraphqlLiterals(code: string, file: string, options: Gr
6254 } )
6355 }
6456
57+ return insertions
58+ }
59+
60+ function rewrite ( code : string , file : string , options : GraphqlTransformOptions ) {
61+ const insertions = collectFragmentInsertions ( code , file , options )
62+
6563 if ( ! insertions . length ) return
6664
67- return applyInsertions ( code , insertions )
65+ const source = new MagicString ( code )
66+
67+ for ( const insertion of insertions ) source . appendLeft ( insertion . at , insertion . text )
68+
69+ return source
70+ }
71+
72+ export function transformGraphqlLiterals ( code : string , file : string , options : GraphqlTransformOptions ) {
73+ return rewrite ( code , file , options ) ?. toString ( )
6874}
6975
7076export function createGraphqlTransformPlugin ( options : GraphqlTransformOptions ) {
7177 return {
7278 name : 'nuxt-shopify:graphql' ,
73- enforce : 'pre ' as const ,
79+ enforce : 'post ' as const ,
7480
7581 transform ( code : string , id : string ) {
7682 if ( ! TRANSFORMABLE . test ( id ) || ! code . includes ( '#graphql' ) ) return
7783
7884 const file = id . split ( '?' ) [ 0 ] !
79- const transformed = transformGraphqlLiterals ( code , file , options )
85+ const transformed = rewrite ( code , file , options )
8086
8187 if ( ! transformed ) return
8288
83- return { code : transformed , map : null }
89+ return {
90+ code : transformed . toString ( ) ,
91+ map : transformed . generateMap ( { source : file , includeContent : true , hires : true } ) ,
92+ }
8493 } ,
8594 }
8695}
0 commit comments