Skip to content

Commit

Permalink
feat: add PURE annotations for bundlers
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed May 19, 2021
2 parents a3a15d4 + fd62d7c commit c49e30d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {stripIgnoredCharacters} from 'graphql';

const debug = createDebug('babel-plugin-graphql-tag');
const {
addComment,
cloneDeep,
isIdentifier,
isMemberExpression,
Expand All @@ -24,6 +25,8 @@ const {
isObjectPattern,
} = types;

const PURE_ANNOTATION = '#__PURE__';

// eslint-disable-next-line no-restricted-syntax
const uniqueFn = parseExpression(`
(definitions) => {
Expand Down Expand Up @@ -109,6 +112,12 @@ export default declare((api, options) => {

definitionsProperty.value = callExpression(uniqueId, [allDefinitions]);

// Marking these function calls with an annotation indicating that
// they're pure will allow bundlers like Webpack and Rollup to more
// aggressively remove unused queries from bundles.
addComment(allDefinitions, 'leading', PURE_ANNOTATION);
addComment(definitionsProperty.value, 'leading', PURE_ANNOTATION);

uniqueUsed = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const baz = {
};
const foo = {
"kind": "Document",
"definitions": _unique([{
"definitions": /*#__PURE__*/_unique( /*#__PURE__*/[{
"kind": "OperationDefinition",
"operation": "query",
"name": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const baz = {
};
const foo = {
"kind": "Document",
"definitions": _unique([{
"definitions": /*#__PURE__*/_unique( /*#__PURE__*/[{
"kind": "OperationDefinition",
"operation": "query",
"name": {
Expand Down

0 comments on commit c49e30d

Please sign in to comment.