Skip to content

Commit 3dac6d8

Browse files
authored
Add a config option to add pure magic comment for static global variables (dotansimha#4380)
* Add a config option to add pure magic comment for static global variables * pureMagicComment is JS specific so make it optional * Remove extra space * Remove annoying warning * Fix
1 parent b044d32 commit 3dac6d8

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

.github/FUNDING.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
21
custom: https://www.buymeacoffee.com/JDnuAwr

packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

135140
export 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

151157
export 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);

packages/plugins/other/visitor-plugin-common/src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,9 @@ export class DeclarationBlock {
225225
return (
226226
(this._comment ? this._comment : '') +
227227
result +
228-
(this._kind === 'interface' || this._kind === 'enum' || this._kind === 'namespace' || this._kind === "function" ? '' : ';') +
228+
(this._kind === 'interface' || this._kind === 'enum' || this._kind === 'namespace' || this._kind === 'function'
229+
? ''
230+
: ';') +
229231
'\n'
230232
);
231233
}

0 commit comments

Comments
 (0)