babel-plugin-graphql-tag icon indicating copy to clipboard operation
babel-plugin-graphql-tag copied to clipboard

Remove "source" field from emitted loc object

Open evan-scott-zocdoc opened this issue 7 years ago • 4 comments

Not sure if this is possible or needed by the babel plugin, but it would be great to remove the "source" part of the "loc" object. It seems to just be bloat since the query is preprocessed into other objects.

evan-scott-zocdoc avatar May 02 '18 18:05 evan-scott-zocdoc

Near as I can tell, the source property is used to create unique cache keys during fragment processing. I think it's safe to eagerly normalize the value in this plugin, as it's mostly noisy whitespace in my experience.

evocateur avatar Nov 16 '18 17:11 evocateur

The source is taking bytes and computation for just generating a cache key. This can be optimized in compile time, for example, pre-generate a shorter hash instead of saving the full source.

const unsafeLocBody = hash(normalize(loc.source.body.substring(loc.start, loc.end)));
const loc = {
  start: 0,
  end: unsafeLocBody.length,
  source: {
    body: unsafeLocBody
  }
}

The drawback is that it is then tied to the implementation of graphql-tag. But I would still like to have it as an option. Maybe the plugin can provide a post-process callback so the user can do additional transformation to the generated object definition.

randing89 avatar Aug 18 '19 09:08 randing89

Given that the purpose of this babel plugin is to remove imports of graphql-tag, I would assume that the whole loc object of the AST can be removed unless there are any consumers of the AST's loc details, which seem unlikely - although I stand to be corrected.

bnjmnt4n avatar Jul 25 '20 23:07 bnjmnt4n

I think if you're only using Apollo, it's safe to remove. Would you accept a PR that changes the options to have strip: false | "none" | "source-whitespace" | "loc" (with true meaning source-whitespace for compatibility) and updating the docs to detail these options?

Happy to change naming too if you feel strongly

jacobp100 avatar Aug 09 '22 14:08 jacobp100