@@ -130,6 +130,11 @@ export interface RawClientSideBasePluginConfig extends RawConfig {
130130 *
131131 */
132132 importDocumentNodeExternallyFrom ?: string ;
133+ /**
134+ * @default false
135+ * @description This config adds PURE magic comment to the static variables to enforce treeshaking for your bundler.
136+ */
137+ pureMagicComment ?: boolean ;
133138}
134139
135140export interface ClientSideBasePluginConfig extends ParsedConfig {
@@ -146,6 +151,7 @@ export interface ClientSideBasePluginConfig extends ParsedConfig {
146151 importDocumentNodeExternallyFrom ?: 'near-operation-file' | string ;
147152 importOperationTypesFrom ?: string ;
148153 globalNamespace ?: boolean ;
154+ pureMagicComment ?: boolean ;
149155}
150156
151157export class ClientSideBaseVisitor <
@@ -182,6 +188,7 @@ export class ClientSideBaseVisitor<
182188 return getConfigValue ( rawConfig . documentMode , DocumentMode . graphQLTag ) ;
183189 } ) ( rawConfig ) ,
184190 importDocumentNodeExternallyFrom : getConfigValue ( rawConfig . importDocumentNodeExternallyFrom , '' ) ,
191+ pureMagicComment : getConfigValue ( rawConfig . pureMagicComment , false ) ,
185192 ...additionalConfig ,
186193 } as any ) ;
187194
@@ -296,7 +303,9 @@ export class ClientSideBaseVisitor<
296303 const isDocumentNode =
297304 this . config . documentMode === DocumentMode . documentNode ||
298305 this . config . documentMode === DocumentMode . documentNodeImportFragments ;
299- return `export const ${ name } ${ isDocumentNode ? ': DocumentNode' : '' } = ${ this . _gql ( fragmentDocument ) } ;` ;
306+ return `export const ${ name } ${ isDocumentNode ? ': DocumentNode' : '' } =${
307+ this . config . pureMagicComment ? ' /*#__PURE__*/' : ''
308+ } ${ this . _gql ( fragmentDocument ) } ;`;
300309 }
301310
302311 private get fragmentsGraph ( ) : DepGraph < LoadedFragment > {
@@ -342,7 +351,7 @@ export class ClientSideBaseVisitor<
342351 return localFragments . join ( '\n' ) ;
343352 }
344353
345- protected _parseImport ( importStr : string ) {
354+ protected _parseImport ( importStr : string ) : { moduleName : string ; propName : string } {
346355 const [ moduleName , propName ] = importStr . split ( '#' ) ;
347356
348357 return {
@@ -443,7 +452,7 @@ export class ClientSideBaseVisitor<
443452 this . config . documentMode === DocumentMode . documentNodeImportFragments ;
444453 documentString = `${ this . config . noExport ? '' : 'export' } const ${ documentVariableName } ${
445454 isDocumentNode ? ': DocumentNode' : ''
446- } = ${ this . _gql ( node ) } ;`;
455+ } =${ this . config . pureMagicComment ? ' /*#__PURE__*/' : '' } ${ this . _gql ( node ) } ;`;
447456 }
448457
449458 const operationType : string = pascalCase ( node . operation ) ;
0 commit comments