babel-plugin-graphql-tag
babel-plugin-graphql-tag copied to clipboard
Remove "source" field from emitted loc object
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.
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.
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.
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.
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